mirror of
https://github.com/python/cpython.git
synced 2025-10-11 09:23:31 +00:00
bpo-41798: Allocate unicodedata CAPI on the heap (GH-24128)
This commit is contained in:
parent
1459fed92c
commit
61d26394f9
1 changed files with 29 additions and 8 deletions
|
@ -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,
|
void *capi = PyCapsule_GetPointer(capsule, PyUnicodeData_CAPSULE_NAME);
|
||||||
.getcode = capi_getcode,
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1477,13 +1498,13 @@ unicodedata_exec(PyObject *module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Export C API */
|
/* Export C API */
|
||||||
v = PyCapsule_New((void *)&unicodedata_capi, PyUnicodeData_CAPSULE_NAME,
|
PyObject *capsule = unicodedata_create_capi();
|
||||||
NULL);
|
if (capsule == NULL) {
|
||||||
if (v == NULL) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (PyModule_AddObject(module, "_ucnhash_CAPI", v) < 0) {
|
int rc = PyModule_AddObjectRef(module, "_ucnhash_CAPI", capsule);
|
||||||
Py_DECREF(v);
|
Py_DECREF(capsule);
|
||||||
|
if (rc < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue