We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contactedHave 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
| Expand Up | @@ -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 thread
Comment on lines
+643
to
+659
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| .. 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 | ||||||||||||||||||||||||||||||||||||||||||||||
| ^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||
| Expand Down | ||||||||||||||||||||||||||||||||||||||||||||||
| Expand Up | @@ -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); | ||
|
Comment thread
Outdated
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide commentCould you start by adding these two functions in a separated PR, to make this PR shorter?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide commentI don't really think that's necessary; the PR is not that big.
Sorry, something went wrong.
All reactions
|
||
| #endif | ||
|
|
||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 | ||
| /* New in 3.5 */ | ||
| PyAPI_FUNC(int) PyModule_SetDocString(PyObject *, const char *); | ||
| Expand Down | ||
| Expand Up | @@ -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); | ||
|
Comment thread
Outdated
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide commentI'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: typedef struct {
long long __max_align_ll __attribute__((__aligned__(__alignof__(long long))));
long double __max_align_ld __attribute__((__aligned__(__alignof__(long double))));
/* _Float128 is defined as a basic type, so max_align_t must be
sufficiently aligned for it. This code must work in C++, so we
use __float128 here; that is only available on some
architectures, but only on i386 is extra alignment needed for
__float128. */
#ifdef __i386__
__float128 __max_align_f128 __attribute__((__aligned__(__alignof(__float128))));
#endif
} max_align_t;
Maybe we can at least put long long and long double: // Unused members added to make PyModuleConst_Def large enough
// to get a stable ABI and support future additions.
long long m_long_long;
long double m_long_double;
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide commentThis is the only issue I found here :)
Sorry, something went wrong.
All reactions
|
||
| } 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; | ||
| Expand Down | ||
| @@ -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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide commentnitpick: static PyObject * in here.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.