| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 87bc3b7 commit 20f59fe
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Fix :func:`codecs.lookup` to normalize the encoding name the same way than :func:`encodings.normalize_encoding`, except that :func:`codecs.lookup` also converts the name to lower case. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,36 +49,36 @@ int PyCodec_Register(PyObject *search_function) | |||
| 49 | 49 | return -1; | |
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | - /* Convert a string to a normalized Python string: all characters are | ||
| 53 | - converted to lower case, spaces are replaced with underscores. */ | ||
| 52 | + extern int _Py_normalize_encoding(const char *, char *, size_t); | ||
| 53 | + | ||
| 54 | + /* Convert a string to a normalized Python string(decoded from UTF-8): all characters are | ||
| 55 | + converted to lower case, spaces and hyphens are replaced with underscores. */ | ||
| 54 | 56 | ||
| 55 | 57 | static | |
| 56 | 58 | PyObject *normalizestring(const char *string) | |
| 57 | 59 | { | |
| 58 | - size_t i; | ||
| 59 | 60 | size_t len = strlen(string); | |
| 60 | - char *p; | ||
| 61 | + char *encoding; | ||
| 61 | 62 | PyObject *v; | |
| 62 | 63 | ||
| 63 | 64 | if (len > PY_SSIZE_T_MAX) { | |
| 64 | 65 | PyErr_SetString(PyExc_OverflowError, "string is too large"); | |
| 65 | 66 | return NULL; | |
| 66 | 67 | } | |
| 67 | 68 | ||
| 68 | - p = PyMem_Malloc(len + 1); | ||
| 69 | - if (p == NULL) | ||
| 69 | + encoding = PyMem_Malloc(len + 1); | ||
| 70 | + if (encoding == NULL) | ||
| 70 | 71 | return PyErr_NoMemory(); | |
| 71 | - for (i = 0; i < len; i++) { | ||
| 72 | - char ch = string[i]; | ||
| 73 | - if (ch == ' ') | ||
| 74 | - ch = '-'; | ||
| 75 | - else | ||
| 76 | - ch = Py_TOLOWER(Py_CHARMASK(ch)); | ||
| 77 | - p[i] = ch; | ||
| 72 | + | ||
| 73 | + if (!_Py_normalize_encoding(string, encoding, len + 1)) | ||
| 74 | + { | ||
| 75 | + PyErr_SetString(PyExc_RuntimeError, "_Py_normalize_encoding() failed"); | ||
| 76 | + PyMem_Free(encoding); | ||
| 77 | + return NULL; | ||
| 78 | 78 | } | |
| 79 | - p[i] = '\0'; | ||
| 80 | - v = PyUnicode_FromString(p); | ||
| 81 | - PyMem_Free(p); | ||
| 79 | + | ||
| 80 | + v = PyUnicode_FromString(encoding); | ||
| 81 | + PyMem_Free(encoding); | ||
| 82 | 82 | return v; | |
| 83 | 83 | } | |
| 84 | 84 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments