bpo-1635741: _PyUnicode_Name_CAPI moves to internal C API (GH-22713)

The private _PyUnicode_Name_CAPI structure of the PyCapsule API
unicodedata.ucnhash_CAPI moves to the internal C API. Moreover, the
structure gets a new state member which must be passed to the
getcode() and getname() functions.

* Move Include/ucnhash.h to Include/internal/pycore_ucnhash.h
* unicodedata module is now built with Py_BUILD_CORE_MODULE.
* unicodedata: move hashAPI variable into unicodedata_module_state.
This commit is contained in:
Victor Stinner 2020-10-26 16:43:47 +01:00 committed by GitHub
parent b510e101f8
commit 47e1afd2a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 49 deletions

View file

@ -16,7 +16,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "ucnhash.h"
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include "structmember.h" // PyMemberDef
#include <stdbool.h>
@ -97,6 +97,8 @@ typedef struct {
// Borrowed reference to &UCD_Type. It is used to prepare the code
// to convert the UCD_Type static type to a heap type.
PyTypeObject *ucd_type;
_PyUnicode_Name_CAPI capi;
} unicodedata_module_state;
// bpo-1635741: Temporary global state until the unicodedata module
@ -1180,10 +1182,11 @@ _getucname(unicodedata_module_state *state, PyObject *self,
}
static int
capi_getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen,
capi_getucname(void *state_raw, PyObject *self, Py_UCS4 code,
char* buffer, int buflen,
int with_alias_and_seq)
{
unicodedata_module_state *state = &global_module_state;
unicodedata_module_state *state = (unicodedata_module_state *)state_raw;
return _getucname(state, self, code, buffer, buflen, with_alias_and_seq);
}
@ -1323,21 +1326,15 @@ _getcode(unicodedata_module_state *state, PyObject* self,
}
static int
capi_getcode(PyObject* self, const char* name, int namelen, Py_UCS4* code,
capi_getcode(void *state_raw, PyObject* self,
const char* name, int namelen, Py_UCS4* code,
int with_named_seq)
{
unicodedata_module_state *state = &global_module_state;
unicodedata_module_state *state = (unicodedata_module_state *)state_raw;
return _getcode(state, self, name, namelen, code, with_named_seq);
}
static const _PyUnicode_Name_CAPI hashAPI =
{
sizeof(_PyUnicode_Name_CAPI),
capi_getucname,
capi_getcode
};
/* -------------------------------------------------------------------- */
/* Python bindings */
@ -1510,6 +1507,11 @@ PyInit_unicodedata(void)
PyObject *m, *v;
unicodedata_module_state *state = &global_module_state;
state->capi.size = sizeof(_PyUnicode_Name_CAPI);
state->capi.state = state;
state->capi.getname = capi_getucname;
state->capi.getcode = capi_getcode;
Py_SET_TYPE(&UCD_Type, &PyType_Type);
state->ucd_type = &UCD_Type;
@ -1528,7 +1530,7 @@ PyInit_unicodedata(void)
PyModule_AddObject(m, "ucd_3_2_0", v);
/* Export C API */
v = PyCapsule_New((void *)&hashAPI, PyUnicodeData_CAPSULE_NAME, NULL);
v = PyCapsule_New((void *)&state->capi, PyUnicodeData_CAPSULE_NAME, NULL);
if (v != NULL)
PyModule_AddObject(m, "ucnhash_CAPI", v);
return m;