-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-55531: Implement normalize_encoding
in C
#136643
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
base: main
Are you sure you want to change the base?
Changes from all commits
92873d6
4bae23a
b5f3df3
2ad72b2
3660160
4e12b9e
1c9e55a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
:mod:`encodings`: Improve :func:`~encodings.normalize_encoding` performance | ||
by implementing the function in C using the private | ||
``_Py_normalize_encoding`` which has been modified to make lowercase | ||
conversion optional. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1022,6 +1022,58 @@ _codecs_lookup_error_impl(PyObject *module, const char *name) | |||||
return PyCodec_LookupError(name); | ||||||
} | ||||||
|
||||||
extern int _Py_normalize_encoding(const char *, char *, size_t, int); | ||||||
|
||||||
/*[clinic input] | ||||||
_codecs._normalize_encoding | ||||||
encoding: unicode | ||||||
Normalize an encoding name *encoding*. | ||||||
Used for encodings.normalize_encoding. Does not convert to lower case. | ||||||
[clinic start generated code]*/ | ||||||
|
||||||
static PyObject * | ||||||
_codecs__normalize_encoding_impl(PyObject *module, PyObject *encoding) | ||||||
/*[clinic end generated code: output=d27465d81e361f8e input=3ff3f4d64995b988]*/ | ||||||
{ | ||||||
Py_ssize_t len; | ||||||
const char *cstr = PyUnicode_AsUTF8AndSize(encoding, &len); | ||||||
if (cstr == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
if (len > PY_SSIZE_T_MAX) { | ||||||
PyErr_SetString(PyExc_OverflowError, "encoding is too large"); | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
PyUnicodeWriter *writer = PyUnicodeWriter_Create(len + 1); | ||||||
if (writer == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
char *normalized = PyMem_Malloc(len + 1); | ||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be possible to use a VLA for this, but I'm not sure it's worth the additional complexity. @picnixz WDYT? |
||||||
if (normalized == NULL) { | ||||||
PyUnicodeWriter_Discard(writer); | ||||||
return PyErr_NoMemory(); | ||||||
} | ||||||
|
||||||
if (!_Py_normalize_encoding(cstr, normalized, len + 1, 0)) { | ||||||
PyMem_Free(normalized); | ||||||
PyUnicodeWriter_Discard(writer); | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
if (PyUnicodeWriter_WriteUTF8(writer, normalized, (Py_ssize_t)strlen(normalized)) < 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The size shouldn't need to be recalculated here. It's always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cpython/Lib/test/test_codecs.py Lines 3901 to 3902 in 28937d3
No, it must be done to match the current behavior, where it can change. |
||||||
PyUnicodeWriter_Discard(writer); | ||||||
PyMem_Free(normalized); | ||||||
return NULL; | ||||||
} | ||||||
PyMem_Free(normalized); | ||||||
return PyUnicodeWriter_Finish(writer); | ||||||
} | ||||||
|
||||||
/* --- Module API --------------------------------------------------------- */ | ||||||
|
||||||
static PyMethodDef _codecs_functions[] = { | ||||||
|
@@ -1071,6 +1123,7 @@ static PyMethodDef _codecs_functions[] = { | |||||
_CODECS_REGISTER_ERROR_METHODDEF | ||||||
_CODECS__UNREGISTER_ERROR_METHODDEF | ||||||
_CODECS_LOOKUP_ERROR_METHODDEF | ||||||
_CODECS__NORMALIZE_ENCODING_METHODDEF | ||||||
{NULL, NULL} /* sentinel */ | ||||||
}; | ||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.