@@ -207,7 +207,8 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
207207 npy_packed_static_string * out_pss = (npy_packed_static_string * )out ;
208208 npy_string_free (out_pss );
209209 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" );
211212 return -1 ;
212213 }
213214 npy_static_string out_ss = {0 , NULL };
@@ -336,7 +337,7 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[],
336337 while (N -- ) {
337338 const npy_packed_static_string * ps = (npy_packed_static_string * )in ;
338339 npy_static_string s = {0 , NULL };
339- npy_static_string name = { 0 , NULL } ;
340+ npy_static_string name ;
340341 unsigned char * this_string = NULL ;
341342 size_t n_bytes ;
342343 int is_null = npy_load_string (ps , & s );
@@ -481,23 +482,28 @@ bool_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
481482 while (N -- ) {
482483 npy_packed_static_string * out_pss = (npy_packed_static_string * )out ;
483484 npy_string_free (out_pss );
485+ char * ret_val = NULL ;
486+ size_t size = 0 ;
484487 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 ;
489490 }
490491 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 ;
495494 }
496495 else {
497496 gil_error (PyExc_RuntimeError ,
498497 "invalid value encountered in bool to string cast" );
499498 return -1 ;
500499 }
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+ }
501507 in += in_stride ;
502508 out += out_stride ;
503509 }
@@ -593,7 +599,9 @@ pyobj_to_string(PyObject *obj, char *out)
593599 npy_packed_static_string * out_ss = (npy_packed_static_string * )out ;
594600 npy_string_free (out_ss );
595601 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." );
597605 Py_DECREF (pystr_val );
598606 return -1 ;
599607 }
@@ -1087,7 +1095,8 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[],
10871095 if (npy_string_newsize (datetime_buf , strlen (datetime_buf ),
10881096 out_pss ) < 0 ) {
10891097 PyErr_SetString (PyExc_MemoryError ,
1090- "npy_string_newsize failed" );
1098+ "Failed to allocate string when converting "
1099+ "from a datetime." );
10911100 return -1 ;
10921101 }
10931102 }
0 commit comments