Skip to content

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

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,10 +1308,31 @@ capi_getcode(const char* name, int namelen, Py_UCS4* code,

}

static const _PyUnicode_Name_CAPI unicodedata_capi =
static void
unicodedata_destroy_capi(PyObject *capsule)
{
.getname = capi_getucname,
.getcode = capi_getcode,
void *capi = PyCapsule_GetPointer(capsule, PyUnicodeData_CAPSULE_NAME);
PyMem_Free(capi);
}

static PyObject *
unicodedata_create_capi(void)
{
_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;

PyObject *capsule = PyCapsule_New(capi,
PyUnicodeData_CAPSULE_NAME,
unicodedata_destroy_capi);
if (capsule == NULL) {
PyMem_Free(capi);
}
return capsule;
};


Expand Down Expand Up @@ -1477,13 +1498,13 @@ unicodedata_exec(PyObject *module)
}

/* Export C API */
v = PyCapsule_New((void *)&unicodedata_capi, PyUnicodeData_CAPSULE_NAME,
NULL);
if (v == NULL) {
PyObject *capsule = unicodedata_create_capi();
if (capsule == NULL) {
return -1;
}
if (PyModule_AddObject(module, "_ucnhash_CAPI", v) < 0) {
Py_DECREF(v);
int rc = PyModule_AddObjectRef(module, "_ucnhash_CAPI", capsule);
Py_DECREF(capsule);
if (rc < 0) {
return -1;
}
return 0;
Expand Down