GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-91058: Add error suggestions to 'import from' import errors by pablogsal · Pull Request #98305 · python/cpython Conversation
|
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit 77f9de1 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit cd528b6 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Signed-off-by: Pablo Galindo <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks amazing (and a very useful error) @pablogsal. Just a quick question
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return NULL; | ||
| } | ||
|
|
||
| PyObject* mod = PyImport_Import(mod_name); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I was wondering whether we could avoid "realy-importing" a module if it isn't already (e.g. whether it is currently cached or not)? (IIRC from x import y should put the x to the sys.modules, but might be missing something too).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
PyImport_Import goes through the cache if for whatever reason the module is already imported, no? (And indeed, the module is imported as part of raising the exception).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
PyImport_Import goes through the cache if for whatever reason the module is already imported, no? (And indeed, the module is imported as part of raising the exception).
Exactly, but consider the scenario where it isn't already imported and the module itself has some side effects (like it prints something at the top level). Importing it would then might produce unexpected side effects that only appear when the traceback is printed, so I thought whether we could limit this hint when the module is already cached.
if module not in sys.modules: return None # give up if it isn't already in the cache
module = PyImport_Import(mod_name)
...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
but consider the scenario where it isn't already imported and the module itself has some side effects
Hummmm. But isn't this a pre-condition for this kind of import error? If from foo import x fails to import foo then there is no lookup failure and we will never arrive here. And to be able to look x in foo then foo needs to be in sys.modules. Maybe I am missing something ... :(
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think it's at least theoretically possible for the module to be removed by now, because some Python code may run between the time the error is thrown and the time this code runs. For example, in pythonrun.c we call print_exception_message() before print_exception_suggestions(), and that does a bunch of allocations and DECREFs that could trigger GC.
You could call https://docs.python.org/3/c-api/import.html#c.PyImport_GetModule instead to return the module only if it's already imported.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit dee788b 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks, a few suggestions.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| return NULL; | ||
| } | ||
|
|
||
| PyObject* mod = PyImport_Import(mod_name); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think it's at least theoretically possible for the module to be removed by now, because some Python code may run between the time the error is thrown and the time this code runs. For example, in pythonrun.c we call print_exception_message() before print_exception_suggestions(), and that does a bunch of allocations and DECREFs that could trigger GC.
You could call https://docs.python.org/3/c-api/import.html#c.PyImport_GetModule instead to return the module only if it's already imported.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.