-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
Expand file tree
/
Copy pathlazyimportobject.c
More file actions
181 lines (163 loc) · 4.92 KB
/
Copy pathlazyimportobject.c
File metadata and controls
181 lines (163 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Lazy object implementation.
#include "Python.h"
#include "pycore_ceval.h"
#include "pycore_frame.h"
#include "pycore_import.h"
#include "pycore_interpframe.h"
#include "pycore_lazyimportobject.h"
#include "pycore_modsupport.h"
#define PyLazyImportObject_CAST(op) ((PyLazyImportObject *)(op))
PyObject *
_PyLazyImport_New(_PyInterpreterFrame *frame, PyObject *builtins, PyObject *name, PyObject *fromlist)
{
PyLazyImportObject *m;
if (!name || !PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError, "expected str for name");
return NULL;
}
if (fromlist == Py_None || fromlist == NULL) {
fromlist = NULL;
}
else if (!PyUnicode_Check(fromlist) && !PyTuple_Check(fromlist)) {
PyErr_SetString(PyExc_TypeError,
"lazy_import: fromlist must be None, a string, or a tuple");
return NULL;
}
m = PyObject_GC_New(PyLazyImportObject, &PyLazyImport_Type);
if (m == NULL) {
return NULL;
}
m->lz_builtins = Py_XNewRef(builtins);
m->lz_from = Py_NewRef(name);
m->lz_attr = Py_XNewRef(fromlist);
// Capture frame information for the original import location.
m->lz_code = NULL;
m->lz_instr_offset = -1;
if (frame != NULL) {
PyCodeObject *code = _PyFrame_GetCode(frame);
if (code != NULL) {
m->lz_code = (PyCodeObject *)Py_NewRef(code);
// Calculate the instruction offset from the current frame.
m->lz_instr_offset = _PyInterpreterFrame_LASTI(frame);
}
}
_PyObject_GC_TRACK(m);
return (PyObject *)m;
}
static int
lazy_import_traverse(PyObject *op, visitproc visit, void *arg)
{
PyLazyImportObject *m = PyLazyImportObject_CAST(op);
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(PyObject *op)
{
PyLazyImportObject *m = PyLazyImportObject_CAST(op);
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(PyObject *op)
{
_PyObject_GC_UNTRACK(op);
(void)lazy_import_clear(op);
Py_TYPE(op)->tp_free(op);
}
/* Specialize the error message for failed attribute lookups. */
static PyObject *
lazy_import_getattro(PyObject *op, PyObject *name)
{
PyObject *value = _PyObject_GenericGetAttrWithDict(op, name, NULL, /* suppress */1);
if (value == NULL) {
if (PyErr_Occurred()) {
// pass up non-AttributeError exception
return NULL;
}
PyObject *lz_name = _PyLazyImport_GetName(op);
if (lz_name == NULL) {
return NULL;
}
PyErr_Format(PyExc_AttributeError,
"cannot access attribute %R on unresolved lazy import %R",
name, lz_name);
Py_DECREF(lz_name);
return NULL;
}
return value;
}
static PyObject *
lazy_import_name(PyLazyImportObject *m)
{
if (m->lz_attr != NULL) {
if (PyUnicode_Check(m->lz_attr)) {
return PyUnicode_FromFormat("%U.%U", m->lz_from, m->lz_attr);
}
else {
return PyUnicode_FromFormat("%U...", m->lz_from);
}
}
return Py_NewRef(m->lz_from);
}
static PyObject *
lazy_import_repr(PyObject *op)
{
PyLazyImportObject *m = PyLazyImportObject_CAST(op);
PyObject *name = lazy_import_name(m);
if (name == NULL) {
return NULL;
}
PyObject *res = PyUnicode_FromFormat("<%T '%U'>", op, name);
Py_DECREF(name);
return res;
}
PyObject *
_PyLazyImport_GetName(PyObject *op)
{
PyLazyImportObject *lazy_import = PyLazyImportObject_CAST(op);
assert(PyLazyImport_CheckExact(lazy_import));
return lazy_import_name(lazy_import);
}
static PyObject *
lazy_import_resolve(PyObject *self, PyObject *args)
{
return _PyImport_LoadLazyImportTstate(PyThreadState_GET(), self);
}
static PyMethodDef lazy_import_methods[] = {
{
"resolve", lazy_import_resolve, METH_NOARGS,
PyDoc_STR("resolves the lazy import and returns the actual object")
},
{NULL, NULL}
};
PyDoc_STRVAR(lazy_import_doc,
"lazy_import(builtins, name, fromlist=None, /)\n"
"--\n"
"\n"
"Represents a lazy import that will be resolved on first use.\n"
"\n"
"Instances of this object accessed from the global scope will be\n"
"automatically imported based upon their name and then replaced with\n"
"the imported value.");
PyTypeObject PyLazyImport_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "lazy_import",
.tp_basicsize = sizeof(PyLazyImportObject),
.tp_dealloc = lazy_import_dealloc,
.tp_repr = lazy_import_repr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_doc = lazy_import_doc,
.tp_getattro = lazy_import_getattro,
.tp_traverse = lazy_import_traverse,
.tp_clear = lazy_import_clear,
.tp_methods = lazy_import_methods,
.tp_alloc = PyType_GenericAlloc,
.tp_free = PyObject_GC_Del,
};