Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Next Next commit
Define module constants in PyModuleDef
Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran committed Apr 17, 2021
commit 436c1b08ddb4d1593895b80061cfdca5fe47ad0c
37 changes: 37 additions & 0 deletions 37 Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ typedef struct PyModuleDef_Slot{

#endif /* New in 3.5 */

struct PyModuleConstants_Def;
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000
/* New in 3.10 */
#define Py_mc_none 1
#define Py_mc_long 2
#define Py_mc_bool 3
#define Py_mc_double 4
#define Py_mc_string 5
#define Py_mc_call 6
#define Py_mc_type 7

typedef struct PyModuleConstants_Def {
const char *name;
int type;
union {
const char *m_str;
long m_long;
double m_double;
PyObject* (*m_call)(void);
} value;
} PyModuleConstants_Def;

PyAPI_FUNC(int) PyModule_AddConstants(PyObject *, PyModuleConstants_Def *);

#define PyMC_None(name) {(name), Py_mc_none, {.m_long=0}}
#define PyMC_Long(name, value) {(name), Py_mc_long, {.m_long=(value)}}
#define PyMC_Bool(name, value) {(name), Py_mc_bool, {.m_long=(value)}}
#define PyMC_Double(name, value) {(name), Py_mc_double, {.m_double=(value)}}
#define PyMC_String(name, value) {(name), Py_mc_string, {.m_string=(value)}}
#define PyMC_Call(name, value) {(name), Py_mc_call, {.m_call=(value)}}

#define PyMC_LongMacro(m) PyMC_Long(#m, m)
#define PyMC_StringMacro(m) PyMC_String(#m, m)

#endif /* New in 3.10 */

typedef struct PyModuleDef{
PyModuleDef_Base m_base;
const char* m_name;
Expand All @@ -82,6 +118,7 @@ typedef struct PyModuleDef{
traverseproc m_traverse;
inquiry m_clear;
freefunc m_free;
struct PyModuleConstants_Def* m_constants;
} PyModuleDef;


Expand Down
51 changes: 23 additions & 28 deletions 51 Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ m_inf(void)
#endif
}

static PyObject*
m_inf_o(void)
{
return PyFloat_FromDouble(m_inf());
}

/* Constant nan value, generated in the same way as float('nan'). */
/* We don't currently assume that Py_NAN is defined everywhere. */

Expand All @@ -285,6 +291,12 @@ m_nan(void)
#endif
}

static PyObject*
m_nan_o(void)
{
return PyFloat_FromDouble(m_nan());
}

#endif

static double
Expand Down Expand Up @@ -3514,30 +3526,6 @@ math_ulp_impl(PyObject *module, double x)
return x2 - x;
}

static int
math_exec(PyObject *module)
{
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
return -1;
}
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
return -1;
}
#endif
return 0;
}

static PyMethodDef math_methods[] = {
{"acos", math_acos, METH_O, math_acos_doc},
{"acosh", math_acosh, METH_O, math_acosh_doc},
Expand Down Expand Up @@ -3595,9 +3583,16 @@ static PyMethodDef math_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyModuleDef_Slot math_slots[] = {
{Py_mod_exec, math_exec},
{0, NULL}
static PyModuleConstants_Def math_constants[] = {
PyMC_Double("pi", Py_MATH_PI),
PyMC_Double("e", Py_MATH_E),
// 2pi
PyMC_Double("tau", Py_MATH_TAU),
PyMC_Call("inf", m_inf_o),
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
PyMC_Call("nan", m_nan_o),
#endif
{NULL, 0},
};

PyDoc_STRVAR(module_doc,
Expand All @@ -3610,7 +3605,7 @@ static struct PyModuleDef mathmodule = {
.m_doc = module_doc,
.m_size = 0,
.m_methods = math_methods,
.m_slots = math_slots,
.m_constants = math_constants,
};

PyMODINIT_FUNC
Expand Down
161 changes: 83 additions & 78 deletions 161 Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14834,84 +14834,6 @@ static PyMethodDef posix_methods[] = {
static int
all_ins(PyObject *m)
{
#ifdef F_OK
if (PyModule_AddIntMacro(m, F_OK)) return -1;
#endif
#ifdef R_OK
if (PyModule_AddIntMacro(m, R_OK)) return -1;
#endif
#ifdef W_OK
if (PyModule_AddIntMacro(m, W_OK)) return -1;
#endif
#ifdef X_OK
if (PyModule_AddIntMacro(m, X_OK)) return -1;
#endif
#ifdef NGROUPS_MAX
if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
#endif
#ifdef TMP_MAX
if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
#endif
#ifdef WCONTINUED
if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
#endif
#ifdef WNOHANG
if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
#endif
#ifdef WUNTRACED
if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
#endif
#ifdef O_RDONLY
if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
#endif
#ifdef O_WRONLY
if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
#endif
#ifdef O_RDWR
if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
#endif
#ifdef O_NDELAY
if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
#endif
#ifdef O_NONBLOCK
if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
#endif
#ifdef O_APPEND
if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
#endif
#ifdef O_DSYNC
if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
#endif
#ifdef O_RSYNC
if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
#endif
#ifdef O_SYNC
if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
#endif
#ifdef O_NOCTTY
if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
#endif
#ifdef O_CREAT
if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
#endif
#ifdef O_EXCL
if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
#endif
#ifdef O_TRUNC
if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
#endif
#ifdef O_BINARY
if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
#endif
#ifdef O_TEXT
if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
#endif
#ifdef O_XATTR
if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
#endif
#ifdef O_LARGEFILE
if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
#endif
#ifndef __GNU__
#ifdef O_SHLOCK
if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Expand Down Expand Up @@ -15765,6 +15687,88 @@ posixmodule_exec(PyObject *m)
return 0;
}

static PyModuleConstants_Def _posix_constants[] = {
#ifdef F_OK
PyMC_LongMacro(F_OK),
#endif
#ifdef R_OK
PyMC_LongMacro(R_OK),
#endif
#ifdef W_OK
PyMC_LongMacro(W_OK),
#endif
#ifdef X_OK
PyMC_LongMacro(X_OK),
#endif
#ifdef NGROUPS_MAX
PyMC_LongMacro(NGROUPS_MAX),
#endif
#ifdef TMP_MAX
PyMC_LongMacro(TMP_MAX),
#endif
#ifdef WCONTINUED
PyMC_LongMacro(WCONTINUED),
#endif
#ifdef WNOHANG
PyMC_LongMacro(WNOHANG),
#endif
#ifdef WUNTRACED
PyMC_LongMacro(WUNTRACED),
#endif
#ifdef O_RDONLY
PyMC_LongMacro(O_RDONLY),
#endif
#ifdef O_WRONLY
PyMC_LongMacro(O_WRONLY),
#endif
#ifdef O_RDWR
PyMC_LongMacro(O_RDWR),
#endif
#ifdef O_NDELAY
PyMC_LongMacro(O_NDELAY),
#endif
#ifdef O_NONBLOCK
PyMC_LongMacro(O_NONBLOCK),
#endif
#ifdef O_APPEND
PyMC_LongMacro(O_APPEND),
#endif
#ifdef O_DSYNC
PyMC_LongMacro(O_DSYNC),
#endif
#ifdef O_RSYNC
PyMC_LongMacro(O_RSYNC),
#endif
#ifdef O_SYNC
PyMC_LongMacro(O_SYNC),
#endif
#ifdef O_NOCTTY
PyMC_LongMacro(O_NOCTTY),
#endif
#ifdef O_CREAT
PyMC_LongMacro(O_CREAT),
#endif
#ifdef O_EXCL
PyMC_LongMacro(O_EXCL),
#endif
#ifdef O_TRUNC
PyMC_LongMacro(O_TRUNC),
#endif
#ifdef O_BINARY
PyMC_LongMacro(O_BINARY),
#endif
#ifdef O_TEXT
PyMC_LongMacro(O_TEXT),
#endif
#ifdef O_XATTR
PyMC_LongMacro(O_XATTR),
#endif
#ifdef O_LARGEFILE
PyMC_LongMacro(O_LARGEFILE),
#endif
{NULL, 0},
};


static PyModuleDef_Slot posixmodile_slots[] = {
{Py_mod_exec, posixmodule_exec},
Expand All @@ -15781,6 +15785,7 @@ static struct PyModuleDef posixmodule = {
.m_traverse = _posix_traverse,
.m_clear = _posix_clear,
.m_free = _posix_free,
.m_constants = _posix_constants,
};

PyMODINIT_FUNC
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.