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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
17 changes: 4 additions & 13 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.

"""#"
"""

import codecs
from _codecs import _normalize_encoding
import sys
from . import aliases

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


class CodecRegistryError(LookupError, SystemError):
pass

Expand All @@ -55,18 +57,7 @@ def normalize_encoding(encoding):
if isinstance(encoding, bytes):
encoding = str(encoding, "ascii")

chars = []
punct = False
for c in encoding:
if c.isalnum() or c == '.':
if punct and chars:
chars.append('_')
if c.isascii():
chars.append(c)
punct = False
else:
punct = True
return ''.join(chars)
return _normalize_encoding(encoding)

def search_function(encoding):

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

# encodings.normalize_encoding() doesn't convert
# characters to lower case.
self.assertEqual(normalize('UTF 8'), 'UTF_8')
Expand Down
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.
44 changes: 44 additions & 0 deletions Modules/_codecsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,49 @@ _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]*/
{
const char *cstr = PyUnicode_AsUTF8(encoding);
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;
}

char *normalized = PyMem_Malloc(len + 1);
if (normalized == NULL) {
return PyErr_NoMemory();
}

if (!_Py_normalize_encoding(cstr, normalized, len + 1, 0)) {
PyErr_SetString(PyExc_RuntimeError, "_Py_normalize_encoding() failed");
PyMem_Free(normalized);
return NULL;
}

PyObject *v = PyUnicode_FromString(normalized);
PyMem_Free(normalized);
return v;
}


/* --- Module API --------------------------------------------------------- */

static PyMethodDef _codecs_functions[] = {
Expand Down Expand Up @@ -1071,6 +1114,7 @@ static PyMethodDef _codecs_functions[] = {
_CODECS_REGISTER_ERROR_METHODDEF
_CODECS__UNREGISTER_ERROR_METHODDEF
_CODECS_LOOKUP_ERROR_METHODDEF
_CODECS__NORMALIZE_ENCODING_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
66 changes: 65 additions & 1 deletion Modules/clinic/_codecsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3587,13 +3587,14 @@ PyUnicode_FromEncodedObject(PyObject *obj,
return v;
}

/* Normalize an encoding name: similar to encodings.normalize_encoding(), but
also convert to lowercase. Return 1 on success, or 0 on error (encoding is
longer than lower_len-1). */
/* Normalize an encoding name like encodings.normalize_encoding()
but allow to convert to lowercase if *to_lower* is true.
Return 1 on success, or 0 on error (encoding is longer than lower_len-1). */
int
_Py_normalize_encoding(const char *encoding,
char *lower,
size_t lower_len)
size_t lower_len,
int to_lower)
{
const char *e;
char *l;
Expand Down Expand Up @@ -3624,7 +3625,7 @@ _Py_normalize_encoding(const char *encoding,
if (l == l_end) {
return 0;
}
*l++ = Py_TOLOWER(c);
*l++ = to_lower ? Py_TOLOWER(c) : c;
}
else {
punct = 1;
Expand Down Expand Up @@ -3659,7 +3660,7 @@ PyUnicode_Decode(const char *s,
}

/* Shortcuts for common default encodings */
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) {
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower), 1)) {
char *lower = buflower;

/* Fast paths */
Expand Down Expand Up @@ -3916,7 +3917,7 @@ PyUnicode_AsEncodedString(PyObject *unicode,
}

/* Shortcuts for common default encodings */
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) {
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower), 1)) {
char *lower = buflower;

/* Fast paths */
Expand Down
7 changes: 4 additions & 3 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ PyCodec_Unregister(PyObject *search_function)
return 0;
}

extern int _Py_normalize_encoding(const char *, char *, size_t);
extern int _Py_normalize_encoding(const char *, char *, size_t, int);

/* Convert a string to a normalized Python string(decoded from UTF-8): all characters are
converted to lower case, spaces and hyphens are replaced with underscores. */
Expand All @@ -108,10 +108,11 @@ PyObject *normalizestring(const char *string)
}

encoding = PyMem_Malloc(len + 1);
if (encoding == NULL)
if (encoding == NULL) {
return PyErr_NoMemory();
}

if (!_Py_normalize_encoding(string, encoding, len + 1))
if (!_Py_normalize_encoding(string, encoding, len + 1, 1))
{
PyErr_SetString(PyExc_RuntimeError, "_Py_normalize_encoding() failed");
PyMem_Free(encoding);
Expand Down
4 changes: 2 additions & 2 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ _Py_mbrtowc(wchar_t *pwc, const char *str, size_t len, mbstate_t *pmbs)

#define USE_FORCE_ASCII

extern int _Py_normalize_encoding(const char *, char *, size_t);
extern int _Py_normalize_encoding(const char *, char *, size_t, int);

/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale
and POSIX locale. nl_langinfo(CODESET) announces an alias of the
Expand Down Expand Up @@ -231,7 +231,7 @@ check_force_ascii(void)
}

char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) {
if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding), 1)) {
goto error;
}

Expand Down
Loading