diff options
| author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2024-12-10 05:21:55 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-10 05:21:55 (GMT) |
| commit | 6441d42f92f9daa2f3d9ff0ec3de0d60c26e041d (patch) | |
| tree | f8b42dbfb40464c13dfbd46e3af3131e842a33f4 /Python | |
| parent | 310efdabf556c513de9bf38820e20f5289931b55 (diff) | |
| download | cpython-6441d42f92f9daa2f3d9ff0ec3de0d60c26e041d.zip cpython-6441d42f92f9daa2f3d9ff0ec3de0d60c26e041d.tar.gz cpython-6441d42f92f9daa2f3d9ff0ec3de0d60c26e041d.tar.bz2 | |
[3.13] gh-127651: Use __file__ in diagnostics if origin is missing (#127660) (#127775)
gh-127651: Use __file__ in diagnostics if origin is missing (#127660)
See the left hand side in https://github.com/python/cpython/pull/123929/files#diff-c22186367cbe20233e843261998dc027ae5f1f8c0d2e778abfa454ae74cc59deL2840-L2849
---------
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit 3983527c3a6b389e373a233e514919555853ccb3)
Diffstat (limited to 'Python')
| -rw-r--r-- | Python/ceval.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index d970ffa..c502a53 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2785,6 +2785,20 @@ import_from(PyThreadState *tstate, PyObject *v, PyObject *name) } } + if (origin == NULL) { + // Fall back to __file__ for diagnostics if we don't have + // an origin that is a location + origin = PyModule_GetFilenameObject(v); + if (origin == NULL) { + if (!PyErr_ExceptionMatches(PyExc_SystemError)) { + goto done; + } + // PyModule_GetFilenameObject raised "module filename missing" + _PyErr_Clear(tstate); + } + assert(origin == NULL || PyUnicode_Check(origin)); + } + if (is_possibly_shadowing_stdlib) { assert(origin); errmsg = PyUnicode_FromFormat( @@ -2845,9 +2859,11 @@ import_from(PyThreadState *tstate, PyObject *v, PyObject *name) } done_with_errmsg: - /* NULL checks for errmsg, mod_name, origin done by PyErr_SetImportError. */ - _PyErr_SetImportErrorWithNameFrom(errmsg, mod_name, origin, name); - Py_DECREF(errmsg); + if (errmsg != NULL) { + /* NULL checks for mod_name and origin done by _PyErr_SetImportErrorWithNameFrom */ + _PyErr_SetImportErrorWithNameFrom(errmsg, mod_name, origin, name); + Py_DECREF(errmsg); + } done: Py_XDECREF(origin); |
