1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Module object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2017-09-08 02:51:28 -03:00
|
|
|
#include "internal/pystate.h"
|
2001-08-02 01:15:00 -03:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static Py_ssize_t max_module_number;
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *md_dict;
|
|
|
|
struct PyModuleDef *md_def;
|
|
|
|
void *md_state;
|
2013-07-31 18:14:08 -03:00
|
|
|
PyObject *md_weaklist;
|
|
|
|
PyObject *md_name; /* for logging purposes after md_dict is cleared */
|
1997-05-02 00:12:38 -03:00
|
|
|
} PyModuleObject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2001-10-21 19:28:58 -03:00
|
|
|
static PyMemberDef module_members[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
|
|
|
|
{0}
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
PyTypeObject PyModuleDef_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"moduledef", /* tp_name */
|
|
|
|
sizeof(struct PyModuleDef), /* tp_size */
|
|
|
|
0, /* tp_itemsize */
|
2008-06-11 02:26:20 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-23 09:24:10 -03: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 14:56:58 -03:00
|
|
|
static int
|
2013-07-31 18:14:08 -03:00
|
|
|
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
|
|
|
|
PyObject *name, PyObject *doc)
|
2013-05-04 14:56:58 -03:00
|
|
|
{
|
2014-04-24 20:39:18 -03:00
|
|
|
_Py_IDENTIFIER(__name__);
|
|
|
|
_Py_IDENTIFIER(__doc__);
|
|
|
|
_Py_IDENTIFIER(__package__);
|
|
|
|
_Py_IDENTIFIER(__loader__);
|
|
|
|
_Py_IDENTIFIER(__spec__);
|
2015-03-18 16:53:15 -03:00
|
|
|
|
2013-05-04 14:56:58 -03:00
|
|
|
if (md_dict == NULL)
|
|
|
|
return -1;
|
|
|
|
if (doc == NULL)
|
|
|
|
doc = Py_None;
|
|
|
|
|
2014-04-24 20:39:18 -03:00
|
|
|
if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2014-04-24 20:39:18 -03:00
|
|
|
if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2014-04-24 20:39:18 -03:00
|
|
|
if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2014-04-24 20:39:18 -03:00
|
|
|
if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2014-04-24 20:39:18 -03:00
|
|
|
if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
|
2013-11-22 12:05:39 -04:00
|
|
|
return -1;
|
2013-07-31 18:14:08 -03:00
|
|
|
if (PyUnicode_CheckExact(name)) {
|
|
|
|
Py_INCREF(name);
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(mod->md_name, name);
|
2013-07-31 18:14:08 -03:00
|
|
|
}
|
2013-05-04 14:56:58 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2011-03-04 08:57:07 -04:00
|
|
|
PyModule_NewObject(PyObject *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03: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 18:14:08 -03:00
|
|
|
m->md_weaklist = NULL;
|
|
|
|
m->md_name = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
m->md_dict = PyDict_New();
|
2013-07-31 18:14:08 -03:00
|
|
|
if (module_init_dict(m, m->md_dict, name, NULL) != 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
goto fail;
|
|
|
|
PyObject_GC_Track(m);
|
|
|
|
return (PyObject *)m;
|
1993-11-17 18:58:56 -04:00
|
|
|
|
|
|
|
fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2011-03-04 08:57:07 -04: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 09:24:10 -03: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 08:57:07 -04:00
|
|
|
|
2016-08-21 04:41:56 -03: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 02:26:20 -03:00
|
|
|
PyObject *
|
|
|
|
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
|
2017-09-14 15:18:12 -03: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 02:26:20 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
const char* name;
|
|
|
|
PyModuleObject *m;
|
2017-09-14 15:18:12 -03:00
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
if (!PyModuleDef_Init(module))
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
name = module->m_name;
|
2015-05-23 09:24:10 -03: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 12:52:27 -03: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 04:25:54 -04:00
|
|
|
const char *p = strrchr(_Py_PackageContext, '.');
|
2010-05-09 12:52:27 -03: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 09:24:10 -03:00
|
|
|
if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
|
2012-07-19 15:45:43 -03:00
|
|
|
Py_DECREF(m);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2012-07-19 15:45:43 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (module->m_doc != NULL) {
|
2015-05-23 09:24:10 -03:00
|
|
|
if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
|
2012-07-19 15:45:43 -03:00
|
|
|
Py_DECREF(m);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m->md_def = module;
|
|
|
|
return (PyObject*)m;
|
2008-06-11 02:26:20 -03:00
|
|
|
}
|
|
|
|
|
2015-05-23 09:24:10 -03: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 04:16:47 -04:00
|
|
|
const char *name;
|
2015-05-23 09:24:10 -03: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 04:41:56 -03:00
|
|
|
m = PyModule_NewObject(nameobj);
|
2015-05-23 09:24:10 -03: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 04:41:56 -03:00
|
|
|
ret = _add_methods_to_object(m, nameobj, def->m_methods);
|
2015-05-23 09:24:10 -03: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-23 12:03:46 -03:00
|
|
|
Py_DECREF(nameobj);
|
2015-05-23 09:24:10 -03: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 04:41:56 -03:00
|
|
|
if (def->m_size >= 0) {
|
2015-05-23 09:24:10 -03: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 17:14:44 -03:00
|
|
|
/* handled in PyModule_FromDefAndSpec2 */
|
2015-05-23 09:24:10 -03: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 04:41:56 -03:00
|
|
|
int res;
|
|
|
|
PyObject *name = PyModule_GetNameObject(m);
|
2015-05-23 09:24:10 -03:00
|
|
|
if (name == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-21 04:41:56 -03:00
|
|
|
res = _add_methods_to_object(m, name, functions);
|
2015-05-23 09:24:10 -03:00
|
|
|
Py_DECREF(name);
|
2016-08-21 04:41:56 -03:00
|
|
|
return res;
|
2015-05-23 09:24:10 -03: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 02:26:20 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyModule_GetDict(PyObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *d;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
d = ((PyModuleObject *)m) -> md_dict;
|
2016-08-19 06:00:13 -03:00
|
|
|
assert(d != NULL);
|
2010-05-09 12:52:27 -03:00
|
|
|
return d;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2011-02-22 20:21:43 -04:00
|
|
|
PyObject*
|
|
|
|
PyModule_GetNameObject(PyObject *m)
|
1990-10-26 12:00:11 -03:00
|
|
|
{
|
2014-04-24 20:39:18 -03:00
|
|
|
_Py_IDENTIFIER(__name__);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *d;
|
2011-02-22 20:21:43 -04:00
|
|
|
PyObject *name;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
2014-04-24 20:39:18 -03:00
|
|
|
(name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
|
2011-02-22 20:21:43 -04:00
|
|
|
!PyUnicode_Check(name))
|
2010-05-09 12:52:27 -03:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_SystemError, "nameless module");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-22 20:21:43 -04: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 03:13:07 -04:00
|
|
|
return PyUnicode_AsUTF8(name);
|
1990-10-26 12:00:11 -03:00
|
|
|
}
|
|
|
|
|
2010-08-17 20:37:11 -03:00
|
|
|
PyObject*
|
|
|
|
PyModule_GetFilenameObject(PyObject *m)
|
1999-02-15 10:47:16 -04:00
|
|
|
{
|
2014-04-24 20:39:18 -03:00
|
|
|
_Py_IDENTIFIER(__file__);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *d;
|
|
|
|
PyObject *fileobj;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
2014-04-24 20:39:18 -03:00
|
|
|
(fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
|
2010-05-09 12:52:27 -03:00
|
|
|
!PyUnicode_Check(fileobj))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_SystemError, "module filename missing");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-17 20:37:11 -03:00
|
|
|
Py_INCREF(fileobj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return fileobj;
|
2010-05-06 21:50:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
PyModule_GetFilename(PyObject *m)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *fileobj;
|
2016-11-20 04:16:47 -04:00
|
|
|
const char *utf8;
|
2010-08-17 20:37:11 -03:00
|
|
|
fileobj = PyModule_GetFilenameObject(m);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (fileobj == NULL)
|
|
|
|
return NULL;
|
2016-11-20 03:13:07 -04:00
|
|
|
utf8 = PyUnicode_AsUTF8(fileobj);
|
2011-02-22 20:21:43 -04:00
|
|
|
Py_DECREF(fileobj); /* module dict has still a reference */
|
2010-08-17 20:37:11 -03:00
|
|
|
return utf8;
|
1999-02-15 10:47:16 -04:00
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
PyModuleDef*
|
|
|
|
PyModule_GetDef(PyObject* m)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyModuleObject *)m)->md_def;
|
2008-06-11 02:26:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
PyModule_GetState(PyObject* m)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyModuleObject *)m)->md_state;
|
2008-06-11 02:26:20 -03:00
|
|
|
}
|
|
|
|
|
1998-02-19 16:51:52 -04:00
|
|
|
void
|
2000-07-09 03:03:25 -03:00
|
|
|
_PyModule_Clear(PyObject *m)
|
2014-02-10 12:21:34 -04:00
|
|
|
{
|
|
|
|
PyObject *d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d != NULL)
|
|
|
|
_PyModule_ClearDict(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyModule_ClearDict(PyObject *d)
|
1998-02-19 16:51:52 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03: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 15:38:11 -03:00
|
|
|
if (PyUnicode_READ_CHAR(key, 0) == '_' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ_CHAR(key, 1) != '_') {
|
2010-05-18 21:03:09 -03:00
|
|
|
if (Py_VerboseFlag > 1) {
|
2016-11-20 03:13:07 -04:00
|
|
|
const char *s = PyUnicode_AsUTF8(key);
|
2010-05-18 21:03:09 -03:00
|
|
|
if (s != NULL)
|
|
|
|
PySys_WriteStderr("# clear[1] %s\n", s);
|
|
|
|
else
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2014-02-12 03:55:01 -04:00
|
|
|
if (PyDict_SetItem(d, key, Py_None) != 0)
|
|
|
|
PyErr_Clear();
|
2010-05-09 12:52:27 -03: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 02:41:54 -03:00
|
|
|
if (PyUnicode_READ_CHAR(key, 0) != '_' ||
|
2016-11-16 04:17:58 -04:00
|
|
|
!_PyUnicode_EqualToASCIIString(key, "__builtins__"))
|
2010-05-18 21:03:09 -03:00
|
|
|
{
|
|
|
|
if (Py_VerboseFlag > 1) {
|
2016-11-20 03:13:07 -04:00
|
|
|
const char *s = PyUnicode_AsUTF8(key);
|
2010-05-18 21:03:09 -03:00
|
|
|
if (s != NULL)
|
|
|
|
PySys_WriteStderr("# clear[2] %s\n", s);
|
|
|
|
else
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2014-02-12 03:55:01 -04:00
|
|
|
if (PyDict_SetItem(d, key, Py_None) != 0)
|
|
|
|
PyErr_Clear();
|
2010-05-09 12:52:27 -03: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 16:51:52 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-19 03:51:07 -03: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 09:07:46 -03:00
|
|
|
/* Methods */
|
|
|
|
|
2017-03-19 03:51:07 -03: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 01:15:00 -03:00
|
|
|
static int
|
2017-03-19 03:51:07 -03:00
|
|
|
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
|
|
|
|
/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
|
2001-08-02 01:15:00 -03:00
|
|
|
{
|
2017-03-19 03:51:07 -03:00
|
|
|
PyObject *dict = self->md_dict;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL)
|
|
|
|
return -1;
|
2017-03-19 03:51:07 -03:00
|
|
|
self->md_dict = dict;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2017-03-19 03:51:07 -03:00
|
|
|
if (module_init_dict(self, dict, name, doc) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 03:03:25 -03:00
|
|
|
module_dealloc(PyModuleObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_UnTrack(m);
|
2013-07-31 18:14:08 -03: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 12:52:27 -03:00
|
|
|
if (m->md_def && m->md_def->m_free)
|
|
|
|
m->md_def->m_free(m);
|
2013-07-31 18:14:08 -03:00
|
|
|
Py_XDECREF(m->md_dict);
|
|
|
|
Py_XDECREF(m->md_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (m->md_state != NULL)
|
|
|
|
PyMem_FREE(m->md_state);
|
|
|
|
Py_TYPE(m)->tp_free((PyObject *)m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
module_repr(PyModuleObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2013-11-22 12:05:39 -04:00
|
|
|
PyThreadState *tstate = PyThreadState_GET();
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2014-04-24 18:47:47 -03:00
|
|
|
static PyObject*
|
2014-04-24 20:29:23 -03:00
|
|
|
module_getattro(PyModuleObject *m, PyObject *name)
|
2014-04-24 18:47:47 -03:00
|
|
|
{
|
|
|
|
PyObject *attr, *mod_name;
|
2014-04-24 20:29:23 -03:00
|
|
|
attr = PyObject_GenericGetAttr((PyObject *)m, name);
|
|
|
|
if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
|
2014-04-24 18:47:47 -03:00
|
|
|
return attr;
|
|
|
|
PyErr_Clear();
|
2014-04-24 20:29:23 -03:00
|
|
|
if (m->md_dict) {
|
2014-04-24 20:39:18 -03:00
|
|
|
_Py_IDENTIFIER(__name__);
|
|
|
|
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
|
2014-04-24 20:29:23 -03:00
|
|
|
if (mod_name) {
|
2014-04-24 18:47:47 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"module '%U' has no attribute '%U'", mod_name, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-04-24 20:29:23 -03:00
|
|
|
else if (PyErr_Occurred()) {
|
2014-04-24 18:47:47 -03:00
|
|
|
PyErr_Clear();
|
2014-04-24 20:29:23 -03:00
|
|
|
}
|
2014-04-24 18:47:47 -03:00
|
|
|
}
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"module has no attribute '%U'", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-02 11:58:27 -04:00
|
|
|
static int
|
|
|
|
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03: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 11:58:27 -04:00
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static int
|
|
|
|
module_clear(PyModuleObject *m)
|
|
|
|
{
|
2010-05-09 12:52:27 -03: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 02:26:20 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-05-24 13:09:06 -03:00
|
|
|
static PyObject *
|
|
|
|
module_dir(PyObject *self, PyObject *args)
|
|
|
|
{
|
2011-10-14 05:20:37 -03:00
|
|
|
_Py_IDENTIFIER(__dict__);
|
2011-05-24 13:09:06 -03:00
|
|
|
PyObject *result = NULL;
|
2011-10-10 13:11:30 -03:00
|
|
|
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
|
2011-05-24 13:09:06 -03: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 14:46:15 -03:00
|
|
|
PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
|
2011-05-24 13:09:06 -03:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyModule_Type = {
|
2010-05-09 12:52:27 -03: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 20:29:23 -03:00
|
|
|
(getattrofunc)module_getattro, /* tp_getattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
2017-03-19 03:51:07 -03:00
|
|
|
module___init____doc__, /* tp_doc */
|
2010-05-09 12:52:27 -03:00
|
|
|
(traverseproc)module_traverse, /* tp_traverse */
|
|
|
|
(inquiry)module_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
2013-07-31 18:14:08 -03:00
|
|
|
offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
2011-05-24 13:09:06 -03:00
|
|
|
module_methods, /* tp_methods */
|
2010-05-09 12:52:27 -03: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 03:51:07 -03:00
|
|
|
module___init__, /* tp_init */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|