-
-
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 1 commit
92873d6
4bae23a
b5f3df3
2ad72b2
3660160
4e12b9e
1c9e55a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1037,30 +1037,41 @@ static PyObject * | |||||
_codecs__normalize_encoding_impl(PyObject *module, PyObject *encoding) | ||||||
/*[clinic end generated code: output=d27465d81e361f8e input=3ff3f4d64995b988]*/ | ||||||
{ | ||||||
const char *cstr = PyUnicode_AsUTF8(encoding); | ||||||
Py_ssize_t len; | ||||||
const char *cstr = PyUnicode_AsUTF8AndSize(encoding, &len); | ||||||
if (cstr == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
size_t len = strlen(cstr); | ||||||
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); | ||||||
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; | ||||||
} | ||||||
|
||||||
PyObject *v = PyUnicode_FromString(normalized); | ||||||
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 v; | ||||||
return PyUnicodeWriter_Finish(writer); | ||||||
} | ||||||
|
||||||
/* --- Module API --------------------------------------------------------- */ | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.