Skip to content

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
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

StanFromIreland
Copy link
Member

@StanFromIreland StanFromIreland commented Jul 14, 2025

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I know that it's a draft but here are already some comments that you can dismiss if you're working on them.

@StanFromIreland
Copy link
Member Author

StanFromIreland commented Jul 14, 2025

I have cleaned up the changes and ensure the behavior remains the same, however there are still a few points I need input from @malemburg
(And as Benedikt said, should be their own issue)

  • This function is documented as taking strings, but during the 2->3 conversion and undocumented, and untested change was made which allowed it to accept bytes. I have kept it this way (in Python, to make removal simpler), though I think this should either be documented and tested, or removed.
  • The function has been documented as ascii only, and for bytes, it is. However, for strings, it has not been enforced with an error. What should we do?

@StanFromIreland StanFromIreland marked this pull request as ready for review July 14, 2025 12:54
@StanFromIreland StanFromIreland requested a review from picnixz July 14, 2025 12:54
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Would you mind running some microbenchmarks?

Comment on lines +1040 to +1045
const char *cstr = PyUnicode_AsUTF8(encoding);
if (cstr == NULL) {
return NULL;
}

size_t len = strlen(cstr);
Copy link
Member

Choose a reason for hiding this comment

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

Use PyUnicode_AsUTF8AndSize.

return NULL;
}

char *normalized = PyMem_Malloc(len + 1);
Copy link
Member

Choose a reason for hiding this comment

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

We can avoid copies at the end by using PyUnicodeWriter here. It'll look something like this:

PyUnicodeWriter *writer = PyUnicodeWriter_Create(len + 1); // instead of PyMem_Malloc
if (writer == NULL) {
    /* ... */
}
/* ... */
if (PyUnicodeWriter_WriteUTF8(writer, normalized, len + 1) < 0) {
    /* ... */
}
return PyUnicodeWriter_Finish(writer);

@@ -26,9 +26,10 @@
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
"""#"
"""
Copy link
Member

Choose a reason for hiding this comment

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

Hate to be that guy, but these fly-by formatting changes just make it harder to review.

@@ -3900,6 +3900,7 @@ def test_encodings_normalize_encoding(self):
self.assertEqual(normalize('utf_8'), 'utf_8')
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
self.assertEqual(normalize('utf 8'), 'utf_8')

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change


import codecs
from _codecs import _normalize_encoding
Copy link
Member

Choose a reason for hiding this comment

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

put the from import after the sys import

@@ -37,6 +38,7 @@
_import_tail = ['*']
_aliases = aliases.aliases


Copy link
Member

Choose a reason for hiding this comment

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

If you want to slip PEP-8 stuff, do it everywhere. In this case, I don't think we need to do it.

Copy link
Member

Choose a reason for hiding this comment

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

(Don't do it everywhere, that creates conflicts. If we're going to reformat, do it in a seperate PR and backport it to 3.13 and 3.14.)

Copy link
Member

Choose a reason for hiding this comment

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

(Yes, also; so in this case, just remove this change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy