Repository navigation
bpo-41798: Allocate unicodedata CAPI on the heap #24128
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1308,10 +1308,24 @@ capi_getcode(const char* name, int namelen, Py_UCS4* code, | |
|
|
||
| } | ||
|
|
||
| static const _PyUnicode_Name_CAPI unicodedata_capi = | ||
| static void | ||
| udc_destroy_capi(PyObject *capsule) | ||
| { | ||
| void *capi = PyCapsule_GetPointer(capsule, PyUnicodeData_CAPSULE_NAME); | ||
| PyMem_Free(capi); | ||
| } | ||
|
|
||
| static _PyUnicode_Name_CAPI * | ||
| udc_get_capi(void) | ||
|
erlend-aasland marked this conversation as resolved.
Outdated
|
||
| { | ||
| .getname = capi_getucname, | ||
| .getcode = capi_getcode, | ||
| _PyUnicode_Name_CAPI *capi = PyMem_Malloc(sizeof(_PyUnicode_Name_CAPI)); | ||
| if (capi == NULL) { | ||
| PyErr_NoMemory(); | ||
| return NULL; | ||
| } | ||
| capi->getname = capi_getucname; | ||
| capi->getcode = capi_getcode; | ||
| return capi; | ||
| }; | ||
|
|
||
|
|
||
|
|
@@ -1477,13 +1491,19 @@ unicodedata_exec(PyObject *module) | |
| } | ||
|
|
||
| /* Export C API */ | ||
|
erlend-aasland marked this conversation as resolved.
|
||
| v = PyCapsule_New((void *)&unicodedata_capi, PyUnicodeData_CAPSULE_NAME, | ||
| NULL); | ||
| if (v == NULL) { | ||
| _PyUnicode_Name_CAPI *capi = udc_get_capi(); | ||
| if (capi == NULL) { | ||
| return -1; | ||
| } | ||
| if (PyModule_AddObject(module, "_ucnhash_CAPI", v) < 0) { | ||
| Py_DECREF(v); | ||
| PyObject *capsule = PyCapsule_New(capi, | ||
| PyUnicodeData_CAPSULE_NAME, | ||
| udc_destroy_capi); | ||
| if (capsule == NULL) { | ||
| PyMem_Free(capi); | ||
| return -1; | ||
| } | ||
| if (PyModule_AddObject(module, "_ucnhash_CAPI", capsule) < 0) { | ||
|
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
Contributor
Author
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. We could do that, but I'm not sure it would improve this code. What do you think?
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.
Contributor
Author
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. After thinking about it, I do agree. It's easier to follow the ref count when reading the code. I'll change it. Thanks!
Contributor
Author
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. Should I change the other |
||
| Py_DECREF(capsule); | ||
| return -1; | ||
| } | ||
| return 0; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.