Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Allocate unicodedata CAPI on the heap
  • Loading branch information
Erlend E. Aasland
Erlend E. Aasland committed Jan 5, 2021
commit b3542651fb35abacdc085e8689546090eda71ab4
36 changes: 28 additions & 8 deletions Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
{
void *capi = PyCapsule_GetPointer(capsule, PyUnicodeData_CAPSULE_NAME);
PyMem_Free(capi);
}

static _PyUnicode_Name_CAPI *
udc_get_capi(void)
Comment thread
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;
};


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

/* Export C API */
Comment thread
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using PyModule_AddObjectRef in here too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyModule_AddObjectRef() is more easy to understand than PyModule_AddObject(), because PyModule_AddObject() will steal the refs sometimes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change the other PyModule_AddObject in unicodedata_exec() while we're there?

Py_DECREF(capsule);
return -1;
}
return 0;
Expand Down