[3.8] bpo-40417: Fix deprecation warning in PyImport_ReloadModule (GH-19750) (GH-19934)

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

Co-authored-by: Robert Rouhani <robert.rouhani@gmail.com>
This commit is contained in:
Robert Rouhani 2020-05-05 17:32:14 -07:00 committed by GitHub
parent 2a3b876b02
commit a32587a60d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -0,0 +1 @@
Fix imp module deprecation warning when PyImport_ReloadModule is called. Patch by Robert Rouhani.

View File

@ -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;
}