Skip to content

gh-129569: The function unicodedata.normalize() always returns built-in str #129570

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_unicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ def test_bug_834676(self):
# Check for bug 834676
unicodedata.normalize('NFC', '\ud55c\uae00')

def test_normalize_return_type(self):
# gh-129569: normalize() return type must always be str
normalize = unicodedata.normalize

class MyStr(str):
pass

normalization_forms = ("NFC", "NFKC", "NFD", "NFKD")
input_strings = (
# normalized strings
"",
"ascii",
# unnormalized strings
"\u1e0b\u0323",
"\u0071\u0307\u0323",
)

for form in normalization_forms:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use itertools.product to also iterate over the possible strings and the possible classes if you want and add input_str=..., as well as input_type=... in the sub test parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just use two loops.

Copy link
Contributor Author

@Hizuru3 Hizuru3 Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I integrated the input strings but I don't see an obvious advantage to iterate over classes since the only two patterns, one of which is the built-in str, are possible here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my intent was to reduce the overall indentation using itertools.product, but if we're using nested loops it's fine to keep it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. itertools.product is cool but it really shines with 3+ nested constructs IMHO.

for input_str in input_strings:
with self.subTest(form=form, input_str=input_str):
self.assertIs(type(normalize(form, input_str)), str)
self.assertIs(type(normalize(form, MyStr(input_str))), str)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`unicodedata.normalize` to always return a built-in :class:`str` object when given an input of a :class:`str` subclass, regardless of whether the string is already normalized.
10 changes: 5 additions & 5 deletions Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,34 +933,34 @@ unicodedata_UCD_normalize_impl(PyObject *self, PyObject *form,
if (PyUnicode_GET_LENGTH(input) == 0) {
/* Special case empty input strings, since resizing
them later would cause internal errors. */
return Py_NewRef(input);
return PyUnicode_FromObject(input);
}

if (PyUnicode_CompareWithASCIIString(form, "NFC") == 0) {
if (is_normalized_quickcheck(self, input,
true, false, true) == YES) {
return Py_NewRef(input);
return PyUnicode_FromObject(input);
}
return nfc_nfkc(self, input, 0);
}
if (PyUnicode_CompareWithASCIIString(form, "NFKC") == 0) {
if (is_normalized_quickcheck(self, input,
true, true, true) == YES) {
return Py_NewRef(input);
return PyUnicode_FromObject(input);
}
return nfc_nfkc(self, input, 1);
}
if (PyUnicode_CompareWithASCIIString(form, "NFD") == 0) {
if (is_normalized_quickcheck(self, input,
false, false, true) == YES) {
return Py_NewRef(input);
return PyUnicode_FromObject(input);
}
return nfd_nfkd(self, input, 0);
}
if (PyUnicode_CompareWithASCIIString(form, "NFKD") == 0) {
if (is_normalized_quickcheck(self, input,
false, true, true) == YES) {
return Py_NewRef(input);
return PyUnicode_FromObject(input);
}
return nfd_nfkd(self, input, 1);
}
Expand Down
Loading