8000 bpo-1635741 - port unicodedata to multi-phase init by koubaa · Pull Request #22145 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-1635741 - port unicodedata to multi-phase init #22145

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add module to capsule
  • Loading branch information
koubaa committed Sep 15, 2020
commit f8dc82f36d18f4628533f0decfa3cb7f5f3c8b33
2 changes: 2 additions & 0 deletions Include/ucnhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef struct {
int (*getcode)(PyObject *self, const char* name, int namelen, Py_UCS4* code,
int with_named_seq);

PyObject *module;

} _PyUnicode_Name_CAPI;

#ifdef __cplusplus
Expand Down
39 changes: 21 additions & 18 deletions Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _Py_IDENTIFIER(NFKD);

typedef struct {
PyTypeObject *ucd_type;
_PyUnicode_Name_CAPI capsule_api;
} unicodedata_state;

static inline unicodedata_state*
Expand Down Expand Up @@ -1461,10 +1462,11 @@ _getucname_internal(unicodedata_state *state, PyObject *self, Py_UCS4 code,
}

static int
_getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen,
_getucname(PyObject *module, Py_UCS4 code, char* buffer, int buflen,
int with_alias_and_seq)
{
return _getucname_internal(NULL, self, code, buffer, buflen,
unicodedata_state *state = unicodedata_get_state(module);
return _getucname_internal(state, module, code, buffer, buflen,
with_alias_and_seq);
}

Expand Down Expand Up @@ -1601,19 +1603,13 @@ _getcode_internal(unicodedata_state *state, PyObject* self, const char* name,
}

static int
_getcode(PyObject* self, const char* name,
_getcode(PyObject* module, const char* name,
int namelen, Py_UCS4* code, int with_named_seq)
{
return _getcode_internal(NULL, self, name, namelen, code, with_named_seq);
unicodedata_state *state = unicodedata_get_state(module);
return _getcode_internal(state, module, name, namelen, code, with_named_seq);
}

static const _PyUnicode_Name_CAPI hashAPI =
{
sizeof(_PyUnicode_Name_CAPI),
_getucname,
_getcode
};

static PyObject *
unicodedata_UCD_name_internal(PyObject *self, unicodedata_state *state,
int chr, PyObject *default_value)
Expand Down Expand Up @@ -1843,16 +1839,17 @@ unicodedata_free(void *module)
unicodedata_clear((PyObject *)module);
}

static int unicodedata_exec(PyObject *m)
static int unicodedata_exec(PyObject *mod)
{
unicodedata_state *state = unicodedata_get_state(m);
unicodedata_state *state = unicodedata_get_state(mod);
state->ucd_type = (PyTypeObject *)PyType_FromModuleAndSpec(
m, &unicodedata_ucd_type_spec, NULL);
mod, &unicodedata_ucd_type_spec, NULL);
if (state->ucd_type == NULL) {
return -1;
}

if (PyModule_AddStringConstant(m, "unidata_version", UNIDATA_VERSION) < 0) {
if (PyModule_AddStringConstant(mod, "unidata_version",
UNIDATA_VERSION) < 0) {
return -1;
}

Expand All @@ -1862,17 +1859,23 @@ static int unicodedata_exec(PyObject *m)
if (v == NULL) {
return -1;
}
if (PyModule_AddObject(m, "ucd_3_2_0", v) < 0) {
if (PyModule_AddObject(mod, "ucd_3_2_0", v) < 0) {
Py_DECREF(v);
return -1;
}

state->capsule_api.size = sizeof(_PyUnicode_Name_CAPI);
state->capsule_api.getname = _getucname;
state->capsule_api.getcode = _getcode;
state->capsule_api.module = mod;

/* Export C API */
v = PyCapsule_New((void *)&hashAPI, PyUnicodeData_CAPSULE_NAME, NULL);
v = PyCapsule_New((void *)&state->capsule_api,
PyUnicodeData_CAPSULE_NAME, NULL);
if (v == NULL) {
return -1;
}
if (PyModule_AddObject(m, "ucnhash_CAPI", v) < 0) {
if (PyModule_AddObject(mod, "ucnhash_CAPI", v) < 0) {
Py_DECREF(v);
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6523,8 +6523,8 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
s++;
ch = 0xffffffff; /* in case 'getcode' messes up */
if (namelen <= INT_MAX &&
ucnhash_CAPI->getcode(NULL, start, (int)namelen,
&ch, 0)) {
ucnhash_CAPI->getcode(ucnhash_CAPI->module, start,
(int)namelen, &ch, 0)) {
assert(ch <= MAX_UNICODE);
WRITE_CHAR(ch);
continue;
Expand Down
6 changes: 4 additions & 2 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
for (i = start, ressize = 0; i < end; ++i) {
/* object is guaranteed to be "ready" */
c = PyUnicode_READ_CHAR(object, i);
if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
if (ucnhash_CAPI->getname(ucnhash_CAPI->module, c,
buffer, sizeof(buffer), 1)) {
replsize = 1+1+1+(int)strlen(buffer)+1;
}
else if (c >= 0x10000) {
Expand All @@ -1009,7 +1010,8 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
i < end; ++i) {
c = PyUnicode_READ_CHAR(object, i);
*outp++ = '\\';
if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
if (ucnhash_CAPI->getname(ucnhash_CAPI->module, c,
buffer, sizeof(buffer), 1)) {
*outp++ = 'N';
*outp++ = '{';
strcpy((char *)outp, buffer);
Expand Down
0