| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Module object implementation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-07 23:51:28 -06:00
										 |  |  | #include "internal/pystate.h"
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | #include "structmember.h"
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | static Py_ssize_t max_module_number; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     PyObject *md_dict; | 
					
						
							|  |  |  |     struct PyModuleDef *md_def; | 
					
						
							|  |  |  |     void *md_state; | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     PyObject *md_weaklist; | 
					
						
							|  |  |  |     PyObject *md_name;  /* for logging purposes after md_dict is cleared */ | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | } PyModuleObject; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-21 22:28:58 +00:00
										 |  |  | static PyMemberDef module_members[] = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY}, | 
					
						
							|  |  |  |     {0} | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | PyTypeObject PyModuleDef_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "moduledef",                                /* tp_name */ | 
					
						
							|  |  |  |     sizeof(struct PyModuleDef),                 /* tp_size */ | 
					
						
							|  |  |  |     0,                                          /* tp_itemsize */ | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | PyObject* | 
					
						
							|  |  |  | PyModuleDef_Init(struct PyModuleDef* def) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (PyType_Ready(&PyModuleDef_Type) < 0) | 
					
						
							|  |  |  |          return NULL; | 
					
						
							|  |  |  |     if (def->m_base.m_index == 0) { | 
					
						
							|  |  |  |         max_module_number++; | 
					
						
							|  |  |  |         Py_REFCNT(def) = 1; | 
					
						
							|  |  |  |         Py_TYPE(def) = &PyModuleDef_Type; | 
					
						
							|  |  |  |         def->m_base.m_index = max_module_number; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (PyObject*)def; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  | module_init_dict(PyModuleObject *mod, PyObject *md_dict, | 
					
						
							|  |  |  |                  PyObject *name, PyObject *doc) | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     _Py_IDENTIFIER(__name__); | 
					
						
							|  |  |  |     _Py_IDENTIFIER(__doc__); | 
					
						
							|  |  |  |     _Py_IDENTIFIER(__package__); | 
					
						
							|  |  |  |     _Py_IDENTIFIER(__loader__); | 
					
						
							|  |  |  |     _Py_IDENTIFIER(__spec__); | 
					
						
							| 
									
										
										
										
											2015-03-18 21:53:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  |     if (md_dict == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     if (doc == NULL) | 
					
						
							|  |  |  |         doc = Py_None; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0) | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0) | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0) | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0) | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     if (PyUnicode_CheckExact(name)) { | 
					
						
							|  |  |  |         Py_INCREF(name); | 
					
						
							| 
									
										
										
										
											2016-04-06 09:45:48 +03:00
										 |  |  |         Py_XSETREF(mod->md_name, name); | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-05-04 13:56:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2011-03-04 12:57:07 +00:00
										 |  |  | PyModule_NewObject(PyObject *name) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyModuleObject *m; | 
					
						
							|  |  |  |     m = PyObject_GC_New(PyModuleObject, &PyModule_Type); | 
					
						
							|  |  |  |     if (m == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     m->md_def = NULL; | 
					
						
							|  |  |  |     m->md_state = NULL; | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     m->md_weaklist = NULL; | 
					
						
							|  |  |  |     m->md_name = NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     m->md_dict = PyDict_New(); | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     if (module_init_dict(m, m->md_dict, name, NULL) != 0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     PyObject_GC_Track(m); | 
					
						
							|  |  |  |     return (PyObject *)m; | 
					
						
							| 
									
										
										
										
											1993-11-17 22:58:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |  fail: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(m); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-04 12:57:07 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyModule_New(const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *nameobj, *module; | 
					
						
							|  |  |  |     nameobj = PyUnicode_FromString(name); | 
					
						
							|  |  |  |     if (nameobj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     module = PyModule_NewObject(nameobj); | 
					
						
							|  |  |  |     Py_DECREF(nameobj); | 
					
						
							|  |  |  |     return module; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | /* Check API/ABI version
 | 
					
						
							|  |  |  |  * Issues a warning on mismatch, which is usually not fatal. | 
					
						
							|  |  |  |  * Returns 0 if an exception is raised. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | check_api_version(const char *name, int module_api_version) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { | 
					
						
							|  |  |  |         int err; | 
					
						
							|  |  |  |         err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, | 
					
						
							|  |  |  |             "Python C API version mismatch for module %.100s: " | 
					
						
							|  |  |  |             "This Python has API version %d, module %.100s has version %d.", | 
					
						
							|  |  |  |              name, | 
					
						
							|  |  |  |              PYTHON_API_VERSION, name, module_api_version); | 
					
						
							|  |  |  |         if (err) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-04 12:57:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  | static int | 
					
						
							|  |  |  | _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *func; | 
					
						
							|  |  |  |     PyMethodDef *fdef; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (fdef = functions; fdef->ml_name != NULL; fdef++) { | 
					
						
							|  |  |  |         if ((fdef->ml_flags & METH_CLASS) || | 
					
						
							|  |  |  |             (fdef->ml_flags & METH_STATIC)) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                             "module functions cannot set" | 
					
						
							|  |  |  |                             " METH_CLASS or METH_STATIC"); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         func = PyCFunction_NewEx(fdef, (PyObject*)module, name); | 
					
						
							|  |  |  |         if (func == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) { | 
					
						
							|  |  |  |             Py_DECREF(func); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(func); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyModule_Create2(struct PyModuleDef* module, int module_api_version) | 
					
						
							| 
									
										
										
										
											2017-09-14 12:18:12 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!_PyImport_IsInitialized(PyThreadState_GET()->interp)) | 
					
						
							|  |  |  |         Py_FatalError("Python import machinery not initialized"); | 
					
						
							|  |  |  |     return _PyModule_CreateInitialized(module, module_api_version); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | _PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version) | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const char* name; | 
					
						
							|  |  |  |     PyModuleObject *m; | 
					
						
							| 
									
										
										
										
											2017-09-14 12:18:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     if (!PyModuleDef_Init(module)) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     name = module->m_name; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     if (!check_api_version(name, module_api_version)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (module->m_slots) { | 
					
						
							|  |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_SystemError, | 
					
						
							|  |  |  |             "module %s: PyModule_Create is incompatible with m_slots", name); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     /* Make sure name is fully qualified.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        This is a bit of a hack: when the shared library is loaded, | 
					
						
							|  |  |  |        the module name is "package.module", but the module calls | 
					
						
							|  |  |  |        PyModule_Create*() with just "module" for the name.  The shared | 
					
						
							|  |  |  |        library loader squirrels away the true name of the module in | 
					
						
							|  |  |  |        _Py_PackageContext, and PyModule_Create*() will substitute this | 
					
						
							|  |  |  |        (if the name actually matches). | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     if (_Py_PackageContext != NULL) { | 
					
						
							| 
									
										
										
										
											2016-11-21 10:25:54 +02:00
										 |  |  |         const char *p = strrchr(_Py_PackageContext, '.'); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (p != NULL && strcmp(module->m_name, p+1) == 0) { | 
					
						
							|  |  |  |             name = _Py_PackageContext; | 
					
						
							|  |  |  |             _Py_PackageContext = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (module->m_size > 0) { | 
					
						
							|  |  |  |         m->md_state = PyMem_MALLOC(module->m_size); | 
					
						
							|  |  |  |         if (!m->md_state) { | 
					
						
							|  |  |  |             PyErr_NoMemory(); | 
					
						
							|  |  |  |             Py_DECREF(m); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         memset(m->md_state, 0, module->m_size); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (module->m_methods != NULL) { | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) { | 
					
						
							| 
									
										
										
										
											2012-07-19 13:45:43 -05:00
										 |  |  |             Py_DECREF(m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2012-07-19 13:45:43 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (module->m_doc != NULL) { | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) { | 
					
						
							| 
									
										
										
										
											2012-07-19 13:45:43 -05:00
										 |  |  |             Py_DECREF(m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     m->md_def = module; | 
					
						
							|  |  |  |     return (PyObject*)m; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyModuleDef_Slot* cur_slot; | 
					
						
							|  |  |  |     PyObject *(*create)(PyObject *, PyModuleDef*) = NULL; | 
					
						
							|  |  |  |     PyObject *nameobj; | 
					
						
							|  |  |  |     PyObject *m = NULL; | 
					
						
							|  |  |  |     int has_execution_slots = 0; | 
					
						
							| 
									
										
										
										
											2016-11-20 10:16:47 +02:00
										 |  |  |     const char *name; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyModuleDef_Init(def); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     nameobj = PyObject_GetAttrString(spec, "name"); | 
					
						
							|  |  |  |     if (nameobj == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     name = PyUnicode_AsUTF8(nameobj); | 
					
						
							|  |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!check_api_version(name, module_api_version)) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (def->m_size < 0) { | 
					
						
							|  |  |  |         PyErr_Format( | 
					
						
							|  |  |  |             PyExc_SystemError, | 
					
						
							|  |  |  |             "module %s: m_size may not be negative for multi-phase initialization", | 
					
						
							|  |  |  |             name); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { | 
					
						
							|  |  |  |         if (cur_slot->slot == Py_mod_create) { | 
					
						
							|  |  |  |             if (create) { | 
					
						
							|  |  |  |                 PyErr_Format( | 
					
						
							|  |  |  |                     PyExc_SystemError, | 
					
						
							|  |  |  |                     "module %s has multiple create slots", | 
					
						
							|  |  |  |                     name); | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             create = cur_slot->value; | 
					
						
							|  |  |  |         } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) { | 
					
						
							|  |  |  |             PyErr_Format( | 
					
						
							|  |  |  |                 PyExc_SystemError, | 
					
						
							|  |  |  |                 "module %s uses unknown slot ID %i", | 
					
						
							|  |  |  |                 name, cur_slot->slot); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             has_execution_slots = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (create) { | 
					
						
							|  |  |  |         m = create(spec, def); | 
					
						
							|  |  |  |         if (m == NULL) { | 
					
						
							|  |  |  |             if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |                 PyErr_Format( | 
					
						
							|  |  |  |                     PyExc_SystemError, | 
					
						
							|  |  |  |                     "creation of module %s failed without setting an exception", | 
					
						
							|  |  |  |                     name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (PyErr_Occurred()) { | 
					
						
							|  |  |  |                 PyErr_Format(PyExc_SystemError, | 
					
						
							|  |  |  |                             "creation of module %s raised unreported exception", | 
					
						
							|  |  |  |                             name); | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |         m = PyModule_NewObject(nameobj); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         if (m == NULL) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyModule_Check(m)) { | 
					
						
							|  |  |  |         ((PyModuleObject*)m)->md_state = NULL; | 
					
						
							|  |  |  |         ((PyModuleObject*)m)->md_def = def; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) { | 
					
						
							|  |  |  |             PyErr_Format( | 
					
						
							|  |  |  |                 PyExc_SystemError, | 
					
						
							|  |  |  |                 "module %s is not a module object, but requests module state", | 
					
						
							|  |  |  |                 name); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (has_execution_slots) { | 
					
						
							|  |  |  |             PyErr_Format( | 
					
						
							|  |  |  |                 PyExc_SystemError, | 
					
						
							|  |  |  |                 "module %s specifies execution slots, but did not create " | 
					
						
							|  |  |  |                     "a ModuleType instance", | 
					
						
							|  |  |  |                 name); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (def->m_methods != NULL) { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |         ret = _add_methods_to_object(m, nameobj, def->m_methods); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (def->m_doc != NULL) { | 
					
						
							|  |  |  |         ret = PyModule_SetDocString(m, def->m_doc); | 
					
						
							|  |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-24 01:03:46 +10:00
										 |  |  |     Py_DECREF(nameobj); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     return m; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     Py_DECREF(nameobj); | 
					
						
							|  |  |  |     Py_XDECREF(m); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyModule_ExecDef(PyObject *module, PyModuleDef *def) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyModuleDef_Slot *cur_slot; | 
					
						
							|  |  |  |     const char *name; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     name = PyModule_GetName(module); | 
					
						
							|  |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |     if (def->m_size >= 0) { | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |         PyModuleObject *md = (PyModuleObject*)module; | 
					
						
							|  |  |  |         if (md->md_state == NULL) { | 
					
						
							|  |  |  |             /* Always set a state pointer; this serves as a marker to skip
 | 
					
						
							|  |  |  |              * multiple initialization (importlib.reload() is no-op) */ | 
					
						
							|  |  |  |             md->md_state = PyMem_MALLOC(def->m_size); | 
					
						
							|  |  |  |             if (!md->md_state) { | 
					
						
							|  |  |  |                 PyErr_NoMemory(); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             memset(md->md_state, 0, def->m_size); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (def->m_slots == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { | 
					
						
							|  |  |  |         switch (cur_slot->slot) { | 
					
						
							|  |  |  |             case Py_mod_create: | 
					
						
							| 
									
										
										
										
											2016-09-26 23:14:44 +03:00
										 |  |  |                 /* handled in PyModule_FromDefAndSpec2 */ | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case Py_mod_exec: | 
					
						
							|  |  |  |                 ret = ((int (*)(PyObject *))cur_slot->value)(module); | 
					
						
							|  |  |  |                 if (ret != 0) { | 
					
						
							|  |  |  |                     if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |                         PyErr_Format( | 
					
						
							|  |  |  |                             PyExc_SystemError, | 
					
						
							|  |  |  |                             "execution of module %s failed without setting an exception", | 
					
						
							|  |  |  |                             name); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     return -1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (PyErr_Occurred()) { | 
					
						
							|  |  |  |                     PyErr_Format( | 
					
						
							|  |  |  |                         PyExc_SystemError, | 
					
						
							|  |  |  |                         "execution of module %s raised unreported exception", | 
					
						
							|  |  |  |                         name); | 
					
						
							|  |  |  |                     return -1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 PyErr_Format( | 
					
						
							|  |  |  |                     PyExc_SystemError, | 
					
						
							|  |  |  |                     "module %s initialized with unknown slot %i", | 
					
						
							|  |  |  |                     name, cur_slot->slot); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyModule_AddFunctions(PyObject *m, PyMethodDef *functions) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |     int res; | 
					
						
							|  |  |  |     PyObject *name = PyModule_GetNameObject(m); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |     res = _add_methods_to_object(m, name, functions); | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  |     Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2016-08-21 17:41:56 +10:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2015-05-23 22:24:10 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyModule_SetDocString(PyObject *m, const char *doc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *v; | 
					
						
							|  |  |  |     _Py_IDENTIFIER(__doc__); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     v = PyUnicode_FromString(doc); | 
					
						
							|  |  |  |     if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) { | 
					
						
							|  |  |  |         Py_XDECREF(v); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(v); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | PyModule_GetDict(PyObject *m) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *d; | 
					
						
							|  |  |  |     if (!PyModule_Check(m)) { | 
					
						
							|  |  |  |         PyErr_BadInternalCall(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     d = ((PyModuleObject *)m) -> md_dict; | 
					
						
							| 
									
										
										
										
											2016-08-19 12:00:13 +03:00
										 |  |  |     assert(d != NULL); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return d; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  | PyObject* | 
					
						
							|  |  |  | PyModule_GetNameObject(PyObject *m) | 
					
						
							| 
									
										
										
										
											1990-10-26 15:00:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     _Py_IDENTIFIER(__name__); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *d; | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     PyObject *name; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyModule_Check(m)) { | 
					
						
							|  |  |  |         PyErr_BadArgument(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     d = ((PyModuleObject *)m)->md_dict; | 
					
						
							|  |  |  |     if (d == NULL || | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |         (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL || | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |         !PyUnicode_Check(name)) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_SystemError, "nameless module"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     Py_INCREF(name); | 
					
						
							|  |  |  |     return name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char * | 
					
						
							|  |  |  | PyModule_GetName(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *name = PyModule_GetNameObject(m); | 
					
						
							|  |  |  |     if (name == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     Py_DECREF(name);   /* module dict has still a reference */ | 
					
						
							| 
									
										
										
										
											2016-11-20 09:13:07 +02:00
										 |  |  |     return PyUnicode_AsUTF8(name); | 
					
						
							| 
									
										
										
										
											1990-10-26 15:00:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  | PyObject* | 
					
						
							|  |  |  | PyModule_GetFilenameObject(PyObject *m) | 
					
						
							| 
									
										
										
										
											1999-02-15 14:47:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |     _Py_IDENTIFIER(__file__); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *d; | 
					
						
							|  |  |  |     PyObject *fileobj; | 
					
						
							|  |  |  |     if (!PyModule_Check(m)) { | 
					
						
							|  |  |  |         PyErr_BadArgument(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     d = ((PyModuleObject *)m)->md_dict; | 
					
						
							|  |  |  |     if (d == NULL || | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |         (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL || | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         !PyUnicode_Check(fileobj)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_SystemError, "module filename missing"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     Py_INCREF(fileobj); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return fileobj; | 
					
						
							| 
									
										
										
										
											2010-05-07 00:50:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char * | 
					
						
							|  |  |  | PyModule_GetFilename(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *fileobj; | 
					
						
							| 
									
										
										
										
											2016-11-20 10:16:47 +02:00
										 |  |  |     const char *utf8; | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     fileobj = PyModule_GetFilenameObject(m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (fileobj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2016-11-20 09:13:07 +02:00
										 |  |  |     utf8 = PyUnicode_AsUTF8(fileobj); | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     Py_DECREF(fileobj);   /* module dict has still a reference */ | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     return utf8; | 
					
						
							| 
									
										
										
										
											1999-02-15 14:47:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyModuleDef* | 
					
						
							|  |  |  | PyModule_GetDef(PyObject* m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyModule_Check(m)) { | 
					
						
							|  |  |  |         PyErr_BadArgument(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ((PyModuleObject *)m)->md_def; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void* | 
					
						
							|  |  |  | PyModule_GetState(PyObject* m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyModule_Check(m)) { | 
					
						
							|  |  |  |         PyErr_BadArgument(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ((PyModuleObject *)m)->md_state; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-19 20:51:52 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | _PyModule_Clear(PyObject *m) | 
					
						
							| 
									
										
										
										
											2014-02-10 18:21:34 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *d = ((PyModuleObject *)m)->md_dict; | 
					
						
							|  |  |  |     if (d != NULL) | 
					
						
							|  |  |  |         _PyModule_ClearDict(d); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyModule_ClearDict(PyObject *d) | 
					
						
							| 
									
										
										
										
											1998-02-19 20:51:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* To make the execution order of destructors for global
 | 
					
						
							|  |  |  |        objects a bit more predictable, we first zap all objects | 
					
						
							|  |  |  |        whose name starts with a single underscore, before we clear | 
					
						
							|  |  |  |        the entire dictionary.  We zap them by replacing them with | 
					
						
							|  |  |  |        None, rather than deleting them from the dictionary, to | 
					
						
							|  |  |  |        avoid rehashing the dictionary (to some extent). */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t pos; | 
					
						
							|  |  |  |     PyObject *key, *value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* First, clear only names starting with a single underscore */ | 
					
						
							|  |  |  |     pos = 0; | 
					
						
							|  |  |  |     while (PyDict_Next(d, &pos, &key, &value)) { | 
					
						
							|  |  |  |         if (value != Py_None && PyUnicode_Check(key)) { | 
					
						
							| 
									
										
										
										
											2012-04-29 14:38:11 -04:00
										 |  |  |             if (PyUnicode_READ_CHAR(key, 0) == '_' && | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |                 PyUnicode_READ_CHAR(key, 1) != '_') { | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |                 if (Py_VerboseFlag > 1) { | 
					
						
							| 
									
										
										
										
											2016-11-20 09:13:07 +02:00
										 |  |  |                     const char *s = PyUnicode_AsUTF8(key); | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |                     if (s != NULL) | 
					
						
							|  |  |  |                         PySys_WriteStderr("#   clear[1] %s\n", s); | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                         PyErr_Clear(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-02-12 09:55:01 +02:00
										 |  |  |                 if (PyDict_SetItem(d, key, Py_None) != 0) | 
					
						
							|  |  |  |                     PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Next, clear all names except for __builtins__ */ | 
					
						
							|  |  |  |     pos = 0; | 
					
						
							|  |  |  |     while (PyDict_Next(d, &pos, &key, &value)) { | 
					
						
							|  |  |  |         if (value != Py_None && PyUnicode_Check(key)) { | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |             if (PyUnicode_READ_CHAR(key, 0) != '_' || | 
					
						
							| 
									
										
										
										
											2016-11-16 10:17:58 +02:00
										 |  |  |                 !_PyUnicode_EqualToASCIIString(key, "__builtins__")) | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 if (Py_VerboseFlag > 1) { | 
					
						
							| 
									
										
										
										
											2016-11-20 09:13:07 +02:00
										 |  |  |                     const char *s = PyUnicode_AsUTF8(key); | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |                     if (s != NULL) | 
					
						
							|  |  |  |                         PySys_WriteStderr("#   clear[2] %s\n", s); | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                         PyErr_Clear(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-02-12 09:55:01 +02:00
										 |  |  |                 if (PyDict_SetItem(d, key, Py_None) != 0) | 
					
						
							|  |  |  |                     PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Note: we leave __builtins__ in place, so that destructors
 | 
					
						
							|  |  |  |        of non-global objects defined in this module can still use | 
					
						
							|  |  |  |        builtins, in particularly 'None'. */ | 
					
						
							| 
									
										
										
										
											1998-02-19 20:51:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | class module "PyModuleObject *" "&PyModule_Type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "clinic/moduleobject.c.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module.__init__ | 
					
						
							|  |  |  |     name: unicode | 
					
						
							|  |  |  |     doc: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Create a module object. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The name must be a string; the optional doc argument can have any type. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  | module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc) | 
					
						
							|  |  |  | /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/ | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  |     PyObject *dict = self->md_dict; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (dict == NULL) { | 
					
						
							|  |  |  |         dict = PyDict_New(); | 
					
						
							|  |  |  |         if (dict == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  |         self->md_dict = dict; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  |     if (module_init_dict(self, dict, name, doc) < 0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | module_dealloc(PyModuleObject *m) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_GC_UnTrack(m); | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     if (Py_VerboseFlag && m->md_name) { | 
					
						
							|  |  |  |         PySys_FormatStderr("# destroy %S\n", m->md_name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (m->md_weaklist != NULL) | 
					
						
							|  |  |  |         PyObject_ClearWeakRefs((PyObject *) m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (m->md_def && m->md_def->m_free) | 
					
						
							|  |  |  |         m->md_def->m_free(m); | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     Py_XDECREF(m->md_dict); | 
					
						
							|  |  |  |     Py_XDECREF(m->md_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (m->md_state != NULL) | 
					
						
							|  |  |  |         PyMem_FREE(m->md_state); | 
					
						
							|  |  |  |     Py_TYPE(m)->tp_free((PyObject *)m); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | module_repr(PyModuleObject *m) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     PyThreadState *tstate = PyThreadState_GET(); | 
					
						
							|  |  |  |     PyInterpreterState *interp = tstate->interp; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-24 14:47:47 -07:00
										 |  |  | static PyObject* | 
					
						
							| 
									
										
										
										
											2014-04-24 19:29:23 -04:00
										 |  |  | module_getattro(PyModuleObject *m, PyObject *name) | 
					
						
							| 
									
										
										
										
											2014-04-24 14:47:47 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *attr, *mod_name; | 
					
						
							| 
									
										
										
										
											2014-04-24 19:29:23 -04:00
										 |  |  |     attr = PyObject_GenericGetAttr((PyObject *)m, name); | 
					
						
							|  |  |  |     if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) | 
					
						
							| 
									
										
										
										
											2014-04-24 14:47:47 -07:00
										 |  |  |         return attr; | 
					
						
							|  |  |  |     PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2014-04-24 19:29:23 -04:00
										 |  |  |     if (m->md_dict) { | 
					
						
							| 
									
										
										
										
											2014-04-24 19:39:18 -04:00
										 |  |  |         _Py_IDENTIFIER(__name__); | 
					
						
							|  |  |  |         mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); | 
					
						
							| 
									
										
										
										
											2017-09-19 14:23:01 +03:00
										 |  |  |         if (mod_name && PyUnicode_Check(mod_name)) { | 
					
						
							| 
									
										
										
										
											2014-04-24 14:47:47 -07:00
										 |  |  |             PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                         "module '%U' has no attribute '%U'", mod_name, name); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                 "module has no attribute '%U'", name); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-02 15:58:27 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | module_traverse(PyModuleObject *m, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (m->md_def && m->md_def->m_traverse) { | 
					
						
							|  |  |  |         int res = m->md_def->m_traverse((PyObject*)m, visit, arg); | 
					
						
							|  |  |  |         if (res) | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_VISIT(m->md_dict); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2001-01-02 15:58:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | module_clear(PyModuleObject *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (m->md_def && m->md_def->m_clear) { | 
					
						
							|  |  |  |         int res = m->md_def->m_clear((PyObject*)m); | 
					
						
							|  |  |  |         if (res) | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_CLEAR(m->md_dict); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | module_dir(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-14 10:20:37 +02:00
										 |  |  |     _Py_IDENTIFIER(__dict__); | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2011-10-10 18:11:30 +02:00
										 |  |  |     PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (dict != NULL) { | 
					
						
							|  |  |  |         if (PyDict_Check(dict)) | 
					
						
							|  |  |  |             result = PyDict_Keys(dict); | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             const char *name = PyModule_GetName(self); | 
					
						
							|  |  |  |             if (name) | 
					
						
							|  |  |  |                 PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                              "%.200s.__dict__ is not a dictionary", | 
					
						
							|  |  |  |                              name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_XDECREF(dict); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef module_methods[] = { | 
					
						
							|  |  |  |     {"__dir__", module_dir, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2011-05-24 12:46:15 -05:00
										 |  |  |      PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |     {0} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyTypeObject PyModule_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "module",                                   /* tp_name */ | 
					
						
							|  |  |  |     sizeof(PyModuleObject),                     /* tp_size */ | 
					
						
							|  |  |  |     0,                                          /* tp_itemsize */ | 
					
						
							|  |  |  |     (destructor)module_dealloc,                 /* tp_dealloc */ | 
					
						
							|  |  |  |     0,                                          /* tp_print */ | 
					
						
							|  |  |  |     0,                                          /* tp_getattr */ | 
					
						
							|  |  |  |     0,                                          /* tp_setattr */ | 
					
						
							|  |  |  |     0,                                          /* tp_reserved */ | 
					
						
							|  |  |  |     (reprfunc)module_repr,                      /* tp_repr */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_number */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_sequence */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_mapping */ | 
					
						
							|  |  |  |     0,                                          /* tp_hash */ | 
					
						
							|  |  |  |     0,                                          /* tp_call */ | 
					
						
							|  |  |  |     0,                                          /* tp_str */ | 
					
						
							| 
									
										
										
										
											2014-04-24 19:29:23 -04:00
										 |  |  |     (getattrofunc)module_getattro,              /* tp_getattro */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_GenericSetAttr,                    /* tp_setattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_buffer */ | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
					
						
							|  |  |  |         Py_TPFLAGS_BASETYPE,                    /* tp_flags */ | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  |     module___init____doc__,                     /* tp_doc */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     (traverseproc)module_traverse,              /* tp_traverse */ | 
					
						
							|  |  |  |     (inquiry)module_clear,                      /* tp_clear */ | 
					
						
							|  |  |  |     0,                                          /* tp_richcompare */ | 
					
						
							| 
									
										
										
										
											2013-07-31 23:14:08 +02:00
										 |  |  |     offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                                          /* tp_iter */ | 
					
						
							|  |  |  |     0,                                          /* tp_iternext */ | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |     module_methods,                             /* tp_methods */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     module_members,                             /* tp_members */ | 
					
						
							|  |  |  |     0,                                          /* tp_getset */ | 
					
						
							|  |  |  |     0,                                          /* tp_base */ | 
					
						
							|  |  |  |     0,                                          /* tp_dict */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_get */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_set */ | 
					
						
							|  |  |  |     offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */ | 
					
						
							| 
									
										
										
										
											2017-03-19 08:51:07 +02:00
										 |  |  |     module___init__,                            /* tp_init */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyType_GenericAlloc,                        /* tp_alloc */ | 
					
						
							|  |  |  |     PyType_GenericNew,                          /* tp_new */ | 
					
						
							|  |  |  |     PyObject_GC_Del,                            /* tp_free */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | }; |