@@ -236,10 +236,7 @@ common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
236236// `scalar`. If scalar is not already a string and
237237// coerce is nonzero, __str__ is called to convert it
238238// to a string. If coerce is zero, raises an error for
239- // non-string or non-NA input. If the scalar is the
240- // na_object for the dtype class, return a new
241- // reference to the na_object.
242-
239+ // non-string or non-NA input.
243240static PyObject *
244241get_value (PyObject * scalar , int coerce )
245242{
@@ -260,9 +257,11 @@ get_value(PyObject *scalar, int coerce)
260257 }
261258 }
262259 }
260+ else {
261+ Py_INCREF (scalar );
262+ }
263263
264- // attempt to decode as UTF8
265- return PyUnicode_AsUTF8String (scalar );
264+ return scalar ;
266265}
267266
268267static PyArray_Descr *
@@ -274,6 +273,8 @@ string_discover_descriptor_from_pyobject(PyTypeObject *NPY_UNUSED(cls),
274273 return NULL ;
275274 }
276275
276+ Py_DECREF (val );
277+
277278 PyArray_Descr * ret = (PyArray_Descr * )new_stringdtype_instance (NULL , 1 );
278279
279280 return ret ;
@@ -311,9 +312,9 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
311312 goto fail ;
312313 }
313314
314- char * val = NULL ;
315315 Py_ssize_t length = 0 ;
316- if (PyBytes_AsStringAndSize (val_obj , & val , & length ) == -1 ) {
316+ const char * val = PyUnicode_AsUTF8AndSize (val_obj , & length );
317+ if (val == NULL ) {
317318 Py_DECREF (val_obj );
318319 goto fail ;
319320 }
@@ -325,6 +326,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
325326 Py_DECREF (val_obj );
326327 goto fail ;
327328 }
329+ Py_DECREF (val_obj );
328330 }
329331
330332 NPY_STRING_RELEASE_ALLOCATOR (descr );
0 commit comments