| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | static PyTypeObject moduledef_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
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     m->md_dict = PyDict_New(); | 
					
						
							| 
									
										
										
										
											2011-03-04 12:57:07 +00:00
										 |  |  |     if (m->md_dict == NULL) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto fail; | 
					
						
							| 
									
										
										
										
											2011-03-04 12:57:07 +00:00
										 |  |  |     if (PyDict_SetItemString(m->md_dict, "__name__", name) != 0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto fail; | 
					
						
							|  |  |  |     if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) | 
					
						
							|  |  |  |         goto fail; | 
					
						
							|  |  |  |     if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0) | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyModule_Create2(struct PyModuleDef* module, int module_api_version) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *d, *v, *n; | 
					
						
							|  |  |  |     PyMethodDef *ml; | 
					
						
							|  |  |  |     const char* name; | 
					
						
							|  |  |  |     PyModuleObject *m; | 
					
						
							| 
									
										
										
										
											2010-12-14 23:06:25 +00:00
										 |  |  |     PyInterpreterState *interp = PyThreadState_Get()->interp; | 
					
						
							|  |  |  |     if (interp->modules == NULL) | 
					
						
							|  |  |  |         Py_FatalError("Python import machinery not initialized"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (PyType_Ready(&moduledef_type) < 0) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (module->m_base.m_index == 0) { | 
					
						
							|  |  |  |         max_module_number++; | 
					
						
							|  |  |  |         Py_REFCNT(module) = 1; | 
					
						
							|  |  |  |         Py_TYPE(module) = &moduledef_type; | 
					
						
							|  |  |  |         module->m_base.m_index = max_module_number; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     name = module->m_name; | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  |     if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { | 
					
						
							| 
									
										
										
										
											2010-08-13 14:03:48 +00:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* 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) { | 
					
						
							|  |  |  |         char *p = strrchr(_Py_PackageContext, '.'); | 
					
						
							|  |  |  |         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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     d = PyModule_GetDict((PyObject*)m); | 
					
						
							|  |  |  |     if (module->m_methods != NULL) { | 
					
						
							|  |  |  |         n = PyUnicode_FromString(name); | 
					
						
							|  |  |  |         if (n == NULL) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         for (ml = module->m_methods; ml->ml_name != NULL; ml++) { | 
					
						
							|  |  |  |             if ((ml->ml_flags & METH_CLASS) || | 
					
						
							|  |  |  |                 (ml->ml_flags & METH_STATIC)) { | 
					
						
							|  |  |  |                 PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                                 "module functions cannot set" | 
					
						
							|  |  |  |                                 " METH_CLASS or METH_STATIC"); | 
					
						
							|  |  |  |                 Py_DECREF(n); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             v = PyCFunction_NewEx(ml, (PyObject*)m, n); | 
					
						
							|  |  |  |             if (v == NULL) { | 
					
						
							|  |  |  |                 Py_DECREF(n); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (PyDict_SetItemString(d, ml->ml_name, v) != 0) { | 
					
						
							|  |  |  |                 Py_DECREF(v); | 
					
						
							|  |  |  |                 Py_DECREF(n); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Py_DECREF(v); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(n); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (module->m_doc != NULL) { | 
					
						
							|  |  |  |         v = PyUnicode_FromString(module->m_doc); | 
					
						
							|  |  |  |         if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) { | 
					
						
							|  |  |  |             Py_XDECREF(v); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(v); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     m->md_def = module; | 
					
						
							|  |  |  |     return (PyObject*)m; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     if (d == NULL) | 
					
						
							|  |  |  |         ((PyModuleObject *)m) -> md_dict = d = PyDict_New(); | 
					
						
							|  |  |  |     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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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 || | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |         (name = PyDict_GetItemString(d, "__name__")) == NULL || | 
					
						
							|  |  |  |         !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 */ | 
					
						
							|  |  |  |     return _PyUnicode_AsString(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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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 || | 
					
						
							|  |  |  |         (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || | 
					
						
							|  |  |  |         !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; | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     char *utf8; | 
					
						
							|  |  |  |     fileobj = PyModule_GetFilenameObject(m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (fileobj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     utf8 = _PyUnicode_AsString(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) | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     PyObject *d; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     d = ((PyModuleObject *)m)->md_dict; | 
					
						
							|  |  |  |     if (d == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* 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)) { | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |             if (PyUnicode_READ_CHAR(key, 0) == '_' &&  | 
					
						
							|  |  |  |                 PyUnicode_READ_CHAR(key, 1) != '_') { | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |                 if (Py_VerboseFlag > 1) { | 
					
						
							|  |  |  |                     const char *s = _PyUnicode_AsString(key); | 
					
						
							|  |  |  |                     if (s != NULL) | 
					
						
							|  |  |  |                         PySys_WriteStderr("#   clear[1] %s\n", s); | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                         PyErr_Clear(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 PyDict_SetItem(d, key, Py_None); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* 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) != '_' || | 
					
						
							|  |  |  |                 PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) | 
					
						
							| 
									
										
										
										
											2010-05-19 00:03:09 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 if (Py_VerboseFlag > 1) { | 
					
						
							|  |  |  |                     const char *s = _PyUnicode_AsString(key); | 
					
						
							|  |  |  |                     if (s != NULL) | 
					
						
							|  |  |  |                         PySys_WriteStderr("#   clear[2] %s\n", s); | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                         PyErr_Clear(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 PyDict_SetItem(d, key, Py_None); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* 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
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2002-06-04 05:52:47 +00:00
										 |  |  | module_init(PyModuleObject *m, PyObject *args, PyObject *kwds) | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     static char *kwlist[] = {"name", "doc", NULL}; | 
					
						
							|  |  |  |     PyObject *dict, *name = Py_None, *doc = Py_None; | 
					
						
							|  |  |  |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__", | 
					
						
							|  |  |  |                                      kwlist, &name, &doc)) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     dict = m->md_dict; | 
					
						
							|  |  |  |     if (dict == NULL) { | 
					
						
							|  |  |  |         dict = PyDict_New(); | 
					
						
							|  |  |  |         if (dict == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         m->md_dict = dict; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyDict_SetItemString(dict, "__name__", name) < 0) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     if (PyDict_SetItemString(dict, "__doc__", doc) < 0) | 
					
						
							|  |  |  |         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); | 
					
						
							|  |  |  |     if (m->md_def && m->md_def->m_free) | 
					
						
							|  |  |  |         m->md_def->m_free(m); | 
					
						
							|  |  |  |     if (m->md_dict != NULL) { | 
					
						
							| 
									
										
										
										
											2010-10-12 22:57:59 +00:00
										 |  |  |         _PyModule_Clear((PyObject *)m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(m->md_dict); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     PyObject *name, *filename, *repr; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     name = PyModule_GetNameObject((PyObject *)m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |         name = PyUnicode_FromStringAndSize("?", 1); | 
					
						
							|  |  |  |         if (name == NULL) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     filename = PyModule_GetFilenameObject((PyObject *)m); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (filename == NULL) { | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |         repr = PyUnicode_FromFormat("<module %R (built-in)>", name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         repr = PyUnicode_FromFormat("<module %R from %R>", name, filename); | 
					
						
							|  |  |  |         Py_DECREF(filename); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-23 00:21:43 +00:00
										 |  |  |     Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2010-08-17 23:37:11 +00:00
										 |  |  |     return repr; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-10 18:11:30 +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} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(module_doc, | 
					
						
							| 
									
										
										
										
											2002-06-04 06:02:35 +00:00
										 |  |  | "module(name[, doc])\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Create a module object.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | The name must be a string; the optional doc argument can have any type."); | 
					
						
							| 
									
										
										
										
											2002-06-04 06:02:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							|  |  |  |     PyObject_GenericGetAttr,                    /* tp_getattro */ | 
					
						
							|  |  |  |     PyObject_GenericSetAttr,                    /* tp_setattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_buffer */ | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
					
						
							|  |  |  |         Py_TPFLAGS_BASETYPE,                    /* tp_flags */ | 
					
						
							|  |  |  |     module_doc,                                 /* tp_doc */ | 
					
						
							|  |  |  |     (traverseproc)module_traverse,              /* tp_traverse */ | 
					
						
							|  |  |  |     (inquiry)module_clear,                      /* tp_clear */ | 
					
						
							|  |  |  |     0,                                          /* tp_richcompare */ | 
					
						
							|  |  |  |     0,                                          /* tp_weaklistoffset */ | 
					
						
							|  |  |  |     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 */ | 
					
						
							|  |  |  |     (initproc)module_init,                      /* tp_init */ | 
					
						
							|  |  |  |     PyType_GenericAlloc,                        /* tp_alloc */ | 
					
						
							|  |  |  |     PyType_GenericNew,                          /* tp_new */ | 
					
						
							|  |  |  |     PyObject_GC_Del,                            /* tp_free */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | }; |