Skip to content

Commit f992ee7

Browse files
committed
Import lazy.get
1 parent 6d7c87a commit f992ee7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Lib/test/test_import/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,14 @@ def test_modules_geatattr_other(self):
26542654

26552655
self.assertFalse("test.test_import.data.lazy_imports.basic2" in sys.modules)
26562656

2657+
def test_lazy_value_get(self):
2658+
try:
2659+
import test.test_import.data.lazy_imports.lazy_get_value
2660+
except ImportError as e:
2661+
self.fail('lazy import failed')
2662+
2663+
self.assertTrue("test.test_import.data.lazy_imports.basic2" in sys.modules)
2664+
26572665

26582666
class TestSinglePhaseSnapshot(ModuleSnapshot):
26592667
"""A representation of a single-phase init module for testing.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lazy import test.test_import.data.lazy_imports.basic2 as basic2
2+
3+
def f():
4+
x = globals()
5+
return x['basic2'].get()
6+
7+
f()

Objects/lazyimportobject.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Lazy object implementation */
22

33
#include "Python.h"
4+
#include "pycore_import.h"
45
#include "pycore_lazyimportobject.h"
56

67
PyObject *
@@ -108,6 +109,18 @@ _PyLazyImport_GetName(PyObject *lazy_import)
108109
return lazy_import_name((PyLazyImportObject *)lazy_import);
109110
}
110111

112+
static PyObject *
113+
lazy_get(PyObject *self, PyObject *args)
114+
{
115+
return _PyImport_LoadLazyImportTstate(PyThreadState_GET(), self);
116+
}
117+
118+
static PyMethodDef lazy_methods[] = {
119+
{"get", lazy_get, METH_NOARGS, PyDoc_STR("gets the value that the lazy function references")},
120+
{0}
121+
};
122+
123+
111124
PyTypeObject PyLazyImport_Type = {
112125
PyVarObject_HEAD_INIT(&PyType_Type, 0)
113126
"lazy_import", /* tp_name */
@@ -137,7 +150,7 @@ PyTypeObject PyLazyImport_Type = {
137150
0, /* tp_weaklistoffset */
138151
0, /* tp_iter */
139152
0, /* tp_iternext */
140-
0, /* tp_methods */
153+
lazy_methods, /* tp_methods */
141154
0, /* tp_members */
142155
0, /* tp_getset */
143156
0, /* tp_base */

0 commit comments

Comments
 (0)