Skip to content

Commit

Permalink
[3.8] bpo-40417: Fix deprecation warning in PyImport_ReloadModule (py…
Browse files Browse the repository at this point in the history
…thonGH-19750)

I can add another commit with the new test case I wrote to verify that the warning was being printed before my change, stopped printing after my change, and that the function does not return null after my change.

Automerge-Triggered-By: @brettcannon.
(cherry picked from commit f40bd46)

Co-authored-by: Robert Rouhani <robert.rouhani@gmail.com>
  • Loading branch information
Robmaister committed May 5, 2020
1 parent efc782d commit 78bd252
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix imp module deprecation warning when PyImport_ReloadModule is called. Patch by Robert Rouhani.
14 changes: 7 additions & 7 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,23 +1908,23 @@ PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals
PyObject *
PyImport_ReloadModule(PyObject *m)
{
_Py_IDENTIFIER(imp);
_Py_IDENTIFIER(importlib);
_Py_IDENTIFIER(reload);
PyObject *reloaded_module = NULL;
PyObject *imp = _PyImport_GetModuleId(&PyId_imp);
if (imp == NULL) {
PyObject *importlib = _PyImport_GetModuleId(&PyId_importlib);
if (importlib == NULL) {
if (PyErr_Occurred()) {
return NULL;
}

imp = PyImport_ImportModule("imp");
if (imp == NULL) {
importlib = PyImport_ImportModule("importlib");
if (importlib == NULL) {
return NULL;
}
}

reloaded_module = _PyObject_CallMethodIdObjArgs(imp, &PyId_reload, m, NULL);
Py_DECREF(imp);
reloaded_module = _PyObject_CallMethodIdObjArgs(importlib, &PyId_reload, m, NULL);
Py_DECREF(importlib);
return reloaded_module;
}

Expand Down

0 comments on commit 78bd252

Please sign in to comment.