Skip to content

Commit 2ad72b2

Browse files
Keep the messiness
1 parent b5f3df3 commit 2ad72b2

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

Lib/encodings/__init__.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"""
3030

3131
import codecs
32+
from _codecs import _normalize_encoding
3233
import sys
3334
from . import aliases
3435

@@ -38,18 +39,6 @@
3839
_aliases = aliases.aliases
3940

4041

41-
_norm_encoding_map = (
42-
#0123456789ABCDEF0123456789ABCDEF
43-
' '
44-
' . 0123456789 '
45-
' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
46-
' abcdefghijklmnopqrstuvwxyz '
47-
' '
48-
' '
49-
' '
50-
' ')
51-
52-
5342
class CodecRegistryError(LookupError, SystemError):
5443
pass
5544

@@ -68,10 +57,7 @@ def normalize_encoding(encoding):
6857
if isinstance(encoding, bytes):
6958
encoding = str(encoding, "ascii")
7059

71-
s = encoding.translate(_norm_encoding_map)
72-
return '_'.join(s.split())
73-
74-
from _codecs import _normalize_encoding as normalize_encoding
60+
return _normalize_encoding(encoding)
7561

7662
def search_function(encoding):
7763

Modules/_codecsmodule.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,18 +1026,23 @@ extern int _Py_normalize_encoding(const char *, char *, size_t, int);
10261026

10271027
/*[clinic input]
10281028
_codecs._normalize_encoding
1029-
encoding: str(encoding='ascii')
1029+
encoding: unicode
10301030
10311031
Normalize an encoding name *encoding*.
10321032
10331033
Used for encodings.normalize_encoding. Does not convert to lower case.
10341034
[clinic start generated code]*/
10351035

10361036
static PyObject *
1037-
_codecs__normalize_encoding_impl(PyObject *module, char *encoding)
1038-
/*[clinic end generated code: output=d5e3a4b5266fbe96 input=cdb53c013b2400e3]*/
1037+
_codecs__normalize_encoding_impl(PyObject *module, PyObject *encoding)
1038+
/*[clinic end generated code: output=d27465d81e361f8e input=3ff3f4d64995b988]*/
10391039
{
1040-
size_t len = strlen(encoding);
1040+
const char *cstr = PyUnicode_AsUTF8(encoding);
1041+
if (cstr == NULL) {
1042+
return NULL;
1043+
}
1044+
1045+
size_t len = strlen(cstr);
10411046
if (len > PY_SSIZE_T_MAX) {
10421047
PyErr_SetString(PyExc_OverflowError, "encoding is too large");
10431048
return NULL;
@@ -1048,7 +1053,7 @@ _codecs__normalize_encoding_impl(PyObject *module, char *encoding)
10481053
return PyErr_NoMemory();
10491054
}
10501055

1051-
if (!_Py_normalize_encoding(encoding, normalized, len + 1, 0)) {
1056+
if (!_Py_normalize_encoding(cstr, normalized, len + 1, 0)) {
10521057
PyErr_SetString(PyExc_RuntimeError, "_Py_normalize_encoding() failed");
10531058
PyMem_Free(normalized);
10541059
return NULL;

Modules/clinic/_codecsmodule.c.h

Lines changed: 13 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)