Skip to content
Closed
Prev Previous commit
Next Next commit
Rename new API to PyModuleConst_
Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran committed Apr 17, 2021
commit a277939afe1038956ea4e9bde20fc3be89e0b69d
55 changes: 32 additions & 23 deletions Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,46 @@ typedef struct PyModuleDef_Slot{

#endif /* New in 3.5 */

struct PyModuleConstants_Def;
struct PyModuleConst_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 {
enum PyModuleConst_type {
PyModuleConst_none_type = 1,
PyModuleConst_long_type = 2,
PyModuleConst_bool_type = 3,
PyModuleConst_double_type = 4,
PyModuleConst_string_type = 5,
PyModuleConst_call_type = 6,
};

typedef struct PyModuleConst_Def {
const char *name;
int type;
enum PyModuleConst_type type;
union {
const char *m_str;
long m_long;
double m_double;
PyObject* (*m_call)(void);
} value;
} 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)
} PyModuleConst_Def;

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

#define PyModuleConst_None(name) \
{(name), PyModuleConst_none_type, {.m_long=0}}
#define PyModuleConst_Long(name, value) \
{(name), PyModuleConst_long_type, {.m_long=(value)}}
#define PyModuleConst_Bool(name, value) \
{(name), PyModuleConst_bool_type, {.m_long=(value)}}
#define PyModuleConst_Double(name, value) \
{(name), PyModuleConst_double_type, {.m_double=(value)}}
#define PyModuleConst_String(name, value) \
{(name), PyModuleConst_string_type, {.m_string=(value)}}
#define PyModuleConst_Call(name, value) \
{(name), PyModuleConst_call_type, {.m_call=(value)}}

#define PyModuleConst_LongMacro(m) PyModuleConst_Long(#m, m)
#define PyModuleConst_StringMacro(m) PyModuleConst_String(#m, m)

#endif /* New in 3.10 */

Expand All @@ -116,7 +125,7 @@ typedef struct PyModuleDef{
traverseproc m_traverse;
inquiry m_clear;
freefunc m_free;
struct PyModuleConstants_Def* m_constants;
struct PyModuleConst_Def* m_constants;
} PyModuleDef;


Expand Down
12 changes: 6 additions & 6 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3583,14 +3583,14 @@ static PyMethodDef math_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyModuleConstants_Def math_constants[] = {
PyMC_Double("pi", Py_MATH_PI),
PyMC_Double("e", Py_MATH_E),
static PyModuleConst_Def math_constants[] = {
PyModuleConst_Double("pi", Py_MATH_PI),
PyModuleConst_Double("e", Py_MATH_E),
// 2pi
PyMC_Double("tau", Py_MATH_TAU),
PyMC_Call("inf", m_inf_o),
PyModuleConst_Double("tau", Py_MATH_TAU),
PyModuleConst_Call("inf", m_inf_o),
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
PyMC_Call("nan", m_nan_o),
PyModuleConst_Call("nan", m_nan_o),
#endif
{NULL, 0},
};
Expand Down
54 changes: 27 additions & 27 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15687,84 +15687,84 @@ posixmodule_exec(PyObject *m)
return 0;
}

static PyModuleConstants_Def _posix_constants[] = {
static PyModuleConst_Def _posix_constants[] = {
#ifdef F_OK
PyMC_LongMacro(F_OK),
PyModuleConst_LongMacro(F_OK),
#endif
#ifdef R_OK
PyMC_LongMacro(R_OK),
PyModuleConst_LongMacro(R_OK),
#endif
#ifdef W_OK
PyMC_LongMacro(W_OK),
PyModuleConst_LongMacro(W_OK),
#endif
#ifdef X_OK
PyMC_LongMacro(X_OK),
PyModuleConst_LongMacro(X_OK),
#endif
#ifdef NGROUPS_MAX
PyMC_LongMacro(NGROUPS_MAX),
PyModuleConst_LongMacro(NGROUPS_MAX),
#endif
#ifdef TMP_MAX
PyMC_LongMacro(TMP_MAX),
PyModuleConst_LongMacro(TMP_MAX),
#endif
#ifdef WCONTINUED
PyMC_LongMacro(WCONTINUED),
PyModuleConst_LongMacro(WCONTINUED),
#endif
#ifdef WNOHANG
PyMC_LongMacro(WNOHANG),
PyModuleConst_LongMacro(WNOHANG),
#endif
#ifdef WUNTRACED
PyMC_LongMacro(WUNTRACED),
PyModuleConst_LongMacro(WUNTRACED),
#endif
#ifdef O_RDONLY
PyMC_LongMacro(O_RDONLY),
PyModuleConst_LongMacro(O_RDONLY),
#endif
#ifdef O_WRONLY
PyMC_LongMacro(O_WRONLY),
PyModuleConst_LongMacro(O_WRONLY),
#endif
#ifdef O_RDWR
PyMC_LongMacro(O_RDWR),
PyModuleConst_LongMacro(O_RDWR),
#endif
#ifdef O_NDELAY
PyMC_LongMacro(O_NDELAY),
PyModuleConst_LongMacro(O_NDELAY),
#endif
#ifdef O_NONBLOCK
PyMC_LongMacro(O_NONBLOCK),
PyModuleConst_LongMacro(O_NONBLOCK),
#endif
#ifdef O_APPEND
PyMC_LongMacro(O_APPEND),
PyModuleConst_LongMacro(O_APPEND),
#endif
#ifdef O_DSYNC
PyMC_LongMacro(O_DSYNC),
PyModuleConst_LongMacro(O_DSYNC),
#endif
#ifdef O_RSYNC
PyMC_LongMacro(O_RSYNC),
PyModuleConst_LongMacro(O_RSYNC),
#endif
#ifdef O_SYNC
PyMC_LongMacro(O_SYNC),
PyModuleConst_LongMacro(O_SYNC),
#endif
#ifdef O_NOCTTY
PyMC_LongMacro(O_NOCTTY),
PyModuleConst_LongMacro(O_NOCTTY),
#endif
#ifdef O_CREAT
PyMC_LongMacro(O_CREAT),
PyModuleConst_LongMacro(O_CREAT),
#endif
#ifdef O_EXCL
PyMC_LongMacro(O_EXCL),
PyModuleConst_LongMacro(O_EXCL),
#endif
#ifdef O_TRUNC
PyMC_LongMacro(O_TRUNC),
PyModuleConst_LongMacro(O_TRUNC),
#endif
#ifdef O_BINARY
PyMC_LongMacro(O_BINARY),
PyModuleConst_LongMacro(O_BINARY),
#endif
#ifdef O_TEXT
PyMC_LongMacro(O_TEXT),
PyModuleConst_LongMacro(O_TEXT),
#endif
#ifdef O_XATTR
PyMC_LongMacro(O_XATTR),
PyModuleConst_LongMacro(O_XATTR),
#endif
#ifdef O_LARGEFILE
PyMC_LongMacro(O_LARGEFILE),
PyModuleConst_LongMacro(O_LARGEFILE),
#endif
{NULL, 0},
};
Expand Down
22 changes: 11 additions & 11 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
return 0;
}

static int
module_add_constants(PyObject *module, PyModuleConstants_Def *def)
int
PyModule_AddConstants(PyObject *module, PyModuleConst_Def *def)
{
PyObject *dict;
PyModuleConstants_Def *cur_def;
PyModuleConst_Def *cur_def;
PyObject *v;
int res;

Expand All @@ -190,23 +190,23 @@ module_add_constants(PyObject *module, PyModuleConstants_Def *def)

for (cur_def = def; cur_def && cur_def->name; cur_def++) {
switch(cur_def->type) {
case Py_mc_none:
case PyModuleConst_none_type:
v = Py_None;
Py_INCREF(v);
break;
case Py_mc_long:
case PyModuleConst_long_type:
v = PyLong_FromLong(cur_def->value.m_long);
break;
case Py_mc_bool:
case PyModuleConst_bool_type:
v = PyBool_FromLong(cur_def->value.m_long);
break;
case Py_mc_double:
case PyModuleConst_double_type:
v = PyFloat_FromDouble(cur_def->value.m_double);
break;
case Py_mc_string:
case PyModuleConst_string_type:
v = PyUnicode_FromString(cur_def->value.m_str);
break;
case Py_mc_call:
case PyModuleConst_call_type:
v = cur_def->value.m_call();
break;
default:
Expand Down Expand Up @@ -300,7 +300,7 @@ _PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
}
}
if (module->m_constants != NULL) {
if (module_add_constants((PyObject *) m, module->m_constants) != 0) {
if (PyModule_AddConstants((PyObject *) m, module->m_constants) != 0) {
Py_DECREF(m);
return NULL;
}
Expand Down Expand Up @@ -425,7 +425,7 @@ PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api
}

if (def->m_constants != NULL) {
if (module_add_constants((PyObject *) m, def->m_constants) != 0) {
if (PyModule_AddConstants(m, def->m_constants) != 0) {
Py_DECREF(m);
return NULL;
}
Expand Down