@@ -236,10 +236,7 @@ common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
236
236
// `scalar`. If scalar is not already a string and
237
237
// coerce is nonzero, __str__ is called to convert it
238
238
// 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.
243
240
static PyObject *
244
241
get_value (PyObject * scalar , int coerce )
245
242
{
@@ -260,9 +257,11 @@ get_value(PyObject *scalar, int coerce)
260
257
}
261
258
}
262
259
}
260
+ else {
261
+ Py_INCREF (scalar );
262
+ }
263
263
264
- // attempt to decode as UTF8
265
- return PyUnicode_AsUTF8String (scalar );
264
+ return scalar ;
266
265
}
267
266
268
267
static PyArray_Descr *
@@ -274,6 +273,8 @@ string_discover_descriptor_from_pyobject(PyTypeObject *NPY_UNUSED(cls),
274
273
return NULL ;
275
274
}
276
275
276
+ Py_DECREF (val );
277
+
277
278
PyArray_Descr * ret = (PyArray_Descr * )new_stringdtype_instance (NULL , 1 );
278
279
279
280
return ret ;
@@ -311,9 +312,9 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
311
312
goto fail ;
312
313
}
313
314
314
- char * val = NULL ;
315
315
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 ) {
317
318
Py_DECREF (val_obj );
318
319
goto fail ;
319
320
}
@@ -325,6 +326,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
325
326
Py_DECREF (val_obj );
326
327
goto fail ;
327
328
}
329
+ Py_DECREF (val_obj );
328
330
}
329
331
330
332
NPY_STRING_RELEASE_ALLOCATOR (descr );
0 commit comments