diff options
author | Erlend Egeberg Aasland <erlend.aasland@innova.no> | 2021-01-20 12:03:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-20 12:03:53 +0100 |
commit | 61d26394f97306ab4890f1522f26ee6d17461e2b (patch) | |
tree | 8bc08b1e2da23eb762dc9d081f84d1bf6cb722c1 /Modules | |
parent | Doc: os.path.abspath and Path.resolve are also different (GH-23276) (diff) | |
download | cpython-61d26394f97306ab4890f1522f26ee6d17461e2b.tar.gz cpython-61d26394f97306ab4890f1522f26ee6d17461e2b.tar.bz2 cpython-61d26394f97306ab4890f1522f26ee6d17461e2b.zip |
bpo-41798: Allocate unicodedata CAPI on the heap (GH-24128)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/unicodedata.c | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index 4b8c46c7797..aebae7da576 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -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; }; @@ -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; |