/src/cpython/Objects/moduleobject.c
Line | Count | Source |
1 | | /* Module object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
5 | | #include "pycore_dict.h" // _PyDict_EnablePerThreadRefcounting() |
6 | | #include "pycore_fileutils.h" // _Py_wgetcwd |
7 | | #include "pycore_import.h" // _PyImport_GetNextModuleIndex() |
8 | | #include "pycore_interp.h" // PyInterpreterState.importlib |
9 | | #include "pycore_long.h" // _PyLong_GetOne() |
10 | | #include "pycore_modsupport.h" // _PyModule_CreateInitialized() |
11 | | #include "pycore_moduleobject.h" // _PyModule_GetDef() |
12 | | #include "pycore_object.h" // _PyType_AllocNoTrack |
13 | | #include "pycore_pyerrors.h" // _PyErr_FormatFromCause() |
14 | | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
15 | | #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString() |
16 | | #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
17 | | |
18 | | #include "osdefs.h" // MAXPATHLEN |
19 | | |
20 | | |
21 | | #define _PyModule_CAST(op) \ |
22 | 1.64M | (assert(PyModule_Check(op)), _Py_CAST(PyModuleObject*, (op))) |
23 | | |
24 | | |
25 | | static PyMemberDef module_members[] = { |
26 | | {"__dict__", _Py_T_OBJECT, offsetof(PyModuleObject, md_dict), Py_READONLY}, |
27 | | {0} |
28 | | }; |
29 | | |
30 | | |
31 | | PyTypeObject PyModuleDef_Type = { |
32 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
33 | | "moduledef", /* tp_name */ |
34 | | sizeof(PyModuleDef), /* tp_basicsize */ |
35 | | 0, /* tp_itemsize */ |
36 | | }; |
37 | | |
38 | | |
39 | | int |
40 | | _PyModule_IsExtension(PyObject *obj) |
41 | 0 | { |
42 | 0 | if (!PyModule_Check(obj)) { |
43 | 0 | return 0; |
44 | 0 | } |
45 | 0 | PyModuleObject *module = (PyModuleObject*)obj; |
46 | |
|
47 | 0 | PyModuleDef *def = module->md_def; |
48 | 0 | return (def != NULL && def->m_methods != NULL); |
49 | 0 | } |
50 | | |
51 | | |
52 | | PyObject* |
53 | | PyModuleDef_Init(PyModuleDef* def) |
54 | 830 | { |
55 | | #ifdef Py_GIL_DISABLED |
56 | | // Check that this def does not come from a non-free-threading ABI. |
57 | | // |
58 | | // This is meant as a "sanity check"; users should never rely on it. |
59 | | // In particular, if we run out of ob_flags bits, or otherwise need to |
60 | | // change some of the internals, this check can go away. Still, it |
61 | | // would be nice to keep it for the free-threading transition. |
62 | | // |
63 | | // A PyModuleDef must be initialized with PyModuleDef_HEAD_INIT, |
64 | | // which (via PyObject_HEAD_INIT) sets _Py_STATICALLY_ALLOCATED_FLAG |
65 | | // and not _Py_LEGACY_ABI_CHECK_FLAG. For PyModuleDef, these flags never |
66 | | // change. |
67 | | // This means that the lower nibble of a valid PyModuleDef's ob_flags is |
68 | | // always `_10_` (in binary; `_` is don't care). |
69 | | // |
70 | | // So, a check for these bits won't reject valid PyModuleDef. |
71 | | // Rejecting incompatible extensions is slightly less important; here's |
72 | | // how that works: |
73 | | // |
74 | | // In the pre-free-threading stable ABI, PyModuleDef_HEAD_INIT is big |
75 | | // enough to overlap with free-threading ABI's ob_flags, is all zeros |
76 | | // except for the refcount field. |
77 | | // The refcount field can be: |
78 | | // - 1 (3.11 and below) |
79 | | // - UINT_MAX >> 2 (32-bit 3.12 & 3.13) |
80 | | // - UINT_MAX (64-bit 3.12 & 3.13) |
81 | | // - 7L << 28 (3.14) |
82 | | // |
83 | | // This means that the lower nibble of *any byte* in PyModuleDef_HEAD_INIT |
84 | | // is not `_10_` -- it can be: |
85 | | // - 0b0000 |
86 | | // - 0b0001 |
87 | | // - 0b0011 (from UINT_MAX >> 2) |
88 | | // - 0b0111 (from 7L << 28) |
89 | | // - 0b1111 (e.g. from UINT_MAX) |
90 | | // (The values may change at runtime as the PyModuleDef is used, but |
91 | | // PyModuleDef_Init is required before using the def as a Python object, |
92 | | // so we check at least once with the initial values. |
93 | | uint16_t flags = ((PyObject*)def)->ob_flags; |
94 | | uint16_t bits = _Py_STATICALLY_ALLOCATED_FLAG | _Py_LEGACY_ABI_CHECK_FLAG; |
95 | | if ((flags & bits) != _Py_STATICALLY_ALLOCATED_FLAG) { |
96 | | const char *message = "invalid PyModuleDef, extension possibly " |
97 | | "compiled for non-free-threaded Python"; |
98 | | // Write the error as unraisable: if the extension tries calling |
99 | | // any API, it's likely to segfault and lose the exception. |
100 | | PyErr_SetString(PyExc_SystemError, message); |
101 | | PyErr_WriteUnraisable(NULL); |
102 | | // But also raise the exception normally -- this is technically |
103 | | // a recoverable state. |
104 | | PyErr_SetString(PyExc_SystemError, message); |
105 | | return NULL; |
106 | | } |
107 | | #endif |
108 | 830 | assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY); |
109 | 830 | if (def->m_base.m_index == 0) { |
110 | 447 | Py_SET_REFCNT(def, 1); |
111 | 447 | Py_SET_TYPE(def, &PyModuleDef_Type); |
112 | 447 | def->m_base.m_index = _PyImport_GetNextModuleIndex(); |
113 | 447 | } |
114 | 830 | return (PyObject*)def; |
115 | 830 | } |
116 | | |
117 | | static int |
118 | | module_init_dict(PyModuleObject *mod, PyObject *md_dict, |
119 | | PyObject *name, PyObject *doc) |
120 | 1.48k | { |
121 | 1.48k | assert(md_dict != NULL); |
122 | 1.48k | if (doc == NULL) |
123 | 495 | doc = Py_None; |
124 | | |
125 | 1.48k | if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0) |
126 | 0 | return -1; |
127 | 1.48k | if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0) |
128 | 0 | return -1; |
129 | 1.48k | if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0) |
130 | 0 | return -1; |
131 | 1.48k | if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0) |
132 | 0 | return -1; |
133 | 1.48k | if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0) |
134 | 0 | return -1; |
135 | 1.48k | if (PyUnicode_CheckExact(name)) { |
136 | 1.48k | Py_XSETREF(mod->md_name, Py_NewRef(name)); |
137 | 1.48k | } |
138 | | |
139 | 1.48k | return 0; |
140 | 1.48k | } |
141 | | |
142 | | static PyModuleObject * |
143 | | new_module_notrack(PyTypeObject *mt) |
144 | 1.48k | { |
145 | 1.48k | PyModuleObject *m; |
146 | 1.48k | m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0); |
147 | 1.48k | if (m == NULL) |
148 | 0 | return NULL; |
149 | 1.48k | m->md_def = NULL; |
150 | 1.48k | m->md_state = NULL; |
151 | 1.48k | m->md_weaklist = NULL; |
152 | 1.48k | m->md_name = NULL; |
153 | 1.48k | m->md_dict = PyDict_New(); |
154 | 1.48k | if (m->md_dict == NULL) { |
155 | 0 | Py_DECREF(m); |
156 | 0 | return NULL; |
157 | 0 | } |
158 | 1.48k | return m; |
159 | 1.48k | } |
160 | | |
161 | | static void |
162 | | track_module(PyModuleObject *m) |
163 | 1.48k | { |
164 | 1.48k | _PyDict_EnablePerThreadRefcounting(m->md_dict); |
165 | 1.48k | _PyObject_SetDeferredRefcount((PyObject *)m); |
166 | 1.48k | PyObject_GC_Track(m); |
167 | 1.48k | } |
168 | | |
169 | | static PyObject * |
170 | | new_module(PyTypeObject *mt, PyObject *args, PyObject *kws) |
171 | 994 | { |
172 | 994 | PyModuleObject *m = new_module_notrack(mt); |
173 | 994 | if (m != NULL) { |
174 | 994 | track_module(m); |
175 | 994 | } |
176 | 994 | return (PyObject *)m; |
177 | 994 | } |
178 | | |
179 | | PyObject * |
180 | | PyModule_NewObject(PyObject *name) |
181 | 495 | { |
182 | 495 | PyModuleObject *m = new_module_notrack(&PyModule_Type); |
183 | 495 | if (m == NULL) |
184 | 0 | return NULL; |
185 | 495 | if (module_init_dict(m, m->md_dict, name, NULL) != 0) |
186 | 0 | goto fail; |
187 | 495 | track_module(m); |
188 | 495 | return (PyObject *)m; |
189 | | |
190 | 0 | fail: |
191 | 0 | Py_DECREF(m); |
192 | 0 | return NULL; |
193 | 495 | } |
194 | | |
195 | | PyObject * |
196 | | PyModule_New(const char *name) |
197 | 80 | { |
198 | 80 | PyObject *nameobj, *module; |
199 | 80 | nameobj = PyUnicode_FromString(name); |
200 | 80 | if (nameobj == NULL) |
201 | 0 | return NULL; |
202 | 80 | module = PyModule_NewObject(nameobj); |
203 | 80 | Py_DECREF(nameobj); |
204 | 80 | return module; |
205 | 80 | } |
206 | | |
207 | | /* Check API/ABI version |
208 | | * Issues a warning on mismatch, which is usually not fatal. |
209 | | * Returns 0 if an exception is raised. |
210 | | */ |
211 | | static int |
212 | | check_api_version(const char *name, int module_api_version) |
213 | 447 | { |
214 | 447 | if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { |
215 | 0 | int err; |
216 | 0 | err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
217 | 0 | "Python C API version mismatch for module %.100s: " |
218 | 0 | "This Python has API version %d, module %.100s has version %d.", |
219 | 0 | name, |
220 | 0 | PYTHON_API_VERSION, name, module_api_version); |
221 | 0 | if (err) |
222 | 0 | return 0; |
223 | 0 | } |
224 | 447 | return 1; |
225 | 447 | } |
226 | | |
227 | | static int |
228 | | _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions) |
229 | 423 | { |
230 | 423 | PyObject *func; |
231 | 423 | PyMethodDef *fdef; |
232 | | |
233 | 9.36k | for (fdef = functions; fdef->ml_name != NULL; fdef++) { |
234 | 8.94k | if ((fdef->ml_flags & METH_CLASS) || |
235 | 8.94k | (fdef->ml_flags & METH_STATIC)) { |
236 | 0 | PyErr_SetString(PyExc_ValueError, |
237 | 0 | "module functions cannot set" |
238 | 0 | " METH_CLASS or METH_STATIC"); |
239 | 0 | return -1; |
240 | 0 | } |
241 | 8.94k | func = PyCFunction_NewEx(fdef, (PyObject*)module, name); |
242 | 8.94k | if (func == NULL) { |
243 | 0 | return -1; |
244 | 0 | } |
245 | 8.94k | _PyObject_SetDeferredRefcount(func); |
246 | 8.94k | if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) { |
247 | 0 | Py_DECREF(func); |
248 | 0 | return -1; |
249 | 0 | } |
250 | 8.94k | Py_DECREF(func); |
251 | 8.94k | } |
252 | | |
253 | 423 | return 0; |
254 | 423 | } |
255 | | |
256 | | PyObject * |
257 | | PyModule_Create2(PyModuleDef* module, int module_api_version) |
258 | 0 | { |
259 | 0 | if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) { |
260 | 0 | PyErr_SetString(PyExc_SystemError, |
261 | 0 | "Python import machinery not initialized"); |
262 | 0 | return NULL; |
263 | 0 | } |
264 | 0 | return _PyModule_CreateInitialized(module, module_api_version); |
265 | 0 | } |
266 | | |
267 | | PyObject * |
268 | | _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version) |
269 | 64 | { |
270 | 64 | const char* name; |
271 | 64 | PyModuleObject *m; |
272 | | |
273 | 64 | if (!PyModuleDef_Init(module)) |
274 | 0 | return NULL; |
275 | 64 | name = module->m_name; |
276 | 64 | if (!check_api_version(name, module_api_version)) { |
277 | 0 | return NULL; |
278 | 0 | } |
279 | 64 | if (module->m_slots) { |
280 | 0 | PyErr_Format( |
281 | 0 | PyExc_SystemError, |
282 | 0 | "module %s: PyModule_Create is incompatible with m_slots", name); |
283 | 0 | return NULL; |
284 | 0 | } |
285 | 64 | name = _PyImport_ResolveNameWithPackageContext(name); |
286 | | |
287 | 64 | m = (PyModuleObject*)PyModule_New(name); |
288 | 64 | if (m == NULL) |
289 | 0 | return NULL; |
290 | | |
291 | 64 | if (module->m_size > 0) { |
292 | 0 | m->md_state = PyMem_Malloc(module->m_size); |
293 | 0 | if (!m->md_state) { |
294 | 0 | PyErr_NoMemory(); |
295 | 0 | Py_DECREF(m); |
296 | 0 | return NULL; |
297 | 0 | } |
298 | 0 | memset(m->md_state, 0, module->m_size); |
299 | 0 | } |
300 | | |
301 | 64 | if (module->m_methods != NULL) { |
302 | 64 | if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) { |
303 | 0 | Py_DECREF(m); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | 64 | } |
307 | 64 | if (module->m_doc != NULL) { |
308 | 48 | if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) { |
309 | 0 | Py_DECREF(m); |
310 | 0 | return NULL; |
311 | 0 | } |
312 | 48 | } |
313 | 64 | m->md_def = module; |
314 | | #ifdef Py_GIL_DISABLED |
315 | | m->md_gil = Py_MOD_GIL_USED; |
316 | | #endif |
317 | 64 | return (PyObject*)m; |
318 | 64 | } |
319 | | |
320 | | PyObject * |
321 | | PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version) |
322 | 383 | { |
323 | 383 | PyModuleDef_Slot* cur_slot; |
324 | 383 | PyObject *(*create)(PyObject *, PyModuleDef*) = NULL; |
325 | 383 | PyObject *nameobj; |
326 | 383 | PyObject *m = NULL; |
327 | 383 | int has_multiple_interpreters_slot = 0; |
328 | 383 | void *multiple_interpreters = (void *)0; |
329 | 383 | int has_gil_slot = 0; |
330 | 383 | void *gil_slot = Py_MOD_GIL_USED; |
331 | 383 | int has_execution_slots = 0; |
332 | 383 | const char *name; |
333 | 383 | int ret; |
334 | 383 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
335 | | |
336 | 383 | PyModuleDef_Init(def); |
337 | | |
338 | 383 | nameobj = PyObject_GetAttrString(spec, "name"); |
339 | 383 | if (nameobj == NULL) { |
340 | 0 | return NULL; |
341 | 0 | } |
342 | 383 | name = PyUnicode_AsUTF8(nameobj); |
343 | 383 | if (name == NULL) { |
344 | 0 | goto error; |
345 | 0 | } |
346 | | |
347 | 383 | if (!check_api_version(name, module_api_version)) { |
348 | 0 | goto error; |
349 | 0 | } |
350 | | |
351 | 383 | if (def->m_size < 0) { |
352 | 0 | PyErr_Format( |
353 | 0 | PyExc_SystemError, |
354 | 0 | "module %s: m_size may not be negative for multi-phase initialization", |
355 | 0 | name); |
356 | 0 | goto error; |
357 | 0 | } |
358 | | |
359 | 1.55k | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
360 | 1.17k | switch (cur_slot->slot) { |
361 | 0 | case Py_mod_create: |
362 | 0 | if (create) { |
363 | 0 | PyErr_Format( |
364 | 0 | PyExc_SystemError, |
365 | 0 | "module %s has multiple create slots", |
366 | 0 | name); |
367 | 0 | goto error; |
368 | 0 | } |
369 | 0 | create = cur_slot->value; |
370 | 0 | break; |
371 | 403 | case Py_mod_exec: |
372 | 403 | has_execution_slots = 1; |
373 | 403 | break; |
374 | 383 | case Py_mod_multiple_interpreters: |
375 | 383 | if (has_multiple_interpreters_slot) { |
376 | 0 | PyErr_Format( |
377 | 0 | PyExc_SystemError, |
378 | 0 | "module %s has more than one 'multiple interpreters' slots", |
379 | 0 | name); |
380 | 0 | goto error; |
381 | 0 | } |
382 | 383 | multiple_interpreters = cur_slot->value; |
383 | 383 | has_multiple_interpreters_slot = 1; |
384 | 383 | break; |
385 | 383 | case Py_mod_gil: |
386 | 383 | if (has_gil_slot) { |
387 | 0 | PyErr_Format( |
388 | 0 | PyExc_SystemError, |
389 | 0 | "module %s has more than one 'gil' slot", |
390 | 0 | name); |
391 | 0 | goto error; |
392 | 0 | } |
393 | 383 | gil_slot = cur_slot->value; |
394 | 383 | has_gil_slot = 1; |
395 | 383 | break; |
396 | 6 | case Py_mod_abi: |
397 | 6 | if (PyABIInfo_Check((PyABIInfo *)cur_slot->value, name) < 0) { |
398 | 0 | goto error; |
399 | 0 | } |
400 | 6 | break; |
401 | 6 | default: |
402 | 0 | assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT); |
403 | 0 | PyErr_Format( |
404 | 0 | PyExc_SystemError, |
405 | 0 | "module %s uses unknown slot ID %i", |
406 | 0 | name, cur_slot->slot); |
407 | 0 | goto error; |
408 | 1.17k | } |
409 | 1.17k | } |
410 | | |
411 | | /* By default, multi-phase init modules are expected |
412 | | to work under multiple interpreters. */ |
413 | 383 | if (!has_multiple_interpreters_slot) { |
414 | 0 | multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED; |
415 | 0 | } |
416 | 383 | if (multiple_interpreters == Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED) { |
417 | 0 | if (!_Py_IsMainInterpreter(interp) |
418 | 0 | && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0) |
419 | 0 | { |
420 | 0 | goto error; |
421 | 0 | } |
422 | 0 | } |
423 | 383 | else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED |
424 | 0 | && interp->ceval.own_gil |
425 | 0 | && !_Py_IsMainInterpreter(interp) |
426 | 0 | && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0) |
427 | 0 | { |
428 | 0 | goto error; |
429 | 0 | } |
430 | | |
431 | 383 | if (create) { |
432 | 0 | m = create(spec, def); |
433 | 0 | if (m == NULL) { |
434 | 0 | if (!PyErr_Occurred()) { |
435 | 0 | PyErr_Format( |
436 | 0 | PyExc_SystemError, |
437 | 0 | "creation of module %s failed without setting an exception", |
438 | 0 | name); |
439 | 0 | } |
440 | 0 | goto error; |
441 | 0 | } else { |
442 | 0 | if (PyErr_Occurred()) { |
443 | 0 | _PyErr_FormatFromCause( |
444 | 0 | PyExc_SystemError, |
445 | 0 | "creation of module %s raised unreported exception", |
446 | 0 | name); |
447 | 0 | goto error; |
448 | 0 | } |
449 | 0 | } |
450 | 383 | } else { |
451 | 383 | m = PyModule_NewObject(nameobj); |
452 | 383 | if (m == NULL) { |
453 | 0 | goto error; |
454 | 0 | } |
455 | 383 | } |
456 | | |
457 | 383 | if (PyModule_Check(m)) { |
458 | 383 | ((PyModuleObject*)m)->md_state = NULL; |
459 | 383 | ((PyModuleObject*)m)->md_def = def; |
460 | | #ifdef Py_GIL_DISABLED |
461 | | ((PyModuleObject*)m)->md_gil = gil_slot; |
462 | | #else |
463 | 383 | (void)gil_slot; |
464 | 383 | #endif |
465 | 383 | } else { |
466 | 0 | if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) { |
467 | 0 | PyErr_Format( |
468 | 0 | PyExc_SystemError, |
469 | 0 | "module %s is not a module object, but requests module state", |
470 | 0 | name); |
471 | 0 | goto error; |
472 | 0 | } |
473 | 0 | if (has_execution_slots) { |
474 | 0 | PyErr_Format( |
475 | 0 | PyExc_SystemError, |
476 | 0 | "module %s specifies execution slots, but did not create " |
477 | 0 | "a ModuleType instance", |
478 | 0 | name); |
479 | 0 | goto error; |
480 | 0 | } |
481 | 0 | } |
482 | | |
483 | 383 | if (def->m_methods != NULL) { |
484 | 359 | ret = _add_methods_to_object(m, nameobj, def->m_methods); |
485 | 359 | if (ret != 0) { |
486 | 0 | goto error; |
487 | 0 | } |
488 | 359 | } |
489 | | |
490 | 383 | if (def->m_doc != NULL) { |
491 | 327 | ret = PyModule_SetDocString(m, def->m_doc); |
492 | 327 | if (ret != 0) { |
493 | 0 | goto error; |
494 | 0 | } |
495 | 327 | } |
496 | | |
497 | 383 | Py_DECREF(nameobj); |
498 | 383 | return m; |
499 | | |
500 | 0 | error: |
501 | 0 | Py_DECREF(nameobj); |
502 | 0 | Py_XDECREF(m); |
503 | 0 | return NULL; |
504 | 383 | } |
505 | | |
506 | | #ifdef Py_GIL_DISABLED |
507 | | int |
508 | | PyUnstable_Module_SetGIL(PyObject *module, void *gil) |
509 | | { |
510 | | if (!PyModule_Check(module)) { |
511 | | PyErr_BadInternalCall(); |
512 | | return -1; |
513 | | } |
514 | | ((PyModuleObject *)module)->md_gil = gil; |
515 | | return 0; |
516 | | } |
517 | | #endif |
518 | | |
519 | | int |
520 | | PyModule_ExecDef(PyObject *module, PyModuleDef *def) |
521 | 383 | { |
522 | 383 | PyModuleDef_Slot *cur_slot; |
523 | 383 | const char *name; |
524 | 383 | int ret; |
525 | | |
526 | 383 | name = PyModule_GetName(module); |
527 | 383 | if (name == NULL) { |
528 | 0 | return -1; |
529 | 0 | } |
530 | | |
531 | 383 | if (def->m_size >= 0) { |
532 | 383 | PyModuleObject *md = (PyModuleObject*)module; |
533 | 383 | if (md->md_state == NULL) { |
534 | | /* Always set a state pointer; this serves as a marker to skip |
535 | | * multiple initialization (importlib.reload() is no-op) */ |
536 | 383 | md->md_state = PyMem_Malloc(def->m_size); |
537 | 383 | if (!md->md_state) { |
538 | 0 | PyErr_NoMemory(); |
539 | 0 | return -1; |
540 | 0 | } |
541 | 383 | memset(md->md_state, 0, def->m_size); |
542 | 383 | } |
543 | 383 | } |
544 | | |
545 | 383 | if (def->m_slots == NULL) { |
546 | 0 | return 0; |
547 | 0 | } |
548 | | |
549 | 1.55k | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
550 | 1.17k | switch (cur_slot->slot) { |
551 | 0 | case Py_mod_create: |
552 | | /* handled in PyModule_FromDefAndSpec2 */ |
553 | 0 | break; |
554 | 403 | case Py_mod_exec: |
555 | 403 | ret = ((int (*)(PyObject *))cur_slot->value)(module); |
556 | 403 | if (ret != 0) { |
557 | 0 | if (!PyErr_Occurred()) { |
558 | 0 | PyErr_Format( |
559 | 0 | PyExc_SystemError, |
560 | 0 | "execution of module %s failed without setting an exception", |
561 | 0 | name); |
562 | 0 | } |
563 | 0 | return -1; |
564 | 0 | } |
565 | 403 | if (PyErr_Occurred()) { |
566 | 0 | _PyErr_FormatFromCause( |
567 | 0 | PyExc_SystemError, |
568 | 0 | "execution of module %s raised unreported exception", |
569 | 0 | name); |
570 | 0 | return -1; |
571 | 0 | } |
572 | 403 | break; |
573 | 403 | case Py_mod_multiple_interpreters: |
574 | 766 | case Py_mod_gil: |
575 | 772 | case Py_mod_abi: |
576 | | /* handled in PyModule_FromDefAndSpec2 */ |
577 | 772 | break; |
578 | 0 | default: |
579 | 0 | PyErr_Format( |
580 | 0 | PyExc_SystemError, |
581 | 0 | "module %s initialized with unknown slot %i", |
582 | 0 | name, cur_slot->slot); |
583 | 0 | return -1; |
584 | 1.17k | } |
585 | 1.17k | } |
586 | 383 | return 0; |
587 | 383 | } |
588 | | |
589 | | int |
590 | | PyModule_AddFunctions(PyObject *m, PyMethodDef *functions) |
591 | 64 | { |
592 | 64 | int res; |
593 | 64 | PyObject *name = PyModule_GetNameObject(m); |
594 | 64 | if (name == NULL) { |
595 | 0 | return -1; |
596 | 0 | } |
597 | | |
598 | 64 | res = _add_methods_to_object(m, name, functions); |
599 | 64 | Py_DECREF(name); |
600 | 64 | return res; |
601 | 64 | } |
602 | | |
603 | | int |
604 | | PyModule_SetDocString(PyObject *m, const char *doc) |
605 | 375 | { |
606 | 375 | PyObject *v; |
607 | | |
608 | 375 | v = PyUnicode_FromString(doc); |
609 | 375 | if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) { |
610 | 0 | Py_XDECREF(v); |
611 | 0 | return -1; |
612 | 0 | } |
613 | 375 | Py_DECREF(v); |
614 | 375 | return 0; |
615 | 375 | } |
616 | | |
617 | | PyObject * |
618 | | PyModule_GetDict(PyObject *m) |
619 | 9.16k | { |
620 | 9.16k | if (!PyModule_Check(m)) { |
621 | 0 | PyErr_BadInternalCall(); |
622 | 0 | return NULL; |
623 | 0 | } |
624 | 9.16k | return _PyModule_GetDict(m); // borrowed reference |
625 | 9.16k | } |
626 | | |
627 | | PyObject* |
628 | | PyModule_GetNameObject(PyObject *mod) |
629 | 447 | { |
630 | 447 | if (!PyModule_Check(mod)) { |
631 | 0 | PyErr_BadArgument(); |
632 | 0 | return NULL; |
633 | 0 | } |
634 | 447 | PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference |
635 | 447 | if (dict == NULL || !PyDict_Check(dict)) { |
636 | 0 | goto error; |
637 | 0 | } |
638 | 447 | PyObject *name; |
639 | 447 | if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) { |
640 | | // error or not found |
641 | 0 | goto error; |
642 | 0 | } |
643 | 447 | if (!PyUnicode_Check(name)) { |
644 | 0 | Py_DECREF(name); |
645 | 0 | goto error; |
646 | 0 | } |
647 | 447 | return name; |
648 | | |
649 | 0 | error: |
650 | 0 | if (!PyErr_Occurred()) { |
651 | 0 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
652 | 0 | } |
653 | 0 | return NULL; |
654 | 447 | } |
655 | | |
656 | | const char * |
657 | | PyModule_GetName(PyObject *m) |
658 | 383 | { |
659 | 383 | PyObject *name = PyModule_GetNameObject(m); |
660 | 383 | if (name == NULL) { |
661 | 0 | return NULL; |
662 | 0 | } |
663 | 383 | assert(Py_REFCNT(name) >= 2); |
664 | 383 | Py_DECREF(name); /* module dict has still a reference */ |
665 | 383 | return PyUnicode_AsUTF8(name); |
666 | 383 | } |
667 | | |
668 | | PyObject* |
669 | | _PyModule_GetFilenameObject(PyObject *mod) |
670 | 52 | { |
671 | | // We return None to indicate "not found" or "bogus". |
672 | 52 | if (!PyModule_Check(mod)) { |
673 | 0 | PyErr_BadArgument(); |
674 | 0 | return NULL; |
675 | 0 | } |
676 | 52 | PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference |
677 | 52 | if (dict == NULL) { |
678 | | // The module has been tampered with. |
679 | 0 | Py_RETURN_NONE; |
680 | 0 | } |
681 | 52 | PyObject *fileobj; |
682 | 52 | int res = PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj); |
683 | 52 | if (res < 0) { |
684 | 0 | return NULL; |
685 | 0 | } |
686 | 52 | if (res == 0) { |
687 | | // __file__ isn't set. There are several reasons why this might |
688 | | // be so, most of them valid reasons. If it's the __main__ |
689 | | // module then we're running the REPL or with -c. Otherwise |
690 | | // it's a namespace package or other module with a loader that |
691 | | // isn't disk-based. It could also be that a user created |
692 | | // a module manually but without manually setting __file__. |
693 | 12 | Py_RETURN_NONE; |
694 | 12 | } |
695 | 40 | if (!PyUnicode_Check(fileobj)) { |
696 | 0 | Py_DECREF(fileobj); |
697 | 0 | Py_RETURN_NONE; |
698 | 0 | } |
699 | 40 | return fileobj; |
700 | 40 | } |
701 | | |
702 | | PyObject* |
703 | | PyModule_GetFilenameObject(PyObject *mod) |
704 | 52 | { |
705 | 52 | PyObject *fileobj = _PyModule_GetFilenameObject(mod); |
706 | 52 | if (fileobj == NULL) { |
707 | 0 | return NULL; |
708 | 0 | } |
709 | 52 | if (fileobj == Py_None) { |
710 | 12 | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
711 | 12 | return NULL; |
712 | 12 | } |
713 | 40 | return fileobj; |
714 | 52 | } |
715 | | |
716 | | const char * |
717 | | PyModule_GetFilename(PyObject *m) |
718 | 0 | { |
719 | 0 | PyObject *fileobj; |
720 | 0 | const char *utf8; |
721 | 0 | fileobj = PyModule_GetFilenameObject(m); |
722 | 0 | if (fileobj == NULL) |
723 | 0 | return NULL; |
724 | 0 | utf8 = PyUnicode_AsUTF8(fileobj); |
725 | 0 | Py_DECREF(fileobj); /* module dict has still a reference */ |
726 | 0 | return utf8; |
727 | 0 | } |
728 | | |
729 | | Py_ssize_t |
730 | | _PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen) |
731 | 0 | { |
732 | | // We "return" an empty string for an invalid module |
733 | | // and for a missing, empty, or invalid filename. |
734 | 0 | assert(maxlen >= 0); |
735 | 0 | Py_ssize_t size = -1; |
736 | 0 | PyObject *filenameobj = _PyModule_GetFilenameObject(mod); |
737 | 0 | if (filenameobj == NULL) { |
738 | 0 | return -1; |
739 | 0 | } |
740 | 0 | if (filenameobj == Py_None) { |
741 | | // It is missing or invalid. |
742 | 0 | buffer[0] = '\0'; |
743 | 0 | size = 0; |
744 | 0 | } |
745 | 0 | else { |
746 | 0 | const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size); |
747 | 0 | assert(size >= 0); |
748 | 0 | if (size > maxlen) { |
749 | 0 | size = -1; |
750 | 0 | PyErr_SetString(PyExc_ValueError, "__file__ too long"); |
751 | 0 | } |
752 | 0 | else { |
753 | 0 | (void)strcpy(buffer, filename); |
754 | 0 | } |
755 | 0 | } |
756 | 0 | Py_DECREF(filenameobj); |
757 | 0 | return size; |
758 | 0 | } |
759 | | |
760 | | PyModuleDef* |
761 | | PyModule_GetDef(PyObject* m) |
762 | 417 | { |
763 | 417 | if (!PyModule_Check(m)) { |
764 | 0 | PyErr_BadArgument(); |
765 | 0 | return NULL; |
766 | 0 | } |
767 | 417 | return _PyModule_GetDef(m); |
768 | 417 | } |
769 | | |
770 | | void* |
771 | | PyModule_GetState(PyObject* m) |
772 | 21.9M | { |
773 | 21.9M | if (!PyModule_Check(m)) { |
774 | 0 | PyErr_BadArgument(); |
775 | 0 | return NULL; |
776 | 0 | } |
777 | 21.9M | return _PyModule_GetState(m); |
778 | 21.9M | } |
779 | | |
780 | | void |
781 | | _PyModule_Clear(PyObject *m) |
782 | 0 | { |
783 | 0 | PyObject *d = ((PyModuleObject *)m)->md_dict; |
784 | 0 | if (d != NULL) |
785 | 0 | _PyModule_ClearDict(d); |
786 | 0 | } |
787 | | |
788 | | void |
789 | | _PyModule_ClearDict(PyObject *d) |
790 | 0 | { |
791 | | /* To make the execution order of destructors for global |
792 | | objects a bit more predictable, we first zap all objects |
793 | | whose name starts with a single underscore, before we clear |
794 | | the entire dictionary. We zap them by replacing them with |
795 | | None, rather than deleting them from the dictionary, to |
796 | | avoid rehashing the dictionary (to some extent). */ |
797 | |
|
798 | 0 | Py_ssize_t pos; |
799 | 0 | PyObject *key, *value; |
800 | |
|
801 | 0 | int verbose = _Py_GetConfig()->verbose; |
802 | | |
803 | | /* First, clear only names starting with a single underscore */ |
804 | 0 | pos = 0; |
805 | 0 | while (PyDict_Next(d, &pos, &key, &value)) { |
806 | 0 | if (value != Py_None && PyUnicode_Check(key)) { |
807 | 0 | if (PyUnicode_READ_CHAR(key, 0) == '_' && |
808 | 0 | PyUnicode_READ_CHAR(key, 1) != '_') { |
809 | 0 | if (verbose > 1) { |
810 | 0 | const char *s = PyUnicode_AsUTF8(key); |
811 | 0 | if (s != NULL) |
812 | 0 | PySys_WriteStderr("# clear[1] %s\n", s); |
813 | 0 | else |
814 | 0 | PyErr_Clear(); |
815 | 0 | } |
816 | 0 | if (PyDict_SetItem(d, key, Py_None) != 0) { |
817 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
818 | 0 | "clearing module dict"); |
819 | 0 | } |
820 | 0 | } |
821 | 0 | } |
822 | 0 | } |
823 | | |
824 | | /* Next, clear all names except for __builtins__ */ |
825 | 0 | pos = 0; |
826 | 0 | while (PyDict_Next(d, &pos, &key, &value)) { |
827 | 0 | if (value != Py_None && PyUnicode_Check(key)) { |
828 | 0 | if (PyUnicode_READ_CHAR(key, 0) != '_' || |
829 | 0 | !_PyUnicode_EqualToASCIIString(key, "__builtins__")) |
830 | 0 | { |
831 | 0 | if (verbose > 1) { |
832 | 0 | const char *s = PyUnicode_AsUTF8(key); |
833 | 0 | if (s != NULL) |
834 | 0 | PySys_WriteStderr("# clear[2] %s\n", s); |
835 | 0 | else |
836 | 0 | PyErr_Clear(); |
837 | 0 | } |
838 | 0 | if (PyDict_SetItem(d, key, Py_None) != 0) { |
839 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
840 | 0 | "clearing module dict"); |
841 | 0 | } |
842 | 0 | } |
843 | 0 | } |
844 | 0 | } |
845 | | |
846 | | /* Note: we leave __builtins__ in place, so that destructors |
847 | | of non-global objects defined in this module can still use |
848 | | builtins, in particularly 'None'. */ |
849 | |
|
850 | 0 | } |
851 | | |
852 | | /*[clinic input] |
853 | | class module "PyModuleObject *" "&PyModule_Type" |
854 | | [clinic start generated code]*/ |
855 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/ |
856 | | |
857 | | #include "clinic/moduleobject.c.h" |
858 | | |
859 | | /* Methods */ |
860 | | |
861 | | /*[clinic input] |
862 | | module.__init__ |
863 | | name: unicode |
864 | | doc: object = None |
865 | | |
866 | | Create a module object. |
867 | | |
868 | | The name must be a string; the optional doc argument can have any type. |
869 | | [clinic start generated code]*/ |
870 | | |
871 | | static int |
872 | | module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc) |
873 | | /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/ |
874 | 994 | { |
875 | 994 | return module_init_dict(self, self->md_dict, name, doc); |
876 | 994 | } |
877 | | |
878 | | static void |
879 | | module_dealloc(PyObject *self) |
880 | 44 | { |
881 | 44 | PyModuleObject *m = _PyModule_CAST(self); |
882 | | |
883 | 44 | PyObject_GC_UnTrack(m); |
884 | | |
885 | 44 | int verbose = _Py_GetConfig()->verbose; |
886 | 44 | if (verbose && m->md_name) { |
887 | 0 | PySys_FormatStderr("# destroy %U\n", m->md_name); |
888 | 0 | } |
889 | 44 | FT_CLEAR_WEAKREFS(self, m->md_weaklist); |
890 | | |
891 | | /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */ |
892 | 44 | if (m->md_def && m->md_def->m_free |
893 | 0 | && (m->md_def->m_size <= 0 || m->md_state != NULL)) |
894 | 0 | { |
895 | 0 | m->md_def->m_free(m); |
896 | 0 | } |
897 | | |
898 | 44 | Py_XDECREF(m->md_dict); |
899 | 44 | Py_XDECREF(m->md_name); |
900 | 44 | if (m->md_state != NULL) |
901 | 0 | PyMem_Free(m->md_state); |
902 | 44 | Py_TYPE(m)->tp_free((PyObject *)m); |
903 | 44 | } |
904 | | |
905 | | static PyObject * |
906 | | module_repr(PyObject *self) |
907 | 0 | { |
908 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
909 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
910 | 0 | return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m); |
911 | 0 | } |
912 | | |
913 | | /* Check if the "_initializing" attribute of the module spec is set to true. |
914 | | */ |
915 | | int |
916 | | _PyModuleSpec_IsInitializing(PyObject *spec) |
917 | 44.5k | { |
918 | 44.5k | if (spec == NULL) { |
919 | 0 | return 0; |
920 | 0 | } |
921 | 44.5k | PyObject *value; |
922 | 44.5k | int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value); |
923 | 44.5k | if (rc > 0) { |
924 | 43.8k | rc = PyObject_IsTrue(value); |
925 | 43.8k | Py_DECREF(value); |
926 | 43.8k | } |
927 | 44.5k | return rc; |
928 | 44.5k | } |
929 | | |
930 | | /* Check if the submodule name is in the "_uninitialized_submodules" attribute |
931 | | of the module spec. |
932 | | */ |
933 | | int |
934 | | _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name) |
935 | 6.03k | { |
936 | 6.03k | if (spec == NULL) { |
937 | 0 | return 0; |
938 | 0 | } |
939 | | |
940 | 6.03k | PyObject *value; |
941 | 6.03k | int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_uninitialized_submodules), &value); |
942 | 6.03k | if (rc > 0) { |
943 | 5.84k | rc = PySequence_Contains(value, name); |
944 | 5.84k | Py_DECREF(value); |
945 | 5.84k | } |
946 | 6.03k | return rc; |
947 | 6.03k | } |
948 | | |
949 | | int |
950 | | _PyModuleSpec_GetFileOrigin(PyObject *spec, PyObject **p_origin) |
951 | 6.08k | { |
952 | 6.08k | PyObject *has_location = NULL; |
953 | 6.08k | int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(has_location), &has_location); |
954 | 6.08k | if (rc <= 0) { |
955 | 192 | return rc; |
956 | 192 | } |
957 | | // If origin is not a location, or doesn't exist, or is not a str, we could consider falling |
958 | | // back to module.__file__. But the cases in which module.__file__ is not __spec__.origin |
959 | | // are cases in which we probably shouldn't be guessing. |
960 | 5.89k | rc = PyObject_IsTrue(has_location); |
961 | 5.89k | Py_DECREF(has_location); |
962 | 5.89k | if (rc <= 0) { |
963 | 68 | return rc; |
964 | 68 | } |
965 | | // has_location is true, so origin is a location |
966 | 5.82k | PyObject *origin = NULL; |
967 | 5.82k | rc = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin); |
968 | 5.82k | if (rc <= 0) { |
969 | 0 | return rc; |
970 | 0 | } |
971 | 5.82k | assert(origin != NULL); |
972 | 5.82k | if (!PyUnicode_Check(origin)) { |
973 | 0 | Py_DECREF(origin); |
974 | 0 | return 0; |
975 | 0 | } |
976 | 5.82k | *p_origin = origin; |
977 | 5.82k | return 1; |
978 | 5.82k | } |
979 | | |
980 | | int |
981 | | _PyModule_IsPossiblyShadowing(PyObject *origin) |
982 | 6.08k | { |
983 | | // origin must be a unicode subtype |
984 | | // Returns 1 if the module at origin could be shadowing a module of the |
985 | | // same name later in the module search path. The condition we check is basically: |
986 | | // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py")) |
987 | | // return not sys.flags.safe_path and root == (sys.path[0] or os.getcwd()) |
988 | | // Returns 0 otherwise (or if we aren't sure) |
989 | | // Returns -1 if an error occurred that should be propagated |
990 | 6.08k | if (origin == NULL) { |
991 | 260 | return 0; |
992 | 260 | } |
993 | | |
994 | | // not sys.flags.safe_path |
995 | 5.82k | const PyConfig *config = _Py_GetConfig(); |
996 | 5.82k | if (config->safe_path) { |
997 | 0 | return 0; |
998 | 0 | } |
999 | | |
1000 | | // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py")) |
1001 | 5.82k | wchar_t root[MAXPATHLEN + 1]; |
1002 | 5.82k | Py_ssize_t size = PyUnicode_AsWideChar(origin, root, MAXPATHLEN); |
1003 | 5.82k | if (size < 0) { |
1004 | 0 | return -1; |
1005 | 0 | } |
1006 | 5.82k | assert(size <= MAXPATHLEN); |
1007 | 5.82k | root[size] = L'\0'; |
1008 | | |
1009 | 5.82k | wchar_t *sep = wcsrchr(root, SEP); |
1010 | 5.82k | if (sep == NULL) { |
1011 | 0 | return 0; |
1012 | 0 | } |
1013 | | // If it's a package then we need to look one directory further up |
1014 | 5.82k | if (wcscmp(sep + 1, L"__init__.py") == 0) { |
1015 | 0 | *sep = L'\0'; |
1016 | 0 | sep = wcsrchr(root, SEP); |
1017 | 0 | if (sep == NULL) { |
1018 | 0 | return 0; |
1019 | 0 | } |
1020 | 0 | } |
1021 | 5.82k | *sep = L'\0'; |
1022 | | |
1023 | | // sys.path[0] or os.getcwd() |
1024 | 5.82k | wchar_t *sys_path_0 = config->sys_path_0; |
1025 | 5.82k | if (!sys_path_0) { |
1026 | 5.82k | return 0; |
1027 | 5.82k | } |
1028 | | |
1029 | 0 | wchar_t sys_path_0_buf[MAXPATHLEN]; |
1030 | 0 | if (sys_path_0[0] == L'\0') { |
1031 | | // if sys.path[0] == "", treat it as if it were the current directory |
1032 | 0 | if (!_Py_wgetcwd(sys_path_0_buf, MAXPATHLEN)) { |
1033 | | // If we failed to getcwd, don't raise an exception and instead |
1034 | | // let the caller proceed assuming no shadowing |
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | 0 | sys_path_0 = sys_path_0_buf; |
1038 | 0 | } |
1039 | | |
1040 | 0 | int result = wcscmp(sys_path_0, root) == 0; |
1041 | 0 | return result; |
1042 | 0 | } |
1043 | | |
1044 | | PyObject* |
1045 | | _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress) |
1046 | 1.27M | { |
1047 | | // When suppress=1, this function suppresses AttributeError. |
1048 | 1.27M | PyObject *attr, *mod_name, *getattr; |
1049 | 1.27M | attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress); |
1050 | 1.27M | if (attr) { |
1051 | 1.25M | return attr; |
1052 | 1.25M | } |
1053 | 16.5k | if (suppress == 1) { |
1054 | 10.4k | if (PyErr_Occurred()) { |
1055 | | // pass up non-AttributeError exception |
1056 | 0 | return NULL; |
1057 | 0 | } |
1058 | 10.4k | } |
1059 | 6.03k | else { |
1060 | 6.03k | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1061 | | // pass up non-AttributeError exception |
1062 | 0 | return NULL; |
1063 | 0 | } |
1064 | 6.03k | PyErr_Clear(); |
1065 | 6.03k | } |
1066 | 16.5k | assert(m->md_dict != NULL); |
1067 | 16.5k | if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) { |
1068 | 0 | return NULL; |
1069 | 0 | } |
1070 | 16.5k | if (getattr) { |
1071 | 48 | PyObject *result = PyObject_CallOneArg(getattr, name); |
1072 | 48 | if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1073 | | // suppress AttributeError |
1074 | 48 | PyErr_Clear(); |
1075 | 48 | } |
1076 | 48 | Py_DECREF(getattr); |
1077 | 48 | return result; |
1078 | 48 | } |
1079 | | |
1080 | | // The attribute was not found. We make a best effort attempt at a useful error message, |
1081 | | // but only if we're not suppressing AttributeError. |
1082 | 16.4k | if (suppress == 1) { |
1083 | 10.4k | return NULL; |
1084 | 10.4k | } |
1085 | 6.03k | if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) { |
1086 | 0 | return NULL; |
1087 | 0 | } |
1088 | 6.03k | if (!mod_name || !PyUnicode_Check(mod_name)) { |
1089 | 0 | Py_XDECREF(mod_name); |
1090 | 0 | PyErr_Format(PyExc_AttributeError, |
1091 | 0 | "module has no attribute '%U'", name); |
1092 | 0 | return NULL; |
1093 | 0 | } |
1094 | 6.03k | PyObject *spec; |
1095 | 6.03k | if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) { |
1096 | 0 | Py_DECREF(mod_name); |
1097 | 0 | return NULL; |
1098 | 0 | } |
1099 | 6.03k | if (spec == NULL) { |
1100 | 0 | PyErr_Format(PyExc_AttributeError, |
1101 | 0 | "module '%U' has no attribute '%U'", |
1102 | 0 | mod_name, name); |
1103 | 0 | Py_DECREF(mod_name); |
1104 | 0 | return NULL; |
1105 | 0 | } |
1106 | | |
1107 | 6.03k | PyObject *origin = NULL; |
1108 | 6.03k | if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) { |
1109 | 0 | goto done; |
1110 | 0 | } |
1111 | | |
1112 | 6.03k | int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin); |
1113 | 6.03k | if (is_possibly_shadowing < 0) { |
1114 | 0 | goto done; |
1115 | 0 | } |
1116 | 6.03k | int is_possibly_shadowing_stdlib = 0; |
1117 | 6.03k | if (is_possibly_shadowing) { |
1118 | 0 | PyObject *stdlib_modules; |
1119 | 0 | if (PySys_GetOptionalAttrString("stdlib_module_names", &stdlib_modules) < 0) { |
1120 | 0 | goto done; |
1121 | 0 | } |
1122 | 0 | if (stdlib_modules && PyAnySet_Check(stdlib_modules)) { |
1123 | 0 | is_possibly_shadowing_stdlib = PySet_Contains(stdlib_modules, mod_name); |
1124 | 0 | if (is_possibly_shadowing_stdlib < 0) { |
1125 | 0 | Py_DECREF(stdlib_modules); |
1126 | 0 | goto done; |
1127 | 0 | } |
1128 | 0 | } |
1129 | 0 | Py_XDECREF(stdlib_modules); |
1130 | 0 | } |
1131 | | |
1132 | 6.03k | if (is_possibly_shadowing_stdlib) { |
1133 | 0 | assert(origin); |
1134 | 0 | PyErr_Format(PyExc_AttributeError, |
1135 | 0 | "module '%U' has no attribute '%U' " |
1136 | 0 | "(consider renaming '%U' since it has the same " |
1137 | 0 | "name as the standard library module named '%U' " |
1138 | 0 | "and prevents importing that standard library module)", |
1139 | 0 | mod_name, name, origin, mod_name); |
1140 | 0 | } |
1141 | 6.03k | else { |
1142 | 6.03k | int rc = _PyModuleSpec_IsInitializing(spec); |
1143 | 6.03k | if (rc < 0) { |
1144 | 0 | goto done; |
1145 | 0 | } |
1146 | 6.03k | else if (rc > 0) { |
1147 | 0 | if (is_possibly_shadowing) { |
1148 | 0 | assert(origin); |
1149 | | // For non-stdlib modules, only mention the possibility of |
1150 | | // shadowing if the module is being initialized. |
1151 | 0 | PyErr_Format(PyExc_AttributeError, |
1152 | 0 | "module '%U' has no attribute '%U' " |
1153 | 0 | "(consider renaming '%U' if it has the same name " |
1154 | 0 | "as a library you intended to import)", |
1155 | 0 | mod_name, name, origin); |
1156 | 0 | } |
1157 | 0 | else if (origin) { |
1158 | 0 | PyErr_Format(PyExc_AttributeError, |
1159 | 0 | "partially initialized " |
1160 | 0 | "module '%U' from '%U' has no attribute '%U' " |
1161 | 0 | "(most likely due to a circular import)", |
1162 | 0 | mod_name, origin, name); |
1163 | 0 | } |
1164 | 0 | else { |
1165 | 0 | PyErr_Format(PyExc_AttributeError, |
1166 | 0 | "partially initialized " |
1167 | 0 | "module '%U' has no attribute '%U' " |
1168 | 0 | "(most likely due to a circular import)", |
1169 | 0 | mod_name, name); |
1170 | 0 | } |
1171 | 0 | } |
1172 | 6.03k | else { |
1173 | 6.03k | assert(rc == 0); |
1174 | 6.03k | rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name); |
1175 | 6.03k | if (rc > 0) { |
1176 | 0 | PyErr_Format(PyExc_AttributeError, |
1177 | 0 | "cannot access submodule '%U' of module '%U' " |
1178 | 0 | "(most likely due to a circular import)", |
1179 | 0 | name, mod_name); |
1180 | 0 | } |
1181 | 6.03k | else if (rc == 0) { |
1182 | 6.03k | PyErr_Format(PyExc_AttributeError, |
1183 | 6.03k | "module '%U' has no attribute '%U'", |
1184 | 6.03k | mod_name, name); |
1185 | 6.03k | } |
1186 | 6.03k | } |
1187 | 6.03k | } |
1188 | | |
1189 | 6.03k | done: |
1190 | 6.03k | Py_XDECREF(origin); |
1191 | 6.03k | Py_DECREF(spec); |
1192 | 6.03k | Py_DECREF(mod_name); |
1193 | 6.03k | return NULL; |
1194 | 6.03k | } |
1195 | | |
1196 | | |
1197 | | PyObject* |
1198 | | _Py_module_getattro(PyObject *self, PyObject *name) |
1199 | 1.18M | { |
1200 | 1.18M | PyModuleObject *m = _PyModule_CAST(self); |
1201 | 1.18M | return _Py_module_getattro_impl(m, name, 0); |
1202 | 1.18M | } |
1203 | | |
1204 | | static int |
1205 | | module_traverse(PyObject *self, visitproc visit, void *arg) |
1206 | 455k | { |
1207 | 455k | PyModuleObject *m = _PyModule_CAST(self); |
1208 | | |
1209 | | /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */ |
1210 | 455k | if (m->md_def && m->md_def->m_traverse |
1211 | 71.6k | && (m->md_def->m_size <= 0 || m->md_state != NULL)) |
1212 | 71.6k | { |
1213 | 71.6k | int res = m->md_def->m_traverse((PyObject*)m, visit, arg); |
1214 | 71.6k | if (res) |
1215 | 0 | return res; |
1216 | 71.6k | } |
1217 | | |
1218 | 455k | Py_VISIT(m->md_dict); |
1219 | 455k | return 0; |
1220 | 455k | } |
1221 | | |
1222 | | static int |
1223 | | module_clear(PyObject *self) |
1224 | 0 | { |
1225 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
1226 | | |
1227 | | /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */ |
1228 | 0 | if (m->md_def && m->md_def->m_clear |
1229 | 0 | && (m->md_def->m_size <= 0 || m->md_state != NULL)) |
1230 | 0 | { |
1231 | 0 | int res = m->md_def->m_clear((PyObject*)m); |
1232 | 0 | if (PyErr_Occurred()) { |
1233 | 0 | PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V", |
1234 | 0 | m->md_name ? " " : "", |
1235 | 0 | m->md_name, ""); |
1236 | 0 | } |
1237 | 0 | if (res) |
1238 | 0 | return res; |
1239 | 0 | } |
1240 | 0 | Py_CLEAR(m->md_dict); |
1241 | 0 | return 0; |
1242 | 0 | } |
1243 | | |
1244 | | static PyObject * |
1245 | | module_dir(PyObject *self, PyObject *args) |
1246 | 20 | { |
1247 | 20 | PyObject *result = NULL; |
1248 | 20 | PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__)); |
1249 | | |
1250 | 20 | if (dict != NULL) { |
1251 | 20 | if (PyDict_Check(dict)) { |
1252 | 20 | PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__)); |
1253 | 20 | if (dirfunc) { |
1254 | 0 | result = _PyObject_CallNoArgs(dirfunc); |
1255 | 0 | } |
1256 | 20 | else if (!PyErr_Occurred()) { |
1257 | 20 | result = PyDict_Keys(dict); |
1258 | 20 | } |
1259 | 20 | } |
1260 | 0 | else { |
1261 | 0 | PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary"); |
1262 | 0 | } |
1263 | 20 | } |
1264 | | |
1265 | 20 | Py_XDECREF(dict); |
1266 | 20 | return result; |
1267 | 20 | } |
1268 | | |
1269 | | static PyMethodDef module_methods[] = { |
1270 | | {"__dir__", module_dir, METH_NOARGS, |
1271 | | PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, |
1272 | | {0} |
1273 | | }; |
1274 | | |
1275 | | static PyObject * |
1276 | | module_get_dict(PyModuleObject *m) |
1277 | 0 | { |
1278 | 0 | PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__)); |
1279 | 0 | if (dict == NULL) { |
1280 | 0 | return NULL; |
1281 | 0 | } |
1282 | 0 | if (!PyDict_Check(dict)) { |
1283 | 0 | PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary"); |
1284 | 0 | Py_DECREF(dict); |
1285 | 0 | return NULL; |
1286 | 0 | } |
1287 | 0 | return dict; |
1288 | 0 | } |
1289 | | |
1290 | | static PyObject * |
1291 | | module_get_annotate(PyObject *self, void *Py_UNUSED(ignored)) |
1292 | 0 | { |
1293 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
1294 | |
|
1295 | 0 | PyObject *dict = module_get_dict(m); |
1296 | 0 | if (dict == NULL) { |
1297 | 0 | return NULL; |
1298 | 0 | } |
1299 | | |
1300 | 0 | PyObject *annotate; |
1301 | 0 | if (PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate) == 0) { |
1302 | 0 | annotate = Py_None; |
1303 | 0 | if (PyDict_SetItem(dict, &_Py_ID(__annotate__), annotate) == -1) { |
1304 | 0 | Py_CLEAR(annotate); |
1305 | 0 | } |
1306 | 0 | } |
1307 | 0 | Py_DECREF(dict); |
1308 | 0 | return annotate; |
1309 | 0 | } |
1310 | | |
1311 | | static int |
1312 | | module_set_annotate(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
1313 | 0 | { |
1314 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
1315 | 0 | if (value == NULL) { |
1316 | 0 | PyErr_SetString(PyExc_TypeError, "cannot delete __annotate__ attribute"); |
1317 | 0 | return -1; |
1318 | 0 | } |
1319 | | |
1320 | 0 | PyObject *dict = module_get_dict(m); |
1321 | 0 | if (dict == NULL) { |
1322 | 0 | return -1; |
1323 | 0 | } |
1324 | | |
1325 | 0 | if (!Py_IsNone(value) && !PyCallable_Check(value)) { |
1326 | 0 | PyErr_SetString(PyExc_TypeError, "__annotate__ must be callable or None"); |
1327 | 0 | Py_DECREF(dict); |
1328 | 0 | return -1; |
1329 | 0 | } |
1330 | | |
1331 | 0 | if (PyDict_SetItem(dict, &_Py_ID(__annotate__), value) == -1) { |
1332 | 0 | Py_DECREF(dict); |
1333 | 0 | return -1; |
1334 | 0 | } |
1335 | 0 | if (!Py_IsNone(value)) { |
1336 | 0 | if (PyDict_Pop(dict, &_Py_ID(__annotations__), NULL) == -1) { |
1337 | 0 | Py_DECREF(dict); |
1338 | 0 | return -1; |
1339 | 0 | } |
1340 | 0 | } |
1341 | 0 | Py_DECREF(dict); |
1342 | 0 | return 0; |
1343 | 0 | } |
1344 | | |
1345 | | static PyObject * |
1346 | | module_get_annotations(PyObject *self, void *Py_UNUSED(ignored)) |
1347 | 0 | { |
1348 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
1349 | |
|
1350 | 0 | PyObject *dict = module_get_dict(m); |
1351 | 0 | if (dict == NULL) { |
1352 | 0 | return NULL; |
1353 | 0 | } |
1354 | | |
1355 | 0 | PyObject *annotations; |
1356 | 0 | if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) { |
1357 | 0 | PyObject *spec; |
1358 | 0 | if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) { |
1359 | 0 | Py_DECREF(dict); |
1360 | 0 | return NULL; |
1361 | 0 | } |
1362 | 0 | bool is_initializing = false; |
1363 | 0 | if (spec != NULL) { |
1364 | 0 | int rc = _PyModuleSpec_IsInitializing(spec); |
1365 | 0 | if (rc < 0) { |
1366 | 0 | Py_DECREF(spec); |
1367 | 0 | Py_DECREF(dict); |
1368 | 0 | return NULL; |
1369 | 0 | } |
1370 | 0 | Py_DECREF(spec); |
1371 | 0 | if (rc) { |
1372 | 0 | is_initializing = true; |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | 0 | PyObject *annotate; |
1377 | 0 | int annotate_result = PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate); |
1378 | 0 | if (annotate_result < 0) { |
1379 | 0 | Py_DECREF(dict); |
1380 | 0 | return NULL; |
1381 | 0 | } |
1382 | 0 | if (annotate_result == 1 && PyCallable_Check(annotate)) { |
1383 | 0 | PyObject *one = _PyLong_GetOne(); |
1384 | 0 | annotations = _PyObject_CallOneArg(annotate, one); |
1385 | 0 | if (annotations == NULL) { |
1386 | 0 | Py_DECREF(annotate); |
1387 | 0 | Py_DECREF(dict); |
1388 | 0 | return NULL; |
1389 | 0 | } |
1390 | 0 | if (!PyDict_Check(annotations)) { |
1391 | 0 | PyErr_Format(PyExc_TypeError, |
1392 | 0 | "__annotate__() must return a dict, not %T", |
1393 | 0 | annotations); |
1394 | 0 | Py_DECREF(annotate); |
1395 | 0 | Py_DECREF(annotations); |
1396 | 0 | Py_DECREF(dict); |
1397 | 0 | return NULL; |
1398 | 0 | } |
1399 | 0 | } |
1400 | 0 | else { |
1401 | 0 | annotations = PyDict_New(); |
1402 | 0 | } |
1403 | 0 | Py_XDECREF(annotate); |
1404 | | // Do not cache annotations if the module is still initializing |
1405 | 0 | if (annotations && !is_initializing) { |
1406 | 0 | int result = PyDict_SetItem( |
1407 | 0 | dict, &_Py_ID(__annotations__), annotations); |
1408 | 0 | if (result) { |
1409 | 0 | Py_CLEAR(annotations); |
1410 | 0 | } |
1411 | 0 | } |
1412 | 0 | } |
1413 | 0 | Py_DECREF(dict); |
1414 | 0 | return annotations; |
1415 | 0 | } |
1416 | | |
1417 | | static int |
1418 | | module_set_annotations(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
1419 | 0 | { |
1420 | 0 | PyModuleObject *m = _PyModule_CAST(self); |
1421 | |
|
1422 | 0 | PyObject *dict = module_get_dict(m); |
1423 | 0 | if (dict == NULL) { |
1424 | 0 | return -1; |
1425 | 0 | } |
1426 | | |
1427 | 0 | int ret = -1; |
1428 | 0 | if (value != NULL) { |
1429 | | /* set */ |
1430 | 0 | ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value); |
1431 | 0 | } |
1432 | 0 | else { |
1433 | | /* delete */ |
1434 | 0 | ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL); |
1435 | 0 | if (ret == 0) { |
1436 | 0 | PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__)); |
1437 | 0 | ret = -1; |
1438 | 0 | } |
1439 | 0 | else if (ret > 0) { |
1440 | 0 | ret = 0; |
1441 | 0 | } |
1442 | 0 | } |
1443 | 0 | if (ret == 0 && PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) { |
1444 | 0 | ret = -1; |
1445 | 0 | } |
1446 | |
|
1447 | 0 | Py_DECREF(dict); |
1448 | 0 | return ret; |
1449 | 0 | } |
1450 | | |
1451 | | |
1452 | | static PyGetSetDef module_getsets[] = { |
1453 | | {"__annotations__", module_get_annotations, module_set_annotations}, |
1454 | | {"__annotate__", module_get_annotate, module_set_annotate}, |
1455 | | {NULL} |
1456 | | }; |
1457 | | |
1458 | | PyTypeObject PyModule_Type = { |
1459 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1460 | | "module", /* tp_name */ |
1461 | | sizeof(PyModuleObject), /* tp_basicsize */ |
1462 | | 0, /* tp_itemsize */ |
1463 | | module_dealloc, /* tp_dealloc */ |
1464 | | 0, /* tp_vectorcall_offset */ |
1465 | | 0, /* tp_getattr */ |
1466 | | 0, /* tp_setattr */ |
1467 | | 0, /* tp_as_async */ |
1468 | | module_repr, /* tp_repr */ |
1469 | | 0, /* tp_as_number */ |
1470 | | 0, /* tp_as_sequence */ |
1471 | | 0, /* tp_as_mapping */ |
1472 | | 0, /* tp_hash */ |
1473 | | 0, /* tp_call */ |
1474 | | 0, /* tp_str */ |
1475 | | _Py_module_getattro, /* tp_getattro */ |
1476 | | PyObject_GenericSetAttr, /* tp_setattro */ |
1477 | | 0, /* tp_as_buffer */ |
1478 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1479 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
1480 | | module___init____doc__, /* tp_doc */ |
1481 | | module_traverse, /* tp_traverse */ |
1482 | | module_clear, /* tp_clear */ |
1483 | | 0, /* tp_richcompare */ |
1484 | | offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */ |
1485 | | 0, /* tp_iter */ |
1486 | | 0, /* tp_iternext */ |
1487 | | module_methods, /* tp_methods */ |
1488 | | module_members, /* tp_members */ |
1489 | | module_getsets, /* tp_getset */ |
1490 | | 0, /* tp_base */ |
1491 | | 0, /* tp_dict */ |
1492 | | 0, /* tp_descr_get */ |
1493 | | 0, /* tp_descr_set */ |
1494 | | offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
1495 | | module___init__, /* tp_init */ |
1496 | | 0, /* tp_alloc */ |
1497 | | new_module, /* tp_new */ |
1498 | | PyObject_GC_Del, /* tp_free */ |
1499 | | }; |