Skip to content

Commit a8c1704

Browse files
committed
Hold name_obj until filename use is over
1 parent a40bdbd commit a8c1704

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

bindings/python/auparse_python.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,29 +499,38 @@ AuParser_init(AuParser *self, PyObject *args, PyObject *kwds)
499499
PyErr_SetFromErrno(PyExc_EnvironmentError);
500500
return -1;
501501
}
502-
const char *filename = NULL;
502+
const char *filename = NULL;
503+
#if PY_MAJOR_VERSION >= 3
504+
PyObject *name_obj = NULL;
505+
#endif
503506
#if PY_MAJOR_VERSION < 3
504507
/* PyFile_Name is available in Python 2 */
505508
filename = PYSTR_ASSTRING(PyFile_Name(source));
506509
#else
507510
/* In Python 3 obtain the name attribute if possible */
508-
PyObject *name_obj = PyObject_GetAttrString(source, "name");
511+
name_obj = PyObject_GetAttrString(source, "name");
509512
if (name_obj && PYSTR_CHECK(name_obj))
510513
filename = PYSTR_ASSTRING(name_obj);
511-
Py_XDECREF(name_obj);
512514
#endif
513515
if ((self->au = auparse_init(source_type, fp)) == NULL) {
514516
if (filename)
515517
PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
516518
else
517519
PyErr_SetFromErrno(PyExc_IOError);
520+
#if PY_MAJOR_VERSION >= 3
521+
Py_XDECREF(name_obj);
522+
#endif
518523
fclose(fp);
519524
return -1;
520525
}
526+
#if PY_MAJOR_VERSION >= 3
527+
Py_XDECREF(name_obj);
528+
#endif
521529
} break;
522530
case AUSOURCE_FEED: {
523531
if (source != Py_None) {
524-
PyErr_SetString(PyExc_ValueError, "source must be None when source_type is AUSOURCE_FEED");
532+
PyErr_SetString(PyExc_ValueError,
533+
"source must be None when source_type is AUSOURCE_FEED");
525534
return -1;
526535
}
527536
if ((self->au = auparse_init(source_type, NULL)) == NULL) {

0 commit comments

Comments
 (0)