@@ -1022,6 +1022,44 @@ _codecs_lookup_error_impl(PyObject *module, const char *name)
10221022 return PyCodec_LookupError (name );
10231023}
10241024
1025+ extern int _Py_normalize_encoding (const char * , char * , size_t , int );
1026+
1027+ /*[clinic input]
1028+ _codecs._normalize_encoding
1029+ encoding: str(encoding='ascii')
1030+ /
1031+
1032+ Normalize an encoding name, while not converting to lower case (to_lower == 1).
1033+ Used for encodings.normalize_encoding.
1034+ [clinic start generated code]*/
1035+
1036+ static PyObject *
1037+ _codecs__normalize_encoding_impl (PyObject * module , char * encoding )
1038+ /*[clinic end generated code: output=d5e3a4b5266fbe96 input=ca002bbc262228f1]*/
1039+ {
1040+ size_t len = strlen (encoding );
1041+ if (len > PY_SSIZE_T_MAX ) {
1042+ PyErr_SetString (PyExc_OverflowError , "encoding is too large" );
1043+ return NULL ;
1044+ }
1045+
1046+ char * normalized = PyMem_Malloc (len + 1 );
1047+ if (normalized == NULL ) {
1048+ return PyErr_NoMemory ();
1049+ }
1050+
1051+ if (!_Py_normalize_encoding (encoding , normalized , len + 1 , 0 )) {
1052+ PyErr_SetString (PyExc_RuntimeError , "_Py_normalize_encoding() failed" );
1053+ PyMem_Free (normalized );
1054+ return NULL ;
1055+ }
1056+
1057+ PyObject * v = PyUnicode_FromString (normalized );
1058+ PyMem_Free (normalized );
1059+ return v ;
1060+ }
1061+
1062+
10251063/* --- Module API --------------------------------------------------------- */
10261064
10271065static PyMethodDef _codecs_functions [] = {
@@ -1071,6 +1109,7 @@ static PyMethodDef _codecs_functions[] = {
10711109 _CODECS_REGISTER_ERROR_METHODDEF
10721110 _CODECS__UNREGISTER_ERROR_METHODDEF
10731111 _CODECS_LOOKUP_ERROR_METHODDEF
1112+ _CODECS__NORMALIZE_ENCODING_METHODDEF
10741113 {NULL , NULL } /* sentinel */
10751114};
10761115
0 commit comments