Skip to content

bpo-29849: fix memory leak in import_from #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5017,7 +5017,7 @@ import_from(PyObject *v, PyObject *name)
{
PyObject *x;
_Py_IDENTIFIER(__name__);
PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown;
PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;

x = PyObject_GetAttr(v, name);
if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
Expand All @@ -5032,6 +5032,7 @@ import_from(PyObject *v, PyObject *name)
}
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
if (fullmodname == NULL) {
Py_DECREF(pkgname);
return NULL;
}
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Expand All @@ -5056,17 +5057,21 @@ import_from(PyObject *v, PyObject *name)

if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
PyErr_Clear();
PyErr_SetImportError(
PyUnicode_FromFormat("cannot import name %R from %R (unknown location)",
name, pkgname_or_unknown),
pkgname, NULL);
errmsg = PyUnicode_FromFormat(
"cannot import name %R from %R (unknown location)",
name, pkgname_or_unknown
);
/* doesn't check NULL for errmsg, PyErr_SetImportError will catch */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the comment is necessary long-term.

Copy link
Member Author

@zhangyangyu zhangyangyu Mar 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is to make others know the error check is left out deliberately. It is not an oversight.
Do you think it's a must to remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep it then please change it to "/* NULL check for errmsg done by PyErr_SetImportError. */".

PyErr_SetImportError(errmsg, pkgname, NULL);
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here you might as well fix the formatting for this.

PyErr_SetImportError(
PyUnicode_FromFormat("cannot import name %R from %R (%S)",
name, pkgname_or_unknown, pkgpath),
pkgname, pkgpath);
errmsg = PyUnicode_FromFormat(
"cannot import name %R from %R (%S)",
name, pkgname_or_unknown, pkgpath
);
PyErr_SetImportError(errmsg, pkgname, pkgpath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a similar comment here if you are going to leave it in.

}

Py_XDECREF(errmsg);
Py_XDECREF(pkgname_or_unknown);
Py_XDECREF(pkgpath);
return NULL;
Expand Down