Repository navigation
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
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
436c1b0
Define module constants in PyModuleDef
tiran d93f2da
Add PyModule_AddTypeFromSpec
tiran a1c8240
PyModule_AddNew* helpers
tiran a277939
Rename new API to PyModuleConst_
tiran 22e4f7f
Use Py_mod_constants instead of PyModuleDef.m_constants
tiran 31f8189
Revert "Use Py_mod_constants instead of PyModuleDef.m_constants"
tiran 366e810
Remove PyModuleDef.m_constants again
tiran bb41d82
bpo-42376: New C-APIs to simplify module attribute declaration
tiran 751c560
ULong, private types, add module to call
tiran c34a4df
Add CAPI tests
tiran 3e50f44
Typo
tiran 00236fe
Use single base class feature from bpo-42423
tiran 8458120
It's a strong ref
tiran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove PyModuleDef.m_constants again
- Loading branch information
commit 366e810efce0a95c40bf089d2068ee64ebc8feeb
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -571,6 +571,143 @@ state: | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. versionadded:: 3.9 | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. c:function:: PyTypeeObject * 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 borrowed reference to 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 a borrowed reference to a | ||||||||||||||||||||||||||||||||||||||||||||||
| ``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(void *) | ||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||
| 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_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, posix_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:: union value | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Value of the module constant definition, whose meaning depends on | ||||||||||||||||||||||||||||||||||||||||||||||
| *type*. | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+643
to
+659
Member
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 int constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. versionadded:: 3.10 | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. c:macro:: PyModuleConst_Bool(name, value) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Add an entry for a bool constant. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. 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, c_function) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Add an entry for a constant as returned by *c_function*. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. 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 | ||||||||||||||||||||||||||||||||||||||||||||||
| ^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15687,7 +15687,7 @@ posixmodule_exec(PyObject *m) | |
| return 0; | ||
| } | ||
|
|
||
| static PyModuleConst_Def _posix_constants[] = { | ||
| static PyModuleConst_Def posix_constants[] = { | ||
|
Member
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. How about using another sepeareted PR to do this converation operation? |
||
| #ifdef F_OK | ||
| PyModuleConst_LongMacro(F_OK), | ||
| #endif | ||
|
|
@@ -15769,9 +15769,16 @@ static PyModuleConst_Def _posix_constants[] = { | |
| {NULL, 0}, | ||
| }; | ||
|
|
||
| static int | ||
| posixmodule_init_constants(PyObject *module) | ||
| { | ||
| return PyModule_AddConstants(module, posix_constants); | ||
| } | ||
|
|
||
|
|
||
| static PyModuleDef_Slot posixmodile_slots[] = { | ||
| {Py_mod_exec, posixmodule_exec}, | ||
| {Py_mod_exec, posixmodule_init_constants}, | ||
| {0, NULL} | ||
| }; | ||
|
|
||
|
|
@@ -15785,7 +15792,6 @@ static struct PyModuleDef posixmodule = { | |
| .m_traverse = _posix_traverse, | ||
| .m_clear = _posix_clear, | ||
| .m_free = _posix_free, | ||
| .m_constants = _posix_constants, | ||
| }; | ||
|
|
||
| PyMODINIT_FUNC | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.