Skip to content

Commit 9eef03c

Browse files
committed
Export lazy_import in imp module
1 parent 3b0d745 commit 9eef03c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Lib/importlib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
from ._bootstrap import __import__
6161

62+
from _imp import lazy_import
63+
6264

6365
def invalidate_caches():
6466
"""Call the invalidate_caches() method on all meta path finders stored in

Objects/lazyimportobject.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* Copyright (c) Meta, Inc. and its affiliates. All Rights Reserved */
2-
/* File added for Lazy Imports */
3-
41
/* Lazy object implementation */
52

63
#include "Python.h"
@@ -86,6 +83,24 @@ lazy_import_clear(PyLazyImportObject *m)
8683
return 0;
8784
}
8885

86+
static PyObject *
87+
lazy_import_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
88+
{
89+
if (PyTuple_GET_SIZE(args) != 2 && PyTuple_GET_SIZE(args) != 3) {
90+
PyErr_SetString(PyExc_ValueError, "lazy_import expected 2-3 arguments");
91+
return NULL;
92+
}
93+
94+
PyObject *builtins = PyTuple_GET_ITEM(args, 0);
95+
PyObject *from = PyTuple_GET_ITEM(args, 1);
96+
PyObject *attr = NULL;
97+
if (PyTuple_GET_SIZE(args) == 3) {
98+
attr = PyTuple_GET_ITEM(args, 2);
99+
}
100+
101+
return _PyLazyImport_New(builtins, from, attr);
102+
}
103+
89104
PyObject *
90105
_PyLazyImport_GetName(PyObject *lazy_import)
91106
{
@@ -132,6 +147,6 @@ PyTypeObject PyLazyImport_Type = {
132147
0, /* tp_dictoffset */
133148
0, /* tp_init */
134149
PyType_GenericAlloc, /* tp_alloc */
135-
PyType_GenericNew, /* tp_new */
150+
lazy_import_new, /* tp_new */
136151
PyObject_GC_Del, /* tp_free */
137152
};

Python/import.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4980,6 +4980,11 @@ imp_module_exec(PyObject *module)
49804980
return -1;
49814981
}
49824982

4983+
if (PyModule_AddObjectRef(module, "lazy_import",
4984+
(PyObject *)&PyLazyImport_Type) < 0) {
4985+
return -1;
4986+
}
4987+
49834988
return 0;
49844989
}
49854990

0 commit comments

Comments
 (0)