Address feedback

This commit is contained in:
Pablo Galindo Salgado 2025-12-06 17:51:38 +00:00
parent ab07b14d11
commit 76846fedf4
7 changed files with 50 additions and 49 deletions

View file

@ -349,7 +349,7 @@ Importing Modules
.. c:function:: PyObject* PyImport_GetLazyImportsFilter()
Gets the current lazy imports filter. Returns a new reference.
Gets the current lazy imports filter. Returns a :term:`strong reference`.
.. versionadded:: next
@ -364,17 +364,18 @@ Importing Modules
Sets the current lazy imports filter. The function should be a callable that
will receive (importing_module_name, imported_module_name, [fromlist]) when
an import can potentially be lazy. Returns True if the import should be lazy
or False otherwise.
an import can potentially be lazy. Returns ``True`` if the import should be lazy
or ``False`` otherwise.
.. versionadded:: next
.. c:type:: PyImport_LazyImportsMode
Enumeration of possible lazy import modes:
- ``PyImport_LAZY_NORMAL``
- ``PyImport_LAZY_ALL``
- ``PyImport_LAZY_NONE``
- :c:enumerator:`PyImport_LAZY_NORMAL`
- :c:enumerator:`PyImport_LAZY_ALL`
- :c:enumerator:`PyImport_LAZY_NONE`
.. versionadded:: next

View file

@ -76,6 +76,10 @@ def _m(self): pass
# CapsuleType cannot be accessed from pure Python,
# so there is no fallback definition.
exec("lazy import sys as _lazy_sys", _lz := {})
LazyImportType = type(_lz['_lazy_sys'])
del _lz
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export

View file

@ -23,12 +23,9 @@ _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
if (m == NULL) {
return NULL;
}
Py_XINCREF(builtins);
m->lz_builtins = builtins;
Py_INCREF(from);
m->lz_from = from;
Py_XINCREF(attr);
m->lz_attr = attr;
m->lz_builtins = Py_XNewRef(builtins);
m->lz_from = Py_NewRef(from);
m->lz_attr = Py_XNewRef(attr);
/* Capture frame information for the original import location */
m->lz_code = NULL;
@ -44,18 +41,35 @@ _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
}
}
PyObject_GC_Track(m);
_PyObject_GC_TRACK(m);
return (PyObject *)m;
}
static int
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
{
Py_VISIT(m->lz_builtins);
Py_VISIT(m->lz_from);
Py_VISIT(m->lz_attr);
Py_VISIT(m->lz_code);
return 0;
}
static int
lazy_import_clear(PyLazyImportObject *m)
{
Py_CLEAR(m->lz_builtins);
Py_CLEAR(m->lz_from);
Py_CLEAR(m->lz_attr);
Py_CLEAR(m->lz_code);
return 0;
}
static void
lazy_import_dealloc(PyLazyImportObject *m)
{
PyObject_GC_UnTrack(m);
Py_XDECREF(m->lz_builtins);
Py_XDECREF(m->lz_from);
Py_XDECREF(m->lz_attr);
Py_XDECREF(m->lz_code);
_PyObject_GC_UNTRACK(m);
lazy_import_clear(m);
Py_TYPE(m)->tp_free((PyObject *)m);
}
@ -85,26 +99,6 @@ lazy_import_repr(PyLazyImportObject *m)
return res;
}
static int
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
{
Py_VISIT(m->lz_builtins);
Py_VISIT(m->lz_from);
Py_VISIT(m->lz_attr);
Py_VISIT(m->lz_code);
return 0;
}
static int
lazy_import_clear(PyLazyImportObject *m)
{
Py_CLEAR(m->lz_builtins);
Py_CLEAR(m->lz_from);
Py_CLEAR(m->lz_attr);
Py_CLEAR(m->lz_code);
return 0;
}
static PyObject *
lazy_import_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{

View file

@ -51,7 +51,6 @@ assert_def_missing_or_redundant(PyModuleObject *m)
}
PyTypeObject PyModuleDef_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"moduledef", /* tp_name */

View file

@ -13,10 +13,10 @@ unsigned char M_test_frozenmain[] = {
82,5,93,6,12,0,82,6,93,5,93,6,44,26,0,0,
0,0,0,0,0,0,0,0,12,0,50,4,52,1,0,0,
0,0,0,0,31,0,75,26,0,0,9,0,30,0,82,1,
35,0,41,8,233,0,0,0,0,78,218,18,70,114,111,122,
101,110,32,72,101,108,108,111,32,87,111,114,108,100,218,8,
35,0,41,8,233,0,0,0,0,78,122,18,70,114,111,122,
101,110,32,72,101,108,108,111,32,87,111,114,108,100,122,8,
115,121,115,46,97,114,103,118,218,6,99,111,110,102,105,103,
218,7,99,111,110,102,105,103,32,218,2,58,32,41,5,218,
122,7,99,111,110,102,105,103,32,122,2,58,32,41,5,218,
12,112,114,111,103,114,97,109,95,110,97,109,101,218,10,101,
120,101,99,117,116,97,98,108,101,218,15,117,115,101,95,101,
110,118,105,114,111,110,109,101,110,116,218,17,99,111,110,102,
@ -25,15 +25,15 @@ unsigned char M_test_frozenmain[] = {
3,115,121,115,218,17,95,116,101,115,116,105,110,116,101,114,
110,97,108,99,97,112,105,218,5,112,114,105,110,116,218,4,
97,114,103,118,218,11,103,101,116,95,99,111,110,102,105,103,
115,114,5,0,0,0,218,3,107,101,121,169,0,243,0,0,
115,114,3,0,0,0,218,3,107,101,121,169,0,243,0,0,
0,0,218,18,116,101,115,116,95,102,114,111,122,101,110,109,
97,105,110,46,112,121,218,8,60,109,111,100,117,108,101,62,
114,22,0,0,0,1,0,0,0,115,94,0,0,0,240,3,
114,18,0,0,0,1,0,0,0,115,94,0,0,0,240,3,
1,1,1,243,8,0,1,11,219,0,24,225,0,5,208,6,
26,212,0,27,217,0,5,128,106,144,35,151,40,145,40,212,
0,27,216,9,26,215,9,38,210,9,38,211,9,40,168,24,
213,9,50,128,6,243,2,6,12,2,128,67,241,14,0,5,
10,136,71,144,67,144,53,152,2,152,54,160,35,157,59,152,
45,208,10,40,214,4,41,243,15,6,12,2,114,20,0,0,
45,208,10,40,214,4,41,243,15,6,12,2,114,16,0,0,
0,
};

View file

@ -307,7 +307,7 @@ static PyObject *
builtin___lazy_import___impl(PyObject *module, PyObject *name,
PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)
/*[clinic end generated code: output=300f1771094b9e8c input=57123e246d6c36ee]*/
/*[clinic end generated code: output=300f1771094b9e8c input=9394874f340b2948]*/
{
PyObject *builtins;
PyThreadState *tstate = PyThreadState_GET();
@ -318,11 +318,14 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
locals = globals;
}
builtins = PyMapping_GetItemString(globals, "__builtins__");
if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
return NULL;
}
if (builtins == NULL) {
PyErr_SetString(PyExc_ValueError, "unable to get builtins for lazy import");
return NULL;
} else if (PyModule_Check(builtins)) {
}
if (PyModule_Check(builtins)) {
PyObject *builtins_dict = Py_XNewRef(PyModule_GetDict(builtins));
if (builtins_dict == NULL) {
PyErr_SetString(PyExc_AttributeError, "builtins module has no dict");