Skip to content

Commit 21da3a5

Browse files
committed
fix reference counting bug in usage of get_value
1 parent c186561 commit 21da3a5

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

stringdtype/stringdtype/src/dtype.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
243240
static PyObject *
244241
get_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

268267
static 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

Comments
 (0)