Kaydet (Commit) 38b8ae0f authored tarafından Victor Stinner's avatar Victor Stinner

Issue #24993: Handle import error in namereplace error handler

Handle PyCapsule_Import() failure (exception) in PyCodec_NameReplaceErrors():
return immedialty NULL.
üst aba2466d
...@@ -966,7 +966,6 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) ...@@ -966,7 +966,6 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
} }
static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
static int ucnhash_initialized = 0;
PyObject *PyCodec_NameReplaceErrors(PyObject *exc) PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
{ {
...@@ -988,17 +987,17 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc) ...@@ -988,17 +987,17 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
return NULL; return NULL;
if (!(object = PyUnicodeEncodeError_GetObject(exc))) if (!(object = PyUnicodeEncodeError_GetObject(exc)))
return NULL; return NULL;
if (!ucnhash_initialized) { if (!ucnhash_CAPI) {
/* load the unicode data module */ /* load the unicode data module */
ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
PyUnicodeData_CAPSULE_NAME, 1); PyUnicodeData_CAPSULE_NAME, 1);
ucnhash_initialized = 1; if (!ucnhash_CAPI)
return NULL;
} }
for (i = start, ressize = 0; i < end; ++i) { for (i = start, ressize = 0; i < end; ++i) {
/* object is guaranteed to be "ready" */ /* object is guaranteed to be "ready" */
c = PyUnicode_READ_CHAR(object, i); c = PyUnicode_READ_CHAR(object, i);
if (ucnhash_CAPI && if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
replsize = 1+1+1+(int)strlen(buffer)+1; replsize = 1+1+1+(int)strlen(buffer)+1;
} }
else if (c >= 0x10000) { else if (c >= 0x10000) {
...@@ -1021,8 +1020,7 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc) ...@@ -1021,8 +1020,7 @@ PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
i < end; ++i) { i < end; ++i) {
c = PyUnicode_READ_CHAR(object, i); c = PyUnicode_READ_CHAR(object, i);
*outp++ = '\\'; *outp++ = '\\';
if (ucnhash_CAPI && if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) {
*outp++ = 'N'; *outp++ = 'N';
*outp++ = '{'; *outp++ = '{';
strcpy((char *)outp, buffer); strcpy((char *)outp, buffer);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment