-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
Changes from 1 commit
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
Add helpers to create new type/exception and add it to the module dict in one call. Signed-off-by: Christian Heimes <christian@python.org>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -713,22 +713,58 @@ PyModule_AddType(PyObject *module, PyTypeObject *type) | |
| return PyModule_AddObjectRef(module, name, (PyObject *)type); | ||
| } | ||
|
|
||
| int | ||
| PyModule_AddTypeFromSpec(PyObject *module, PyType_Spec *spec, PyObject *bases, PyTypeObject **rtype) | ||
| PyTypeObject * | ||
| PyModule_AddNewTypeFromSpec(PyObject *module, PyType_Spec *spec, | ||
| PyObject *base) | ||
| { | ||
| PyTypeObject *type; | ||
| PyObject *bases; | ||
|
|
||
| /* Support single, optional type like PyErr_NewException() */ | ||
| if (base == NULL) { | ||
| bases = NULL; | ||
| } | ||
| else if (PyTuple_Check(base)) { | ||
| bases = base; | ||
| Py_INCREF(bases); | ||
| } else { | ||
| bases = PyTuple_Pack(1, base); | ||
| if (bases == NULL) | ||
| return NULL; | ||
| } | ||
|
|
||
| type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, bases); | ||
| /* steal ref to bases */ | ||
| Py_XDECREF(bases); | ||
| if (type == NULL) { | ||
| return -1; | ||
| return NULL; | ||
| } | ||
|
|
||
| if (PyModule_AddType(module, type) < 0) { | ||
| return -1; | ||
| Py_DECREF(type); | ||
| return NULL; | ||
| } | ||
| if (rtype != NULL) { | ||
| *rtype = type; | ||
| return type; | ||
| } | ||
|
|
||
| PyObject * | ||
| PyModule_AddNewException(PyObject *module, const char *name, const char *doc, | ||
|
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. These two functions need tests.
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. Hm, I see these return borrowed references (held by the module object). That saves a DECREF if you don't need the new object, but it'll be problematic for HPy. |
||
| PyObject *base, PyObject *dict) | ||
| { | ||
| PyObject *exc = PyErr_NewExceptionWithDoc(name, doc, base, dict); | ||
| if (exc == NULL) { | ||
| return NULL; | ||
| } | ||
| return 0; | ||
|
|
||
| const char *shortname = strrchr(name, '.'); | ||
| if (shortname == NULL) { | ||
| shortname = name; | ||
| } | ||
| else { | ||
| shortname++; | ||
| } | ||
| if (PyModule_AddObjectRef(module, shortname, exc) < 0) { | ||
| Py_DECREF(exc); | ||
| return NULL; | ||
| } | ||
| return exc; | ||
| } | ||
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.
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 comment
The 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.