-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
gh-86542: New C-APIs to simplify module attribute declaration #23286
Changes from all commits
436c1b0
d93f2da
a1c8240
a277939
22e4f7f
31f8189
366e810
bb41d82
751c560
c34a4df
3e50f44
00236fe
8458120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -571,6 +571,151 @@ state: | |||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.9 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:function:: PyTypeObject * PyModule_AddNewTypeFromSpec(PyObject *module, PyType_Spec *spec, PyObject *base) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Initialize a new type and add it to *module*. | ||||||||||||||||||||||||||||||||||||||||||||||
The function is equivalent to :c:func:`PyType_FromModuleAndSpec` followed | ||||||||||||||||||||||||||||||||||||||||||||||
by :c:func:`PyModule_AddType`. *base* must be either ``NULL``, a single | ||||||||||||||||||||||||||||||||||||||||||||||
type object, or a tuple of types. | ||||||||||||||||||||||||||||||||||||||||||||||
Return ``NULL`` on error; otherwise a ``PyTypeObject *``, which can | ||||||||||||||||||||||||||||||||||||||||||||||
be assigned to a module state object. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:function:: PyObject * PyModule_AddNewException(PyObject *module, const char *name, const char *doc, PyObject *base, PyObject *dict) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Create a new exception and add it to *module*. | ||||||||||||||||||||||||||||||||||||||||||||||
The function is equivalent to :c:func:`PyErr_NewExceptionWithDoc` followed | ||||||||||||||||||||||||||||||||||||||||||||||
by :c:func:`PyModule_AddObjectRef`. The name of the exception object is | ||||||||||||||||||||||||||||||||||||||||||||||
taken from the last component of *name* after dot. | ||||||||||||||||||||||||||||||||||||||||||||||
Return ``NULL`` on error; otherwise ``PyObject *``, which can be assigned | ||||||||||||||||||||||||||||||||||||||||||||||
to a module state object. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:function:: int PyModule_AddConstants(PyObject *module, PyModuleConst_Def *def) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Initialize module constants from a PyModuleConst_Def array. The function | ||||||||||||||||||||||||||||||||||||||||||||||
provides a convenient way to declare module-level constants. | ||||||||||||||||||||||||||||||||||||||||||||||
Return ``-1`` on error, ``0`` on success. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Example:: | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
static PyObject* | ||||||||||||||||||||||||||||||||||||||||||||||
example_call(PyObject *module) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
return PyBytes_FromString("23"); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
#define EXAMPLE_INT 23 | ||||||||||||||||||||||||||||||||||||||||||||||
#define EXAMPLE_STRING "world" | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
static PyModuleConst_Def example_constants[] = { | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_None("none_value"), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_Long("integer", 42), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_ULong("unsigned", 42UL), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_Bool("false_value", 0), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_Bool("true_value", 1), | ||||||||||||||||||||||||||||||||||||||||||||||
#ifdef Py_MATH_PI | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_Double("pi", Py_MATH_PI), | ||||||||||||||||||||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_String("somestring", "Hello"), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_Call("call", example_call), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_LongMacro(EXAMPLE_INT), | ||||||||||||||||||||||||||||||||||||||||||||||
PyModuleConst_StringMacro(EXAMPLE_STRING), | ||||||||||||||||||||||||||||||||||||||||||||||
{NULL}, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
static int | ||||||||||||||||||||||||||||||||||||||||||||||
example_init_constants(PyObject *module) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
return PyModule_AddConstants(module, example_constants); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
static PyModuleDef_Slot example_slots[] = { | ||||||||||||||||||||||||||||||||||||||||||||||
{Py_mod_exec, example_init_constants}, | ||||||||||||||||||||||||||||||||||||||||||||||
{0, NULL} | ||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:type:: PyModuleConst_Def | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
The values for *type* and the definition of the *value* union are | ||||||||||||||||||||||||||||||||||||||||||||||
internal implementation details. Use any of the ``PyModuleConst_`` macros | ||||||||||||||||||||||||||||||||||||||||||||||
to define entries. The array must be terminated by an entry with name | ||||||||||||||||||||||||||||||||||||||||||||||
set to ``NULL``. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:member:: const char *name | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Attribute name. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:member:: int type | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Attribute type. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:member:: void *value | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Value of the module constant definition, whose meaning depends on | ||||||||||||||||||||||||||||||||||||||||||||||
*type*. | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+643
to
+659
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_None(name) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for None. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_Long(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for an integer constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_ULong(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for an unsigned integer constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_Bool(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for a bool constant. ``0`` is false, ``1`` is true. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_Double(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for a float constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_String(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for a string constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_Call(name, func) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for a constant as returned by callback with signature | ||||||||||||||||||||||||||||||||||||||||||||||
``PyObject* (*func)(PyObject *module)``. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_LongMacro(macro) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for an int constant. The name and the value are taken from | ||||||||||||||||||||||||||||||||||||||||||||||
*macro*. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. c:macro:: PyModuleConst_StringMacro(macro) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Add an entry for a string constant. The name and the value are taken from | ||||||||||||||||||||||||||||||||||||||||||||||
*macro*. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
.. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Module lookup | ||||||||||||||||||||||||||||||||||||||||||||||
^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,15 @@ PyAPI_FUNC(int) PyModule_AddType(PyObject *module, PyTypeObject *type); | |
#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c) | ||
#define PyModule_AddStringMacro(m, c) PyModule_AddStringConstant(m, #c, c) | ||
|
||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000 | ||
/* New in 3.9 */ | ||
PyAPI_FUNC(PyTypeObject *) PyModule_AddNewTypeFromSpec( | ||
PyObject *module, PyType_Spec *spec, PyObject *base); | ||
PyAPI_FUNC(PyObject *) PyModule_AddNewException( | ||
PyObject *module, const char *name, const char *doc, | ||
PyObject *base, PyObject *dict); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you start by adding these two functions in a separated PR, to make this PR shorter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really think that's necessary; the PR is not that big. |
||
#endif | ||
|
||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 | ||
/* New in 3.5 */ | ||
PyAPI_FUNC(int) PyModule_SetDocString(PyObject *, const char *); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,53 @@ typedef struct PyModuleDef_Slot{ | |
|
||
#endif /* New in 3.5 */ | ||
|
||
struct PyModuleConst_Def; | ||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000 | ||
/* New in 3.10 */ | ||
enum _PyModuleConst_type { | ||
_PyModuleConst_none_type = 1, | ||
_PyModuleConst_long_type = 2, | ||
_PyModuleConst_ulong_type = 3, | ||
_PyModuleConst_bool_type = 4, | ||
_PyModuleConst_double_type = 5, | ||
_PyModuleConst_string_type = 6, | ||
_PyModuleConst_call_type = 7, | ||
}; | ||
|
||
typedef struct PyModuleConst_Def { | ||
const char *name; | ||
enum _PyModuleConst_type type; | ||
union { | ||
const char *m_str; | ||
long m_long; | ||
unsigned long m_ulong; | ||
double m_double; | ||
PyObject* (*m_call)(PyObject *module); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how to make this member future-proof in term of stable ABI. We should try to put a max_align_t inside, but this type requires C11. GCC defines it with:
Maybe we can at least put long long and long double:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only issue I found here :) |
||
} value; | ||
} 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_ULong(name, value) \ | ||
{(name), _PyModuleConst_ulong_type, {.m_ulong=(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_str=(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 */ | ||
|
||
typedef struct PyModuleDef{ | ||
PyModuleDef_Base m_base; | ||
const char* m_name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add new functions :c:func:`PyModule_AddConstants`, | ||
:c:func:`PyModule_AddNewTypeFromSpec`, :c:func:`PyModule_AddNewException` to | ||
simplify the declaration of attribute in modules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
static PyObject *
in here.