-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Open
StanFromIreland
wants to merge
7
commits into
python:main
Choose a base branch
from
StanFromIreland:enc-normalize-speedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−25
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92873d6
C
StanFromIreland 4bae23a
Correct clinic note
StanFromIreland b5f3df3
Little fixes
StanFromIreland 2ad72b2
Keep the messiness
StanFromIreland 3660160
Clean up tests
StanFromIreland 4e12b9e
Remove unnecessary message
StanFromIreland 1c9e55a
Review
StanFromIreland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2025-07-14-09-33-17.gh-issue-55531.Gt2e12.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.