moduleobject.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /* Module object implementation */
  2. #include "Python.h"
  3. #include "pycore_call.h" // _PyObject_CallNoArgs()
  4. #include "pycore_interp.h" // PyInterpreterState.importlib
  5. #include "pycore_object.h" // _PyType_AllocNoTrack
  6. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  7. #include "pycore_moduleobject.h" // _PyModule_GetDef()
  8. #include "structmember.h" // PyMemberDef
  9. static PyMemberDef module_members[] = {
  10. {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
  11. {0}
  12. };
  13. PyTypeObject PyModuleDef_Type = {
  14. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  15. "moduledef", /* tp_name */
  16. sizeof(PyModuleDef), /* tp_basicsize */
  17. 0, /* tp_itemsize */
  18. };
  19. int
  20. _PyModule_IsExtension(PyObject *obj)
  21. {
  22. if (!PyModule_Check(obj)) {
  23. return 0;
  24. }
  25. PyModuleObject *module = (PyModuleObject*)obj;
  26. PyModuleDef *def = module->md_def;
  27. return (def != NULL && def->m_methods != NULL);
  28. }
  29. PyObject*
  30. PyModuleDef_Init(PyModuleDef* def)
  31. {
  32. assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
  33. if (def->m_base.m_index == 0) {
  34. Py_SET_REFCNT(def, 1);
  35. Py_SET_TYPE(def, &PyModuleDef_Type);
  36. def->m_base.m_index = _PyImport_GetNextModuleIndex();
  37. }
  38. return (PyObject*)def;
  39. }
  40. static int
  41. module_init_dict(PyModuleObject *mod, PyObject *md_dict,
  42. PyObject *name, PyObject *doc)
  43. {
  44. assert(md_dict != NULL);
  45. if (doc == NULL)
  46. doc = Py_None;
  47. if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
  48. return -1;
  49. if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
  50. return -1;
  51. if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
  52. return -1;
  53. if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
  54. return -1;
  55. if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
  56. return -1;
  57. if (PyUnicode_CheckExact(name)) {
  58. Py_XSETREF(mod->md_name, Py_NewRef(name));
  59. }
  60. return 0;
  61. }
  62. static PyModuleObject *
  63. new_module_notrack(PyTypeObject *mt)
  64. {
  65. PyModuleObject *m;
  66. m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
  67. if (m == NULL)
  68. return NULL;
  69. m->md_def = NULL;
  70. m->md_state = NULL;
  71. m->md_weaklist = NULL;
  72. m->md_name = NULL;
  73. m->md_dict = PyDict_New();
  74. if (m->md_dict != NULL) {
  75. return m;
  76. }
  77. Py_DECREF(m);
  78. return NULL;
  79. }
  80. static PyObject *
  81. new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
  82. {
  83. PyObject *m = (PyObject *)new_module_notrack(mt);
  84. if (m != NULL) {
  85. PyObject_GC_Track(m);
  86. }
  87. return m;
  88. }
  89. PyObject *
  90. PyModule_NewObject(PyObject *name)
  91. {
  92. PyModuleObject *m = new_module_notrack(&PyModule_Type);
  93. if (m == NULL)
  94. return NULL;
  95. if (module_init_dict(m, m->md_dict, name, NULL) != 0)
  96. goto fail;
  97. PyObject_GC_Track(m);
  98. return (PyObject *)m;
  99. fail:
  100. Py_DECREF(m);
  101. return NULL;
  102. }
  103. PyObject *
  104. PyModule_New(const char *name)
  105. {
  106. PyObject *nameobj, *module;
  107. nameobj = PyUnicode_FromString(name);
  108. if (nameobj == NULL)
  109. return NULL;
  110. module = PyModule_NewObject(nameobj);
  111. Py_DECREF(nameobj);
  112. return module;
  113. }
  114. /* Check API/ABI version
  115. * Issues a warning on mismatch, which is usually not fatal.
  116. * Returns 0 if an exception is raised.
  117. */
  118. static int
  119. check_api_version(const char *name, int module_api_version)
  120. {
  121. if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
  122. int err;
  123. err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
  124. "Python C API version mismatch for module %.100s: "
  125. "This Python has API version %d, module %.100s has version %d.",
  126. name,
  127. PYTHON_API_VERSION, name, module_api_version);
  128. if (err)
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. static int
  134. _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
  135. {
  136. PyObject *func;
  137. PyMethodDef *fdef;
  138. for (fdef = functions; fdef->ml_name != NULL; fdef++) {
  139. if ((fdef->ml_flags & METH_CLASS) ||
  140. (fdef->ml_flags & METH_STATIC)) {
  141. PyErr_SetString(PyExc_ValueError,
  142. "module functions cannot set"
  143. " METH_CLASS or METH_STATIC");
  144. return -1;
  145. }
  146. func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
  147. if (func == NULL) {
  148. return -1;
  149. }
  150. if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
  151. Py_DECREF(func);
  152. return -1;
  153. }
  154. Py_DECREF(func);
  155. }
  156. return 0;
  157. }
  158. PyObject *
  159. PyModule_Create2(PyModuleDef* module, int module_api_version)
  160. {
  161. if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
  162. PyErr_SetString(PyExc_SystemError,
  163. "Python import machinery not initialized");
  164. return NULL;
  165. }
  166. return _PyModule_CreateInitialized(module, module_api_version);
  167. }
  168. PyObject *
  169. _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
  170. {
  171. const char* name;
  172. PyModuleObject *m;
  173. if (!PyModuleDef_Init(module))
  174. return NULL;
  175. name = module->m_name;
  176. if (!check_api_version(name, module_api_version)) {
  177. return NULL;
  178. }
  179. if (module->m_slots) {
  180. PyErr_Format(
  181. PyExc_SystemError,
  182. "module %s: PyModule_Create is incompatible with m_slots", name);
  183. return NULL;
  184. }
  185. name = _PyImport_ResolveNameWithPackageContext(name);
  186. if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
  187. return NULL;
  188. if (module->m_size > 0) {
  189. m->md_state = PyMem_Malloc(module->m_size);
  190. if (!m->md_state) {
  191. PyErr_NoMemory();
  192. Py_DECREF(m);
  193. return NULL;
  194. }
  195. memset(m->md_state, 0, module->m_size);
  196. }
  197. if (module->m_methods != NULL) {
  198. if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
  199. Py_DECREF(m);
  200. return NULL;
  201. }
  202. }
  203. if (module->m_doc != NULL) {
  204. if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
  205. Py_DECREF(m);
  206. return NULL;
  207. }
  208. }
  209. m->md_def = module;
  210. return (PyObject*)m;
  211. }
  212. PyObject *
  213. PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
  214. {
  215. PyModuleDef_Slot* cur_slot;
  216. PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
  217. PyObject *nameobj;
  218. PyObject *m = NULL;
  219. int has_multiple_interpreters_slot = 0;
  220. void *multiple_interpreters = (void *)0;
  221. int has_execution_slots = 0;
  222. const char *name;
  223. int ret;
  224. PyInterpreterState *interp = _PyInterpreterState_GET();
  225. PyModuleDef_Init(def);
  226. nameobj = PyObject_GetAttrString(spec, "name");
  227. if (nameobj == NULL) {
  228. return NULL;
  229. }
  230. name = PyUnicode_AsUTF8(nameobj);
  231. if (name == NULL) {
  232. goto error;
  233. }
  234. if (!check_api_version(name, module_api_version)) {
  235. goto error;
  236. }
  237. if (def->m_size < 0) {
  238. PyErr_Format(
  239. PyExc_SystemError,
  240. "module %s: m_size may not be negative for multi-phase initialization",
  241. name);
  242. goto error;
  243. }
  244. for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
  245. switch (cur_slot->slot) {
  246. case Py_mod_create:
  247. if (create) {
  248. PyErr_Format(
  249. PyExc_SystemError,
  250. "module %s has multiple create slots",
  251. name);
  252. goto error;
  253. }
  254. create = cur_slot->value;
  255. break;
  256. case Py_mod_exec:
  257. has_execution_slots = 1;
  258. break;
  259. case Py_mod_multiple_interpreters:
  260. if (has_multiple_interpreters_slot) {
  261. PyErr_Format(
  262. PyExc_SystemError,
  263. "module %s has more than one 'multiple interpreters' slots",
  264. name);
  265. goto error;
  266. }
  267. multiple_interpreters = cur_slot->value;
  268. has_multiple_interpreters_slot = 1;
  269. break;
  270. default:
  271. assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
  272. PyErr_Format(
  273. PyExc_SystemError,
  274. "module %s uses unknown slot ID %i",
  275. name, cur_slot->slot);
  276. goto error;
  277. }
  278. }
  279. /* By default, multi-phase init modules are expected
  280. to work under multiple interpreters. */
  281. if (!has_multiple_interpreters_slot) {
  282. multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
  283. }
  284. if (multiple_interpreters == Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED) {
  285. if (!_Py_IsMainInterpreter(interp)
  286. && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
  287. {
  288. goto error;
  289. }
  290. }
  291. else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
  292. && interp->ceval.own_gil
  293. && !_Py_IsMainInterpreter(interp)
  294. && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
  295. {
  296. goto error;
  297. }
  298. if (create) {
  299. m = create(spec, def);
  300. if (m == NULL) {
  301. if (!PyErr_Occurred()) {
  302. PyErr_Format(
  303. PyExc_SystemError,
  304. "creation of module %s failed without setting an exception",
  305. name);
  306. }
  307. goto error;
  308. } else {
  309. if (PyErr_Occurred()) {
  310. _PyErr_FormatFromCause(
  311. PyExc_SystemError,
  312. "creation of module %s raised unreported exception",
  313. name);
  314. goto error;
  315. }
  316. }
  317. } else {
  318. m = PyModule_NewObject(nameobj);
  319. if (m == NULL) {
  320. goto error;
  321. }
  322. }
  323. if (PyModule_Check(m)) {
  324. ((PyModuleObject*)m)->md_state = NULL;
  325. ((PyModuleObject*)m)->md_def = def;
  326. } else {
  327. if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
  328. PyErr_Format(
  329. PyExc_SystemError,
  330. "module %s is not a module object, but requests module state",
  331. name);
  332. goto error;
  333. }
  334. if (has_execution_slots) {
  335. PyErr_Format(
  336. PyExc_SystemError,
  337. "module %s specifies execution slots, but did not create "
  338. "a ModuleType instance",
  339. name);
  340. goto error;
  341. }
  342. }
  343. if (def->m_methods != NULL) {
  344. ret = _add_methods_to_object(m, nameobj, def->m_methods);
  345. if (ret != 0) {
  346. goto error;
  347. }
  348. }
  349. if (def->m_doc != NULL) {
  350. ret = PyModule_SetDocString(m, def->m_doc);
  351. if (ret != 0) {
  352. goto error;
  353. }
  354. }
  355. Py_DECREF(nameobj);
  356. return m;
  357. error:
  358. Py_DECREF(nameobj);
  359. Py_XDECREF(m);
  360. return NULL;
  361. }
  362. int
  363. PyModule_ExecDef(PyObject *module, PyModuleDef *def)
  364. {
  365. PyModuleDef_Slot *cur_slot;
  366. const char *name;
  367. int ret;
  368. name = PyModule_GetName(module);
  369. if (name == NULL) {
  370. return -1;
  371. }
  372. if (def->m_size >= 0) {
  373. PyModuleObject *md = (PyModuleObject*)module;
  374. if (md->md_state == NULL) {
  375. /* Always set a state pointer; this serves as a marker to skip
  376. * multiple initialization (importlib.reload() is no-op) */
  377. md->md_state = PyMem_Malloc(def->m_size);
  378. if (!md->md_state) {
  379. PyErr_NoMemory();
  380. return -1;
  381. }
  382. memset(md->md_state, 0, def->m_size);
  383. }
  384. }
  385. if (def->m_slots == NULL) {
  386. return 0;
  387. }
  388. for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
  389. switch (cur_slot->slot) {
  390. case Py_mod_create:
  391. /* handled in PyModule_FromDefAndSpec2 */
  392. break;
  393. case Py_mod_exec:
  394. ret = ((int (*)(PyObject *))cur_slot->value)(module);
  395. if (ret != 0) {
  396. if (!PyErr_Occurred()) {
  397. PyErr_Format(
  398. PyExc_SystemError,
  399. "execution of module %s failed without setting an exception",
  400. name);
  401. }
  402. return -1;
  403. }
  404. if (PyErr_Occurred()) {
  405. _PyErr_FormatFromCause(
  406. PyExc_SystemError,
  407. "execution of module %s raised unreported exception",
  408. name);
  409. return -1;
  410. }
  411. break;
  412. case Py_mod_multiple_interpreters:
  413. /* handled in PyModule_FromDefAndSpec2 */
  414. break;
  415. default:
  416. PyErr_Format(
  417. PyExc_SystemError,
  418. "module %s initialized with unknown slot %i",
  419. name, cur_slot->slot);
  420. return -1;
  421. }
  422. }
  423. return 0;
  424. }
  425. int
  426. PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
  427. {
  428. int res;
  429. PyObject *name = PyModule_GetNameObject(m);
  430. if (name == NULL) {
  431. return -1;
  432. }
  433. res = _add_methods_to_object(m, name, functions);
  434. Py_DECREF(name);
  435. return res;
  436. }
  437. int
  438. PyModule_SetDocString(PyObject *m, const char *doc)
  439. {
  440. PyObject *v;
  441. v = PyUnicode_FromString(doc);
  442. if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
  443. Py_XDECREF(v);
  444. return -1;
  445. }
  446. Py_DECREF(v);
  447. return 0;
  448. }
  449. PyObject *
  450. PyModule_GetDict(PyObject *m)
  451. {
  452. if (!PyModule_Check(m)) {
  453. PyErr_BadInternalCall();
  454. return NULL;
  455. }
  456. return _PyModule_GetDict(m);
  457. }
  458. PyObject*
  459. PyModule_GetNameObject(PyObject *m)
  460. {
  461. PyObject *d;
  462. PyObject *name;
  463. if (!PyModule_Check(m)) {
  464. PyErr_BadArgument();
  465. return NULL;
  466. }
  467. d = ((PyModuleObject *)m)->md_dict;
  468. if (d == NULL || !PyDict_Check(d) ||
  469. (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL ||
  470. !PyUnicode_Check(name))
  471. {
  472. if (!PyErr_Occurred()) {
  473. PyErr_SetString(PyExc_SystemError, "nameless module");
  474. }
  475. return NULL;
  476. }
  477. return Py_NewRef(name);
  478. }
  479. const char *
  480. PyModule_GetName(PyObject *m)
  481. {
  482. PyObject *name = PyModule_GetNameObject(m);
  483. if (name == NULL) {
  484. return NULL;
  485. }
  486. assert(Py_REFCNT(name) >= 2);
  487. Py_DECREF(name); /* module dict has still a reference */
  488. return PyUnicode_AsUTF8(name);
  489. }
  490. PyObject*
  491. PyModule_GetFilenameObject(PyObject *m)
  492. {
  493. PyObject *d;
  494. PyObject *fileobj;
  495. if (!PyModule_Check(m)) {
  496. PyErr_BadArgument();
  497. return NULL;
  498. }
  499. d = ((PyModuleObject *)m)->md_dict;
  500. if (d == NULL ||
  501. (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL ||
  502. !PyUnicode_Check(fileobj))
  503. {
  504. if (!PyErr_Occurred()) {
  505. PyErr_SetString(PyExc_SystemError, "module filename missing");
  506. }
  507. return NULL;
  508. }
  509. return Py_NewRef(fileobj);
  510. }
  511. const char *
  512. PyModule_GetFilename(PyObject *m)
  513. {
  514. PyObject *fileobj;
  515. const char *utf8;
  516. fileobj = PyModule_GetFilenameObject(m);
  517. if (fileobj == NULL)
  518. return NULL;
  519. utf8 = PyUnicode_AsUTF8(fileobj);
  520. Py_DECREF(fileobj); /* module dict has still a reference */
  521. return utf8;
  522. }
  523. PyModuleDef*
  524. PyModule_GetDef(PyObject* m)
  525. {
  526. if (!PyModule_Check(m)) {
  527. PyErr_BadArgument();
  528. return NULL;
  529. }
  530. return _PyModule_GetDef(m);
  531. }
  532. void*
  533. PyModule_GetState(PyObject* m)
  534. {
  535. if (!PyModule_Check(m)) {
  536. PyErr_BadArgument();
  537. return NULL;
  538. }
  539. return _PyModule_GetState(m);
  540. }
  541. void
  542. _PyModule_Clear(PyObject *m)
  543. {
  544. PyObject *d = ((PyModuleObject *)m)->md_dict;
  545. if (d != NULL)
  546. _PyModule_ClearDict(d);
  547. }
  548. void
  549. _PyModule_ClearDict(PyObject *d)
  550. {
  551. /* To make the execution order of destructors for global
  552. objects a bit more predictable, we first zap all objects
  553. whose name starts with a single underscore, before we clear
  554. the entire dictionary. We zap them by replacing them with
  555. None, rather than deleting them from the dictionary, to
  556. avoid rehashing the dictionary (to some extent). */
  557. Py_ssize_t pos;
  558. PyObject *key, *value;
  559. int verbose = _Py_GetConfig()->verbose;
  560. /* First, clear only names starting with a single underscore */
  561. pos = 0;
  562. while (PyDict_Next(d, &pos, &key, &value)) {
  563. if (value != Py_None && PyUnicode_Check(key)) {
  564. if (PyUnicode_READ_CHAR(key, 0) == '_' &&
  565. PyUnicode_READ_CHAR(key, 1) != '_') {
  566. if (verbose > 1) {
  567. const char *s = PyUnicode_AsUTF8(key);
  568. if (s != NULL)
  569. PySys_WriteStderr("# clear[1] %s\n", s);
  570. else
  571. PyErr_Clear();
  572. }
  573. if (PyDict_SetItem(d, key, Py_None) != 0) {
  574. PyErr_WriteUnraisable(NULL);
  575. }
  576. }
  577. }
  578. }
  579. /* Next, clear all names except for __builtins__ */
  580. pos = 0;
  581. while (PyDict_Next(d, &pos, &key, &value)) {
  582. if (value != Py_None && PyUnicode_Check(key)) {
  583. if (PyUnicode_READ_CHAR(key, 0) != '_' ||
  584. !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
  585. {
  586. if (verbose > 1) {
  587. const char *s = PyUnicode_AsUTF8(key);
  588. if (s != NULL)
  589. PySys_WriteStderr("# clear[2] %s\n", s);
  590. else
  591. PyErr_Clear();
  592. }
  593. if (PyDict_SetItem(d, key, Py_None) != 0) {
  594. PyErr_WriteUnraisable(NULL);
  595. }
  596. }
  597. }
  598. }
  599. /* Note: we leave __builtins__ in place, so that destructors
  600. of non-global objects defined in this module can still use
  601. builtins, in particularly 'None'. */
  602. }
  603. /*[clinic input]
  604. class module "PyModuleObject *" "&PyModule_Type"
  605. [clinic start generated code]*/
  606. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
  607. #include "clinic/moduleobject.c.h"
  608. /* Methods */
  609. /*[clinic input]
  610. module.__init__
  611. name: unicode
  612. doc: object = None
  613. Create a module object.
  614. The name must be a string; the optional doc argument can have any type.
  615. [clinic start generated code]*/
  616. static int
  617. module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
  618. /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
  619. {
  620. PyObject *dict = self->md_dict;
  621. if (dict == NULL) {
  622. dict = PyDict_New();
  623. if (dict == NULL)
  624. return -1;
  625. self->md_dict = dict;
  626. }
  627. if (module_init_dict(self, dict, name, doc) < 0)
  628. return -1;
  629. return 0;
  630. }
  631. static void
  632. module_dealloc(PyModuleObject *m)
  633. {
  634. int verbose = _Py_GetConfig()->verbose;
  635. PyObject_GC_UnTrack(m);
  636. if (verbose && m->md_name) {
  637. PySys_FormatStderr("# destroy %U\n", m->md_name);
  638. }
  639. if (m->md_weaklist != NULL)
  640. PyObject_ClearWeakRefs((PyObject *) m);
  641. /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
  642. if (m->md_def && m->md_def->m_free
  643. && (m->md_def->m_size <= 0 || m->md_state != NULL))
  644. {
  645. m->md_def->m_free(m);
  646. }
  647. Py_XDECREF(m->md_dict);
  648. Py_XDECREF(m->md_name);
  649. if (m->md_state != NULL)
  650. PyMem_Free(m->md_state);
  651. Py_TYPE(m)->tp_free((PyObject *)m);
  652. }
  653. static PyObject *
  654. module_repr(PyModuleObject *m)
  655. {
  656. PyInterpreterState *interp = _PyInterpreterState_GET();
  657. return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
  658. }
  659. /* Check if the "_initializing" attribute of the module spec is set to true.
  660. Clear the exception and return 0 if spec is NULL.
  661. */
  662. int
  663. _PyModuleSpec_IsInitializing(PyObject *spec)
  664. {
  665. if (spec != NULL) {
  666. PyObject *value;
  667. int ok = _PyObject_LookupAttr(spec, &_Py_ID(_initializing), &value);
  668. if (ok == 0) {
  669. return 0;
  670. }
  671. if (value != NULL) {
  672. int initializing = PyObject_IsTrue(value);
  673. Py_DECREF(value);
  674. if (initializing >= 0) {
  675. return initializing;
  676. }
  677. }
  678. }
  679. PyErr_Clear();
  680. return 0;
  681. }
  682. /* Check if the submodule name is in the "_uninitialized_submodules" attribute
  683. of the module spec.
  684. */
  685. int
  686. _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
  687. {
  688. if (spec == NULL) {
  689. return 0;
  690. }
  691. PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules));
  692. if (value == NULL) {
  693. return 0;
  694. }
  695. int is_uninitialized = PySequence_Contains(value, name);
  696. Py_DECREF(value);
  697. if (is_uninitialized == -1) {
  698. return 0;
  699. }
  700. return is_uninitialized;
  701. }
  702. PyObject*
  703. _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
  704. {
  705. // When suppress=1, this function suppresses AttributeError.
  706. PyObject *attr, *mod_name, *getattr;
  707. attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
  708. if (attr) {
  709. return attr;
  710. }
  711. if (suppress == 1) {
  712. if (PyErr_Occurred()) {
  713. // pass up non-AttributeError exception
  714. return NULL;
  715. }
  716. }
  717. else {
  718. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
  719. // pass up non-AttributeError exception
  720. return NULL;
  721. }
  722. PyErr_Clear();
  723. }
  724. assert(m->md_dict != NULL);
  725. getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
  726. if (getattr) {
  727. PyObject *result = PyObject_CallOneArg(getattr, name);
  728. if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
  729. // suppress AttributeError
  730. PyErr_Clear();
  731. }
  732. return result;
  733. }
  734. if (PyErr_Occurred()) {
  735. return NULL;
  736. }
  737. mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
  738. if (mod_name && PyUnicode_Check(mod_name)) {
  739. Py_INCREF(mod_name);
  740. PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
  741. if (spec == NULL && PyErr_Occurred()) {
  742. Py_DECREF(mod_name);
  743. return NULL;
  744. }
  745. if (suppress != 1) {
  746. Py_XINCREF(spec);
  747. if (_PyModuleSpec_IsInitializing(spec)) {
  748. PyErr_Format(PyExc_AttributeError,
  749. "partially initialized "
  750. "module '%U' has no attribute '%U' "
  751. "(most likely due to a circular import)",
  752. mod_name, name);
  753. }
  754. else if (_PyModuleSpec_IsUninitializedSubmodule(spec, name)) {
  755. PyErr_Format(PyExc_AttributeError,
  756. "cannot access submodule '%U' of module '%U' "
  757. "(most likely due to a circular import)",
  758. name, mod_name);
  759. }
  760. else {
  761. PyErr_Format(PyExc_AttributeError,
  762. "module '%U' has no attribute '%U'",
  763. mod_name, name);
  764. }
  765. Py_XDECREF(spec);
  766. }
  767. Py_DECREF(mod_name);
  768. return NULL;
  769. }
  770. else if (PyErr_Occurred()) {
  771. return NULL;
  772. }
  773. if (suppress != 1) {
  774. PyErr_Format(PyExc_AttributeError,
  775. "module has no attribute '%U'", name);
  776. }
  777. return NULL;
  778. }
  779. PyObject*
  780. _Py_module_getattro(PyModuleObject *m, PyObject *name)
  781. {
  782. return _Py_module_getattro_impl(m, name, 0);
  783. }
  784. static int
  785. module_traverse(PyModuleObject *m, visitproc visit, void *arg)
  786. {
  787. /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
  788. if (m->md_def && m->md_def->m_traverse
  789. && (m->md_def->m_size <= 0 || m->md_state != NULL))
  790. {
  791. int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
  792. if (res)
  793. return res;
  794. }
  795. Py_VISIT(m->md_dict);
  796. return 0;
  797. }
  798. static int
  799. module_clear(PyModuleObject *m)
  800. {
  801. /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
  802. if (m->md_def && m->md_def->m_clear
  803. && (m->md_def->m_size <= 0 || m->md_state != NULL))
  804. {
  805. int res = m->md_def->m_clear((PyObject*)m);
  806. if (PyErr_Occurred()) {
  807. PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
  808. m->md_name ? " " : "",
  809. m->md_name, "");
  810. PyErr_WriteUnraisable(NULL);
  811. }
  812. if (res)
  813. return res;
  814. }
  815. Py_CLEAR(m->md_dict);
  816. return 0;
  817. }
  818. static PyObject *
  819. module_dir(PyObject *self, PyObject *args)
  820. {
  821. PyObject *result = NULL;
  822. PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
  823. if (dict != NULL) {
  824. if (PyDict_Check(dict)) {
  825. PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
  826. if (dirfunc) {
  827. result = _PyObject_CallNoArgs(dirfunc);
  828. }
  829. else if (!PyErr_Occurred()) {
  830. result = PyDict_Keys(dict);
  831. }
  832. }
  833. else {
  834. PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
  835. }
  836. }
  837. Py_XDECREF(dict);
  838. return result;
  839. }
  840. static PyMethodDef module_methods[] = {
  841. {"__dir__", module_dir, METH_NOARGS,
  842. PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
  843. {0}
  844. };
  845. static PyObject *
  846. module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
  847. {
  848. PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
  849. if (dict == NULL) {
  850. return NULL;
  851. }
  852. if (!PyDict_Check(dict)) {
  853. PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
  854. Py_DECREF(dict);
  855. return NULL;
  856. }
  857. PyObject *annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
  858. if (annotations) {
  859. Py_INCREF(annotations);
  860. }
  861. else if (!PyErr_Occurred()) {
  862. annotations = PyDict_New();
  863. if (annotations) {
  864. int result = PyDict_SetItem(
  865. dict, &_Py_ID(__annotations__), annotations);
  866. if (result) {
  867. Py_CLEAR(annotations);
  868. }
  869. }
  870. }
  871. Py_DECREF(dict);
  872. return annotations;
  873. }
  874. static int
  875. module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
  876. {
  877. int ret = -1;
  878. PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
  879. if (dict == NULL) {
  880. return -1;
  881. }
  882. if (!PyDict_Check(dict)) {
  883. PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
  884. goto exit;
  885. }
  886. if (value != NULL) {
  887. /* set */
  888. ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
  889. }
  890. else {
  891. /* delete */
  892. ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
  893. if (ret < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
  894. PyErr_SetString(PyExc_AttributeError, "__annotations__");
  895. }
  896. }
  897. exit:
  898. Py_DECREF(dict);
  899. return ret;
  900. }
  901. static PyGetSetDef module_getsets[] = {
  902. {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
  903. {NULL}
  904. };
  905. PyTypeObject PyModule_Type = {
  906. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  907. "module", /* tp_name */
  908. sizeof(PyModuleObject), /* tp_basicsize */
  909. 0, /* tp_itemsize */
  910. (destructor)module_dealloc, /* tp_dealloc */
  911. 0, /* tp_vectorcall_offset */
  912. 0, /* tp_getattr */
  913. 0, /* tp_setattr */
  914. 0, /* tp_as_async */
  915. (reprfunc)module_repr, /* tp_repr */
  916. 0, /* tp_as_number */
  917. 0, /* tp_as_sequence */
  918. 0, /* tp_as_mapping */
  919. 0, /* tp_hash */
  920. 0, /* tp_call */
  921. 0, /* tp_str */
  922. (getattrofunc)_Py_module_getattro, /* tp_getattro */
  923. PyObject_GenericSetAttr, /* tp_setattro */
  924. 0, /* tp_as_buffer */
  925. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  926. Py_TPFLAGS_BASETYPE, /* tp_flags */
  927. module___init____doc__, /* tp_doc */
  928. (traverseproc)module_traverse, /* tp_traverse */
  929. (inquiry)module_clear, /* tp_clear */
  930. 0, /* tp_richcompare */
  931. offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
  932. 0, /* tp_iter */
  933. 0, /* tp_iternext */
  934. module_methods, /* tp_methods */
  935. module_members, /* tp_members */
  936. module_getsets, /* tp_getset */
  937. 0, /* tp_base */
  938. 0, /* tp_dict */
  939. 0, /* tp_descr_get */
  940. 0, /* tp_descr_set */
  941. offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
  942. module___init__, /* tp_init */
  943. 0, /* tp_alloc */
  944. new_module, /* tp_new */
  945. PyObject_GC_Del, /* tp_free */
  946. };