-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-142349: Implement PEP 810 #142351
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
base: main
Are you sure you want to change the base?
gh-142349: Implement PEP 810 #142351
Conversation
…s on bad usage of lazy
Highlight lazy imports in the new REPL
|
I'll be doing small reviews because I'm not on my dev session and it takes too long for creating a PR :') I promise I won't be too nitpicky! |
| pair: name; binding | ||
| pair: keyword; from | ||
| pair: keyword; as | ||
| pair: keyword; lazy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you move this to the below section on lazy specifically.
picnixz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll review more tomorrow when I'm on my dev session, it'll be easier. But here are a few comments and questions about import.c
| PyImport_GetLazyImportsFilter(void) | ||
| { | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| return Py_XNewRef(FT_ATOMIC_LOAD_PTR_RELAXED(LAZY_IMPORTS_FILTER(interp))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the import filter is not set, this may return NULL without an exception set. So either you want it to be documented or you want to return None instead.
| PyObject *old = _Py_atomic_exchange_ptr(&LAZY_IMPORTS_FILTER(interp), Py_XNewRef(filter)); | ||
| Py_XDECREF(old); | ||
| #else | ||
| Py_XSETREF(LAZY_IMPORTS_FILTER(interp), Py_XNewRef(filter)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wondeer whether we couldn't have a FT helper for this. It's probably not the only place that will need this in the future.
| if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &modname) < 0) { | ||
| return NULL; | ||
| } else if (modname == NULL) { | ||
| modname = Py_None; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be two separate ifs right? because you might have modname == NULL if the name doesn't exist.
| if (filter != NULL) { | ||
| PyObject *modname; | ||
| if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &modname) < 0) { | ||
| return NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to decref abs_name here (you can also have a goto error maybe?)
Python/import.c
Outdated
| if (!PyObject_IsTrue(res)) { | ||
| Py_DECREF(abs_name); | ||
| return PyImport_ImportModuleLevelObject( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just me thinking aloud but is it possible to have a UAF here because filter is an arbitrary callable (so, we could alter fromlist)?
|
|
||
| // Add the module name to sys.lazy_modules set (PEP 810) | ||
| PyObject *lazy_modules_set = interp->imports.lazy_modules_set; | ||
| if (lazy_modules_set != NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is it possible to have lazy_modules_set NULL?
| } | ||
|
|
||
| child_dict = get_mod_dict(child_module); | ||
| if (child_dict == NULL || !PyDict_CheckExact(child_dict)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
child_dict can be NULL and we must go to error here
|
|
||
| lazy import json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| lazy import json | |
| lazy import json | |
| lazy import sys |
For 'json' in sys.modules to run.
|
@DinoV @colesbury I haven't paid attention to FT when I updated the draft implementation so please take a special look at that |
In particular I think I got them in 470b9e4 but not sure if that's the best way to fix this. |
📚 Documentation preview 📚: https://cpython-previews--142351.org.readthedocs.build/