Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ad57cf

Browse files
committed
BUG: Cleanup/fix some refcounting PyArray_EnsureArray steals refs...
1 parentb34c59a commit2ad57cf

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

‎numpy/_core/src/multiarray/array_converter.c‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,20 @@ array_converter_as_arrays(PyArrayArrayConverterObject *self,
277277
PyObject*res_item;
278278
if (item->descr==NULL&&policy==PRESERVE) {
279279
res_item=item->object;
280+
Py_INCREF(res_item);
280281
}
281282
else {
282283
res_item= (PyObject*)item->array;
283-
}
284-
Py_INCREF(res_item);
285-
286-
if (!subok) {
287-
Py_SETREF(res_item,PyArray_EnsureArray(res_item));
288-
if (res_item==NULL) {
289-
gotofail;
284+
Py_INCREF(res_item);
285+
if (!subok) {
286+
/* PyArray_EnsureArray steals the reference... */
287+
res_item=PyArray_EnsureArray(res_item);
288+
if(res_item==NULL) {
289+
gotofail;
290+
}
290291
}
291292
}
293+
292294
if (PyTuple_SetItem(res,i,res_item)<0) {
293295
gotofail;
294296
}
@@ -331,9 +333,8 @@ array_converter_wrap(PyArrayArrayConverterObject *self,
331333
}
332334
}
333335

334-
Py_INCREF(obj);
335-
336336
if (self->wrap==Py_None) {
337+
Py_INCREF(obj);
337338
if (ensure_scalar&&PyArray_Check(obj)) {
338339
/* Use PyArray_Return to convert to scalar when necessary */
339340
returnPyArray_Return((PyArrayObject*)obj);
@@ -356,16 +357,21 @@ array_converter_wrap(PyArrayArrayConverterObject *self,
356357
* TODO: It might be best if this was always an array here. However,
357358
* for now scalars are common enough so convert them.
358359
*/
359-
if (!PyArray_Check(obj)) {
360-
Py_SETREF(obj,PyArray_FromAny(obj,NULL,0,0,0,NULL));
361-
if (obj==NULL) {
360+
PyObject*arr;
361+
if (PyArray_Check(obj)) {
362+
Py_INCREF(obj);
363+
arr=obj;
364+
}
365+
else {
366+
arr=PyArray_FromAny(obj,NULL,0,0,0,NULL);
367+
if (arr==NULL) {
362368
returnNULL;
363369
}
364370
}
365371

366372
/* There is no solution here for preferring a scalar return */
367-
res=PyObject_CallOneArg(self->wrap,obj);
368-
Py_DECREF(obj);
373+
res=PyObject_CallOneArg(self->wrap,arr);
374+
Py_DECREF(arr);
369375
}
370376

371377
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp