Skip to content

gh-86542: New C-APIs to simplify module attribute declaration #23286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Prev Previous commit
Next Next commit
Use Py_mod_constants instead of PyModuleDef.m_constants
  • Loading branch information
tiran committed Apr 17, 2021
commit 22e4f7f12d5f186a9be4b5444963ebff7b583295
7 changes: 5 additions & 2 deletions Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ typedef struct PyModuleDef_Slot{
#define Py_mod_create 1
#define Py_mod_exec 2

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000
#define Py_mod_constants 3
#endif

#ifndef Py_LIMITED_API
#define _Py_mod_LAST_SLOT 2
#define _Py_mod_LAST_SLOT 3
#endif

#endif /* New in 3.5 */
Expand Down Expand Up @@ -125,7 +129,6 @@ typedef struct PyModuleDef{
traverseproc m_traverse;
inquiry m_clear;
freefunc m_free;
struct PyModuleConst_Def* m_constants;
} PyModuleDef;


Expand Down
7 changes: 6 additions & 1 deletion Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3599,13 +3599,18 @@ PyDoc_STRVAR(module_doc,
"This module provides access to the mathematical functions\n"
"defined by the C standard.");

static PyModuleDef_Slot math_slots[] = {
{Py_mod_constants, math_constants},
{0, NULL}
};

static struct PyModuleDef mathmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "math",
.m_doc = module_doc,
.m_size = 0,
.m_methods = math_methods,
.m_constants = math_constants,
.m_slots = math_slots,
};

PyMODINIT_FUNC
Expand Down
2 changes: 1 addition & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15772,6 +15772,7 @@ static PyModuleConst_Def _posix_constants[] = {

static PyModuleDef_Slot posixmodile_slots[] = {
{Py_mod_exec, posixmodule_exec},
{Py_mod_constants, _posix_constants},
{0, NULL}
};

Expand All @@ -15785,7 +15786,6 @@ static struct PyModuleDef posixmodule = {
.m_traverse = _posix_traverse,
.m_clear = _posix_clear,
.m_free = _posix_free,
.m_constants = _posix_constants,
};

PyMODINIT_FUNC
Expand Down
19 changes: 6 additions & 13 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,6 @@ _PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
return NULL;
}
}
if (module->m_constants != NULL) {
if (PyModule_AddConstants((PyObject *) m, module->m_constants) != 0) {
Py_DECREF(m);
return NULL;
}
}
m->md_def = module;
return (PyObject*)m;
}
Expand Down Expand Up @@ -424,13 +418,6 @@ PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api
}
}

if (def->m_constants != NULL) {
if (PyModule_AddConstants(m, def->m_constants) != 0) {
Py_DECREF(m);
return NULL;
}
}

Py_DECREF(nameobj);
return m;

Expand Down Expand Up @@ -494,6 +481,12 @@ PyModule_ExecDef(PyObject *module, PyModuleDef *def)
return -1;
}
break;
case Py_mod_constants:
ret = PyModule_AddConstants(module, (PyModuleConst_Def *)cur_slot->value);
if (ret == -1) {
return -1;
}
break;
default:
PyErr_Format(
PyExc_SystemError,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy