Skip to content

Commit 76846fe

Browse files
committed
Address feedback
1 parent ab07b14 commit 76846fe

File tree

7 files changed

+50
-49
lines changed

7 files changed

+50
-49
lines changed

Doc/c-api/import.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ Importing Modules
349349
350350
.. c:function:: PyObject* PyImport_GetLazyImportsFilter()
351351
352-
Gets the current lazy imports filter. Returns a new reference.
352+
Gets the current lazy imports filter. Returns a :term:`strong reference`.
353353
354354
.. versionadded:: next
355355
@@ -364,17 +364,18 @@ Importing Modules
364364
365365
Sets the current lazy imports filter. The function should be a callable that
366366
will receive (importing_module_name, imported_module_name, [fromlist]) when
367-
an import can potentially be lazy. Returns True if the import should be lazy
368-
or False otherwise.
367+
an import can potentially be lazy. Returns ``True`` if the import should be lazy
368+
or ``False`` otherwise.
369369
370370
.. versionadded:: next
371371
372372
.. c:type:: PyImport_LazyImportsMode
373373
374374
Enumeration of possible lazy import modes:
375-
- ``PyImport_LAZY_NORMAL``
376-
- ``PyImport_LAZY_ALL``
377-
- ``PyImport_LAZY_NONE``
375+
376+
- :c:enumerator:`PyImport_LAZY_NORMAL`
377+
- :c:enumerator:`PyImport_LAZY_ALL`
378+
- :c:enumerator:`PyImport_LAZY_NONE`
378379
379380
.. versionadded:: next
380381

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ simple_stmts[asdl_stmt_seq*]:
121121
simple_stmt[stmt_ty] (memo):
122122
| assignment
123123
| &"type" type_alias
124-
| &('import' | 'from' | "lazy" ) import_stmt
124+
| &('import' | 'from' | "lazy") import_stmt
125125
| e=star_expressions { _PyAST_Expr(e, EXTRA) }
126126
| &'return' return_stmt
127127
| &'raise' raise_stmt

Lib/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def _m(self): pass
7676
# CapsuleType cannot be accessed from pure Python,
7777
# so there is no fallback definition.
7878

79+
exec("lazy import sys as _lazy_sys", _lz := {})
80+
LazyImportType = type(_lz['_lazy_sys'])
81+
del _lz
82+
7983
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
8084

8185

Objects/lazyimportobject.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
2323
if (m == NULL) {
2424
return NULL;
2525
}
26-
Py_XINCREF(builtins);
27-
m->lz_builtins = builtins;
28-
Py_INCREF(from);
29-
m->lz_from = from;
30-
Py_XINCREF(attr);
31-
m->lz_attr = attr;
26+
m->lz_builtins = Py_XNewRef(builtins);
27+
m->lz_from = Py_NewRef(from);
28+
m->lz_attr = Py_XNewRef(attr);
3229

3330
/* Capture frame information for the original import location */
3431
m->lz_code = NULL;
@@ -44,18 +41,35 @@ _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
4441
}
4542
}
4643

47-
PyObject_GC_Track(m);
44+
_PyObject_GC_TRACK(m);
4845
return (PyObject *)m;
4946
}
5047

48+
static int
49+
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
50+
{
51+
Py_VISIT(m->lz_builtins);
52+
Py_VISIT(m->lz_from);
53+
Py_VISIT(m->lz_attr);
54+
Py_VISIT(m->lz_code);
55+
return 0;
56+
}
57+
58+
static int
59+
lazy_import_clear(PyLazyImportObject *m)
60+
{
61+
Py_CLEAR(m->lz_builtins);
62+
Py_CLEAR(m->lz_from);
63+
Py_CLEAR(m->lz_attr);
64+
Py_CLEAR(m->lz_code);
65+
return 0;
66+
}
67+
5168
static void
5269
lazy_import_dealloc(PyLazyImportObject *m)
5370
{
54-
PyObject_GC_UnTrack(m);
55-
Py_XDECREF(m->lz_builtins);
56-
Py_XDECREF(m->lz_from);
57-
Py_XDECREF(m->lz_attr);
58-
Py_XDECREF(m->lz_code);
71+
_PyObject_GC_UNTRACK(m);
72+
lazy_import_clear(m);
5973
Py_TYPE(m)->tp_free((PyObject *)m);
6074
}
6175

@@ -85,26 +99,6 @@ lazy_import_repr(PyLazyImportObject *m)
8599
return res;
86100
}
87101

88-
static int
89-
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
90-
{
91-
Py_VISIT(m->lz_builtins);
92-
Py_VISIT(m->lz_from);
93-
Py_VISIT(m->lz_attr);
94-
Py_VISIT(m->lz_code);
95-
return 0;
96-
}
97-
98-
static int
99-
lazy_import_clear(PyLazyImportObject *m)
100-
{
101-
Py_CLEAR(m->lz_builtins);
102-
Py_CLEAR(m->lz_from);
103-
Py_CLEAR(m->lz_attr);
104-
Py_CLEAR(m->lz_code);
105-
return 0;
106-
}
107-
108102
static PyObject *
109103
lazy_import_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
110104
{

Objects/moduleobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ assert_def_missing_or_redundant(PyModuleObject *m)
5151
}
5252

5353

54-
5554
PyTypeObject PyModuleDef_Type = {
5655
PyVarObject_HEAD_INIT(&PyType_Type, 0)
5756
"moduledef", /* tp_name */

Programs/test_frozenmain.h

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bltinmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static PyObject *
307307
builtin___lazy_import___impl(PyObject *module, PyObject *name,
308308
PyObject *globals, PyObject *locals,
309309
PyObject *fromlist, int level)
310-
/*[clinic end generated code: output=300f1771094b9e8c input=57123e246d6c36ee]*/
310+
/*[clinic end generated code: output=300f1771094b9e8c input=9394874f340b2948]*/
311311
{
312312
PyObject *builtins;
313313
PyThreadState *tstate = PyThreadState_GET();
@@ -318,11 +318,14 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
318318
locals = globals;
319319
}
320320

321-
builtins = PyMapping_GetItemString(globals, "__builtins__");
321+
if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
322+
return NULL;
323+
}
322324
if (builtins == NULL) {
323325
PyErr_SetString(PyExc_ValueError, "unable to get builtins for lazy import");
324326
return NULL;
325-
} else if (PyModule_Check(builtins)) {
327+
}
328+
if (PyModule_Check(builtins)) {
326329
PyObject *builtins_dict = Py_XNewRef(PyModule_GetDict(builtins));
327330
if (builtins_dict == NULL) {
328331
PyErr_SetString(PyExc_AttributeError, "builtins module has no dict");

0 commit comments

Comments
 (0)