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"
|
2021-10-12 03:38:19 -03:00
|
|
|
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
2020-04-14 21:35:41 -03:00
|
|
|
#include "pycore_interp.h" // PyInterpreterState.importlib
|
2023-07-03 06:39:11 -03:00
|
|
|
#include "pycore_modsupport.h" // _PyModule_CreateInitialized()
|
2023-07-03 07:48:50 -03:00
|
|
|
#include "pycore_moduleobject.h" // _PyModule_GetDef()
|
|
|
|
#include "pycore_object.h" // _PyType_AllocNoTrack
|
|
|
|
#include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
|
2023-07-03 06:39:11 -03:00
|
|
|
#include "pycore_pystate.h" // _PyInterpreterState_GET()
|
2023-07-25 10:28:30 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2001-10-21 19:28:58 -03:00
|
|
|
static PyMemberDef module_members[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"__dict__", _Py_T_OBJECT, offsetof(PyModuleObject, md_dict), Py_READONLY},
|
2010-05-09 12:52:27 -03:00
|
|
|
{0}
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
2018-03-17 02:41:20 -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 */
|
2022-02-24 12:51:59 -04:00
|
|
|
sizeof(PyModuleDef), /* tp_basicsize */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_itemsize */
|
2008-06-11 02:26:20 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-18 15:47:13 -04:00
|
|
|
int
|
|
|
|
_PyModule_IsExtension(PyObject *obj)
|
|
|
|
{
|
|
|
|
if (!PyModule_Check(obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PyModuleObject *module = (PyModuleObject*)obj;
|
|
|
|
|
2022-02-24 12:51:59 -04:00
|
|
|
PyModuleDef *def = module->md_def;
|
2021-01-18 15:47:13 -04:00
|
|
|
return (def != NULL && def->m_methods != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
PyObject*
|
2022-02-24 12:51:59 -04:00
|
|
|
PyModuleDef_Init(PyModuleDef* def)
|
2015-05-23 09:24:10 -03:00
|
|
|
{
|
2021-06-23 10:40:27 -03:00
|
|
|
assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
|
2015-05-23 09:24:10 -03:00
|
|
|
if (def->m_base.m_index == 0) {
|
2020-02-06 20:24:29 -04:00
|
|
|
Py_SET_REFCNT(def, 1);
|
2020-02-07 04:17:07 -04:00
|
|
|
Py_SET_TYPE(def, &PyModuleDef_Type);
|
2023-02-15 18:32:31 -04:00
|
|
|
def->m_base.m_index = _PyImport_GetNextModuleIndex();
|
2015-05-23 09:24:10 -03:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2021-06-23 06:00:43 -03:00
|
|
|
assert(md_dict != NULL);
|
2013-05-04 14:56:58 -03:00
|
|
|
if (doc == NULL)
|
|
|
|
doc = Py_None;
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
|
2013-05-04 14:56:58 -03:00
|
|
|
return -1;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyDict_SetItem(md_dict, &_Py_ID(__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)) {
|
2022-11-10 18:58:07 -04:00
|
|
|
Py_XSETREF(mod->md_name, Py_NewRef(name));
|
2013-07-31 18:14:08 -03:00
|
|
|
}
|
2013-05-04 14:56:58 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-23 06:00:43 -03:00
|
|
|
static PyModuleObject *
|
|
|
|
new_module_notrack(PyTypeObject *mt)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyModuleObject *m;
|
2022-03-03 06:38:27 -04:00
|
|
|
m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
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();
|
2021-06-23 06:00:43 -03:00
|
|
|
if (m->md_dict != NULL) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
|
|
|
|
{
|
|
|
|
PyObject *m = (PyObject *)new_module_notrack(mt);
|
|
|
|
if (m != NULL) {
|
|
|
|
PyObject_GC_Track(m);
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyModule_NewObject(PyObject *name)
|
|
|
|
{
|
|
|
|
PyModuleObject *m = new_module_notrack(&PyModule_Type);
|
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
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 *
|
2022-02-24 12:51:59 -04:00
|
|
|
PyModule_Create2(PyModuleDef* module, int module_api_version)
|
2017-09-14 15:18:12 -03:00
|
|
|
{
|
2020-04-14 10:14:01 -03:00
|
|
|
if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
|
2020-01-27 17:37:05 -04:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"Python import machinery not initialized");
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
return _PyModule_CreateInitialized(module, module_api_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
2022-02-24 12:51:59 -04:00
|
|
|
_PyModule_CreateInitialized(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
|
|
|
}
|
2023-02-15 18:32:31 -04:00
|
|
|
name = _PyImport_ResolveNameWithPackageContext(name);
|
2010-05-09 12:52:27 -03:00
|
|
|
if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (module->m_size > 0) {
|
2020-12-01 04:56:42 -04:00
|
|
|
m->md_state = PyMem_Malloc(module->m_size);
|
2010-05-09 12:52:27 -03:00
|
|
|
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 *
|
2022-02-24 12:51:59 -04:00
|
|
|
PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
|
2015-05-23 09:24:10 -03:00
|
|
|
{
|
|
|
|
PyModuleDef_Slot* cur_slot;
|
|
|
|
PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
|
|
|
|
PyObject *nameobj;
|
|
|
|
PyObject *m = NULL;
|
2023-05-05 17:04:55 -03:00
|
|
|
int has_multiple_interpreters_slot = 0;
|
|
|
|
void *multiple_interpreters = (void *)0;
|
2015-05-23 09:24:10 -03:00
|
|
|
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;
|
2023-05-05 18:11:27 -03:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
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++) {
|
2022-10-27 00:20:54 -03:00
|
|
|
switch (cur_slot->slot) {
|
|
|
|
case Py_mod_create:
|
|
|
|
if (create) {
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_SystemError,
|
|
|
|
"module %s has multiple create slots",
|
|
|
|
name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
create = cur_slot->value;
|
|
|
|
break;
|
|
|
|
case Py_mod_exec:
|
|
|
|
has_execution_slots = 1;
|
|
|
|
break;
|
2023-05-05 17:04:55 -03:00
|
|
|
case Py_mod_multiple_interpreters:
|
|
|
|
if (has_multiple_interpreters_slot) {
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_SystemError,
|
|
|
|
"module %s has more than one 'multiple interpreters' slots",
|
|
|
|
name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
multiple_interpreters = cur_slot->value;
|
|
|
|
has_multiple_interpreters_slot = 1;
|
|
|
|
break;
|
2022-10-27 00:20:54 -03:00
|
|
|
default:
|
|
|
|
assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
|
2015-05-23 09:24:10 -03:00
|
|
|
PyErr_Format(
|
|
|
|
PyExc_SystemError,
|
2022-10-27 00:20:54 -03:00
|
|
|
"module %s uses unknown slot ID %i",
|
|
|
|
name, cur_slot->slot);
|
2015-05-23 09:24:10 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:04:55 -03:00
|
|
|
/* By default, multi-phase init modules are expected
|
|
|
|
to work under multiple interpreters. */
|
|
|
|
if (!has_multiple_interpreters_slot) {
|
|
|
|
multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
|
|
|
|
}
|
|
|
|
if (multiple_interpreters == Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED) {
|
|
|
|
if (!_Py_IsMainInterpreter(interp)
|
|
|
|
&& _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 18:57:35 -03:00
|
|
|
else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
|
|
|
&& interp->ceval.own_gil
|
|
|
|
&& !_Py_IsMainInterpreter(interp)
|
|
|
|
&& _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2023-05-05 17:04:55 -03:00
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
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()) {
|
2022-12-23 19:43:19 -04:00
|
|
|
_PyErr_FormatFromCause(
|
|
|
|
PyExc_SystemError,
|
|
|
|
"creation of module %s raised unreported exception",
|
|
|
|
name);
|
2015-05-23 09:24:10 -03:00
|
|
|
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) */
|
2020-12-01 04:56:42 -04:00
|
|
|
md->md_state = PyMem_Malloc(def->m_size);
|
2015-05-23 09:24:10 -03:00
|
|
|
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()) {
|
2022-12-23 19:43:19 -04:00
|
|
|
_PyErr_FormatFromCause(
|
2015-05-23 09:24:10 -03:00
|
|
|
PyExc_SystemError,
|
|
|
|
"execution of module %s raised unreported exception",
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2023-05-05 17:04:55 -03:00
|
|
|
case Py_mod_multiple_interpreters:
|
|
|
|
/* handled in PyModule_FromDefAndSpec2 */
|
|
|
|
break;
|
2015-05-23 09:24:10 -03:00
|
|
|
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;
|
|
|
|
|
|
|
|
v = PyUnicode_FromString(doc);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
|
2015-05-23 09:24:10 -03:00
|
|
|
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
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-06-21 16:50:20 -03:00
|
|
|
return _PyModule_GetDict(m); // borrowed reference
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2011-02-22 20:21:43 -04:00
|
|
|
PyObject*
|
2023-08-23 18:57:40 -03:00
|
|
|
PyModule_GetNameObject(PyObject *mod)
|
1990-10-26 12:00:11 -03:00
|
|
|
{
|
2023-08-23 18:57:40 -03:00
|
|
|
if (!PyModule_Check(mod)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-23 18:57:40 -03:00
|
|
|
PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference
|
|
|
|
if (dict == NULL || !PyDict_Check(dict)) {
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2023-08-23 18:57:40 -03:00
|
|
|
PyObject *name;
|
|
|
|
if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
|
2023-11-14 05:25:39 -04:00
|
|
|
// error or not found
|
2023-08-23 18:57:40 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!PyUnicode_Check(name)) {
|
|
|
|
Py_DECREF(name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "nameless module");
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-02-22 20:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
PyModule_GetName(PyObject *m)
|
|
|
|
{
|
|
|
|
PyObject *name = PyModule_GetNameObject(m);
|
2022-04-01 21:56:30 -03:00
|
|
|
if (name == NULL) {
|
2011-02-22 20:21:43 -04:00
|
|
|
return NULL;
|
2022-04-01 21:56:30 -03:00
|
|
|
}
|
|
|
|
assert(Py_REFCNT(name) >= 2);
|
2011-02-22 20:21:43 -04:00
|
|
|
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*
|
2023-08-23 18:57:40 -03:00
|
|
|
PyModule_GetFilenameObject(PyObject *mod)
|
1999-02-15 10:47:16 -04:00
|
|
|
{
|
2023-08-23 18:57:40 -03:00
|
|
|
if (!PyModule_Check(mod)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-23 18:57:40 -03:00
|
|
|
PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference
|
|
|
|
if (dict == NULL) {
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2023-08-23 18:57:40 -03:00
|
|
|
PyObject *fileobj;
|
|
|
|
if (PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj) <= 0) {
|
2023-11-14 05:25:39 -04:00
|
|
|
// error or not found
|
2023-08-23 18:57:40 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!PyUnicode_Check(fileobj)) {
|
|
|
|
Py_DECREF(fileobj);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return fileobj;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "module filename missing");
|
|
|
|
}
|
|
|
|
return NULL;
|
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;
|
|
|
|
}
|
2021-04-21 19:52:52 -03:00
|
|
|
return _PyModule_GetDef(m);
|
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;
|
|
|
|
}
|
2021-04-21 19:52:52 -03:00
|
|
|
return _PyModule_GetState(m);
|
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;
|
|
|
|
|
2020-04-12 22:04:28 -03:00
|
|
|
int verbose = _Py_GetConfig()->verbose;
|
2019-05-14 12:34:56 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* 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) != '_') {
|
2019-05-14 12:34:56 -03:00
|
|
|
if (verbose > 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();
|
|
|
|
}
|
2018-04-29 16:16:30 -03:00
|
|
|
if (PyDict_SetItem(d, key, Py_None) != 0) {
|
2023-11-02 06:16:34 -03:00
|
|
|
PyErr_FormatUnraisable("Exception ignored on clearing module dict");
|
2018-04-29 16:16:30 -03:00
|
|
|
}
|
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
|
|
|
{
|
2019-05-14 12:34:56 -03:00
|
|
|
if (verbose > 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();
|
|
|
|
}
|
2018-04-29 16:16:30 -03:00
|
|
|
if (PyDict_SetItem(d, key, Py_None) != 0) {
|
2023-11-02 06:16:34 -03:00
|
|
|
PyErr_FormatUnraisable("Exception ignored on clearing module dict");
|
2018-04-29 16:16:30 -03:00
|
|
|
}
|
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
|
|
|
{
|
2020-04-12 22:04:28 -03:00
|
|
|
int verbose = _Py_GetConfig()->verbose;
|
2019-05-14 12:34:56 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_UnTrack(m);
|
2019-05-14 12:34:56 -03:00
|
|
|
if (verbose && m->md_name) {
|
2019-10-08 07:46:17 -03:00
|
|
|
PySys_FormatStderr("# destroy %U\n", m->md_name);
|
2013-07-31 18:14:08 -03:00
|
|
|
}
|
|
|
|
if (m->md_weaklist != NULL)
|
|
|
|
PyObject_ClearWeakRefs((PyObject *) m);
|
2020-03-17 14:09:46 -03:00
|
|
|
/* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
|
|
|
|
if (m->md_def && m->md_def->m_free
|
|
|
|
&& (m->md_def->m_size <= 0 || m->md_state != NULL))
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
m->md_def->m_free(m);
|
2020-03-17 14:09:46 -03:00
|
|
|
}
|
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)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(m->md_state);
|
2010-05-09 12:52:27 -03:00
|
|
|
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
|
|
|
{
|
2020-04-14 10:14:01 -03:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
2023-02-15 18:32:31 -04:00
|
|
|
return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2018-10-30 08:19:51 -03:00
|
|
|
/* Check if the "_initializing" attribute of the module spec is set to true.
|
|
|
|
Clear the exception and return 0 if spec is NULL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_PyModuleSpec_IsInitializing(PyObject *spec)
|
|
|
|
{
|
|
|
|
if (spec != NULL) {
|
2023-05-04 11:50:26 -03:00
|
|
|
PyObject *value;
|
2023-07-12 02:57:10 -03:00
|
|
|
int ok = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
|
2023-05-04 11:50:26 -03:00
|
|
|
if (ok == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-30 08:19:51 -03:00
|
|
|
if (value != NULL) {
|
|
|
|
int initializing = PyObject_IsTrue(value);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (initializing >= 0) {
|
|
|
|
return initializing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-24 19:44:46 -03:00
|
|
|
/* Check if the submodule name is in the "_uninitialized_submodules" attribute
|
|
|
|
of the module spec.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
|
|
|
|
{
|
|
|
|
if (spec == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules));
|
2021-07-24 19:44:46 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_uninitialized = PySequence_Contains(value, name);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (is_uninitialized == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return is_uninitialized;
|
|
|
|
}
|
|
|
|
|
2023-05-04 11:50:26 -03:00
|
|
|
PyObject*
|
|
|
|
_Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
|
2014-04-24 18:47:47 -03:00
|
|
|
{
|
2023-05-04 11:50:26 -03:00
|
|
|
// When suppress=1, this function suppresses AttributeError.
|
2017-12-14 06:59:44 -04:00
|
|
|
PyObject *attr, *mod_name, *getattr;
|
2023-05-04 11:50:26 -03:00
|
|
|
attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
|
|
|
|
if (attr) {
|
2014-04-24 18:47:47 -03:00
|
|
|
return attr;
|
2017-12-14 06:59:44 -04:00
|
|
|
}
|
2023-05-04 11:50:26 -03:00
|
|
|
if (suppress == 1) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
// pass up non-AttributeError exception
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
// pass up non-AttributeError exception
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2021-06-23 06:00:43 -03:00
|
|
|
assert(m->md_dict != NULL);
|
2023-11-14 05:25:39 -04:00
|
|
|
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-23 06:00:43 -03:00
|
|
|
if (getattr) {
|
2023-05-04 11:50:26 -03:00
|
|
|
PyObject *result = PyObject_CallOneArg(getattr, name);
|
|
|
|
if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
// suppress AttributeError
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2023-11-14 05:25:39 -04:00
|
|
|
Py_DECREF(getattr);
|
2023-05-04 11:50:26 -03:00
|
|
|
return result;
|
2021-06-23 06:00:43 -03:00
|
|
|
}
|
2023-11-14 05:25:39 -04:00
|
|
|
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
|
2021-06-23 06:00:43 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (mod_name && PyUnicode_Check(mod_name)) {
|
2023-11-14 05:25:39 -04:00
|
|
|
PyObject *spec;
|
|
|
|
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
|
2018-10-30 08:19:51 -03:00
|
|
|
Py_DECREF(mod_name);
|
2014-04-24 18:47:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-05-04 11:50:26 -03:00
|
|
|
if (suppress != 1) {
|
|
|
|
if (_PyModuleSpec_IsInitializing(spec)) {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"partially initialized "
|
|
|
|
"module '%U' has no attribute '%U' "
|
|
|
|
"(most likely due to a circular import)",
|
|
|
|
mod_name, name);
|
|
|
|
}
|
|
|
|
else if (_PyModuleSpec_IsUninitializedSubmodule(spec, name)) {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"cannot access submodule '%U' of module '%U' "
|
|
|
|
"(most likely due to a circular import)",
|
|
|
|
name, mod_name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"module '%U' has no attribute '%U'",
|
|
|
|
mod_name, name);
|
|
|
|
}
|
2021-06-23 06:00:43 -03:00
|
|
|
}
|
2023-11-14 05:25:39 -04:00
|
|
|
Py_XDECREF(spec);
|
2021-06-23 06:00:43 -03:00
|
|
|
Py_DECREF(mod_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-11-14 05:25:39 -04:00
|
|
|
Py_XDECREF(mod_name);
|
2023-05-04 11:50:26 -03:00
|
|
|
if (suppress != 1) {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"module has no attribute '%U'", name);
|
|
|
|
}
|
2014-04-24 18:47:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-04 11:50:26 -03:00
|
|
|
|
|
|
|
PyObject*
|
|
|
|
_Py_module_getattro(PyModuleObject *m, PyObject *name)
|
|
|
|
{
|
|
|
|
return _Py_module_getattro_impl(m, name, 0);
|
|
|
|
}
|
|
|
|
|
2001-01-02 11:58:27 -04:00
|
|
|
static int
|
|
|
|
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
|
|
|
|
{
|
2020-03-17 14:09:46 -03:00
|
|
|
/* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
|
|
|
|
if (m->md_def && m->md_def->m_traverse
|
|
|
|
&& (m->md_def->m_size <= 0 || m->md_state != NULL))
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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)
|
|
|
|
{
|
2020-03-17 14:09:46 -03:00
|
|
|
/* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
|
|
|
|
if (m->md_def && m->md_def->m_clear
|
|
|
|
&& (m->md_def->m_size <= 0 || m->md_state != NULL))
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int res = m->md_def->m_clear((PyObject*)m);
|
2019-10-08 07:46:17 -03:00
|
|
|
if (PyErr_Occurred()) {
|
2023-11-02 06:16:34 -03:00
|
|
|
PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
|
|
|
|
m->md_name ? " " : "",
|
|
|
|
m->md_name, "");
|
2019-10-08 07:46:17 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
|
2011-05-24 13:09:06 -03:00
|
|
|
|
|
|
|
if (dict != NULL) {
|
2017-12-14 06:59:44 -04:00
|
|
|
if (PyDict_Check(dict)) {
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
|
2017-12-14 06:59:44 -04:00
|
|
|
if (dirfunc) {
|
2021-10-11 19:42:23 -03:00
|
|
|
result = _PyObject_CallNoArgs(dirfunc);
|
2017-12-14 06:59:44 -04:00
|
|
|
}
|
2019-02-25 11:59:46 -04:00
|
|
|
else if (!PyErr_Occurred()) {
|
2017-12-14 06:59:44 -04:00
|
|
|
result = PyDict_Keys(dict);
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 13:09:06 -03:00
|
|
|
else {
|
Two minor fixes for accessing a module's name. (#25658)
While working on another issue, I noticed two minor nits in the C implementation of the module object. Both are related to getting a module's name.
First, the C function module_dir() (module.__dir__) starts by ensuring the module dict is valid. If the module dict is invalid, it wants to format an exception using the name of the module, which it gets from PyModule_GetName(). However, PyModule_GetName() gets the name of the module from the dict. So getting the name in this circumstance will never succeed.
When module_dir() wants to format the error but can't get the name, it knows that PyModule_GetName() must have already raised an exception. So it leaves that exception alone and returns an error. The end result is that the exception raised here is kind of useless and misleading: dir(module) on a module with no __dict__ raises SystemError("nameless module"). I changed the code to actually raise the exception it wanted to raise, just without a real module name: TypeError("<module>.__dict__ is not a dictionary"). This seems more useful, and would do a better job putting the programmer who encountered this on the right track of figuring out what was going on.
Second, the C API function PyModule_GetNameObject() checks to see if the module has a dict. If m->md_dict is not NULL, it calls _PyDict_GetItemIdWithError(). However, it's possible for m->md_dict to be None. And if you call _PyDict_GetItemIdWithError(Py_None, ...) it will *crash*.
Unfortunately, this crash was due to my own bug in the other branch. Fixing my code made the crash go away. I assert that this is still possible at the API level.
The fix is easy: add a PyDict_Check() to PyModule_GetNameObject().
Unfortunately, I don't know how to add a unit test for this. Having changed module_dir() above, I can't find any other interfaces callable from Python that eventually call PyModule_GetNameObject(). So I don't know how to trick the runtime into reproducing this error.
Since both these changes are minor--each entails only a small edit to only one line--I didn't bother with a news item.
2021-04-30 00:13:25 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
|
2011-05-24 13:09:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
};
|
|
|
|
|
2021-04-30 00:09:08 -03:00
|
|
|
static PyObject *
|
|
|
|
module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
|
2023-07-18 02:56:58 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(dict)) {
|
2021-04-30 00:09:08 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
|
2023-07-18 02:56:58 -03:00
|
|
|
Py_DECREF(dict);
|
2021-04-30 00:09:08 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-11-14 05:25:39 -04:00
|
|
|
PyObject *annotations;
|
|
|
|
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
|
2021-04-30 00:09:08 -03:00
|
|
|
annotations = PyDict_New();
|
|
|
|
if (annotations) {
|
2022-02-08 16:39:07 -04:00
|
|
|
int result = PyDict_SetItem(
|
|
|
|
dict, &_Py_ID(__annotations__), annotations);
|
2021-04-30 00:09:08 -03:00
|
|
|
if (result) {
|
|
|
|
Py_CLEAR(annotations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return annotations;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
|
|
|
|
{
|
2021-04-30 13:26:45 -03:00
|
|
|
int ret = -1;
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
|
2023-07-18 02:56:58 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(dict)) {
|
2021-04-30 00:09:08 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
|
2021-04-30 13:26:45 -03:00
|
|
|
goto exit;
|
2021-04-30 00:09:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value != NULL) {
|
|
|
|
/* set */
|
2022-02-08 16:39:07 -04:00
|
|
|
ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
|
2021-04-30 00:09:08 -03:00
|
|
|
}
|
2023-07-18 02:56:58 -03:00
|
|
|
else {
|
|
|
|
/* delete */
|
|
|
|
ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
|
|
|
|
if (ret < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, "__annotations__");
|
|
|
|
}
|
2021-04-30 00:09:08 -03:00
|
|
|
}
|
|
|
|
|
2021-04-30 13:26:45 -03:00
|
|
|
exit:
|
2023-07-18 02:56:58 -03:00
|
|
|
Py_DECREF(dict);
|
2021-04-30 13:26:45 -03:00
|
|
|
return ret;
|
2021-04-30 00:09:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyGetSetDef module_getsets[] = {
|
|
|
|
{"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
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 */
|
2018-09-10 13:46:08 -03:00
|
|
|
sizeof(PyModuleObject), /* tp_basicsize */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)module_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(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 */
|
2023-05-04 11:50:26 -03:00
|
|
|
(getattrofunc)_Py_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 */
|
2021-04-30 00:09:08 -03:00
|
|
|
module_getsets, /* tp_getset */
|
2010-05-09 12:52:27 -03:00
|
|
|
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 */
|
2021-06-23 06:00:43 -03:00
|
|
|
0, /* tp_alloc */
|
|
|
|
new_module, /* tp_new */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|