@@ -207,7 +207,8 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
207
207
npy_packed_static_string * out_pss = (npy_packed_static_string * )out ;
208
208
npy_string_free (out_pss );
209
209
if (npy_string_newemptysize (out_num_bytes , out_pss ) < 0 ) {
210
- gil_error (PyExc_MemoryError , "npy_string_newemptysize failed" );
210
+ gil_error (PyExc_MemoryError ,
211
+ "Failed to allocate string in unicode to string cast" );
211
212
return -1 ;
212
213
}
213
214
npy_static_string out_ss = {0 , NULL };
@@ -336,7 +337,7 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[],
336
337
while (N -- ) {
337
338
const npy_packed_static_string * ps = (npy_packed_static_string * )in ;
338
339
npy_static_string s = {0 , NULL };
339
- npy_static_string name = { 0 , NULL } ;
340
+ npy_static_string name ;
340
341
unsigned char * this_string = NULL ;
341
342
size_t n_bytes ;
342
343
int is_null = npy_load_string (ps , & s );
@@ -481,23 +482,28 @@ bool_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
481
482
while (N -- ) {
482
483
npy_packed_static_string * out_pss = (npy_packed_static_string * )out ;
483
484
npy_string_free (out_pss );
485
+ char * ret_val = NULL ;
486
+ size_t size = 0 ;
484
487
if ((npy_bool )(* in ) == 1 ) {
485
- if (npy_string_newsize ("True" , 4 , out_pss ) < 0 ) {
486
- gil_error (PyExc_MemoryError , "npy_string_newsize failed" );
487
- return -1 ;
488
- }
488
+ ret_val = "True" ;
489
+ size = 4 ;
489
490
}
490
491
else if ((npy_bool )(* in ) == 0 ) {
491
- if (npy_string_newsize ("False" , 5 , out_pss ) < 0 ) {
492
- gil_error (PyExc_MemoryError , "npy_string_newsize failed" );
493
- return -1 ;
494
- }
492
+ ret_val = "False" ;
493
+ size = 5 ;
495
494
}
496
495
else {
497
496
gil_error (PyExc_RuntimeError ,
498
497
"invalid value encountered in bool to string cast" );
499
498
return -1 ;
500
499
}
500
+ if (npy_string_newsize (ret_val , size , out_pss ) < 0 ) {
501
+ // execution should never get here because this will be a small
502
+ // string on all platforms
503
+ gil_error (PyExc_MemoryError ,
504
+ "Failed to allocate string in bool to string cast" );
505
+ return -1 ;
506
+ }
501
507
in += in_stride ;
502
508
out += out_stride ;
503
509
}
@@ -593,7 +599,9 @@ pyobj_to_string(PyObject *obj, char *out)
593
599
npy_packed_static_string * out_ss = (npy_packed_static_string * )out ;
594
600
npy_string_free (out_ss );
595
601
if (npy_string_newsize (cstr_val , length , out_ss ) < 0 ) {
596
- PyErr_SetString (PyExc_MemoryError , "npy_string_newsize failed" );
602
+ PyErr_SetString (PyExc_MemoryError ,
603
+ "Failed to allocate numpy string when converting from "
604
+ "python string." );
597
605
Py_DECREF (pystr_val );
598
606
return -1 ;
599
607
}
@@ -1087,7 +1095,8 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[],
1087
1095
if (npy_string_newsize (datetime_buf , strlen (datetime_buf ),
1088
1096
out_pss ) < 0 ) {
1089
1097
PyErr_SetString (PyExc_MemoryError ,
1090
- "npy_string_newsize failed" );
1098
+ "Failed to allocate string when converting "
1099
+ "from a datetime." );
1091
1100
return -1 ;
1092
1101
}
1093
1102
}
0 commit comments