@@ -71,7 +71,6 @@ PyObject *codeclookup(PyObject *self, PyObject *args)
7171 return NULL ;
7272}
7373
74- #ifdef Py_USING_UNICODE
7574/* --- Helpers ------------------------------------------------------------ */
7675
7776static
@@ -97,6 +96,49 @@ PyObject *codec_tuple(PyObject *unicode,
9796 return v ;
9897}
9998
99+ /* --- String codecs ------------------------------------------------------ */
100+ static PyObject *
101+ escape_decode (PyObject * self ,
102+ PyObject * args )
103+ {
104+ const char * errors = NULL ;
105+ const char * data ;
106+ int size ;
107+
108+ if (!PyArg_ParseTuple (args , "s#|z:escape_decode" ,
109+ & data , & size , & errors ))
110+ return NULL ;
111+ return codec_tuple (PyString_DecodeEscape (data , size , errors , 0 , NULL ),
112+ size );
113+ }
114+
115+ static PyObject *
116+ escape_encode (PyObject * self ,
117+ PyObject * args )
118+ {
119+ PyObject * str ;
120+ const char * errors = NULL ;
121+ char * buf ;
122+ int len ;
123+
124+ if (!PyArg_ParseTuple (args , "O!|z:escape_encode" ,
125+ & PyString_Type , & str , & errors ))
126+ return NULL ;
127+
128+ str = PyString_Repr (str , 0 );
129+ if (!str )
130+ return NULL ;
131+
132+ /* The string will be quoted. Unquote, similar to unicode-escape. */
133+ buf = PyString_AS_STRING (str );
134+ len = PyString_GET_SIZE (str );
135+ memmove (buf , buf + 1 , len - 2 );
136+ _PyString_Resize (& str , len - 2 );
137+
138+ return codec_tuple (str , PyString_Size (str ));
139+ }
140+
141+ #ifdef Py_USING_UNICODE
100142/* --- Decoder ------------------------------------------------------------ */
101143
102144static PyObject *
@@ -669,6 +711,8 @@ mbcs_encode(PyObject *self,
669711static PyMethodDef _codecs_functions [] = {
670712 {"register" , codecregister , METH_VARARGS },
671713 {"lookup" , codeclookup , METH_VARARGS },
714+ {"escape_encode" , escape_encode , METH_VARARGS },
715+ {"escape_decode" , escape_decode , METH_VARARGS },
672716#ifdef Py_USING_UNICODE
673717 {"utf_8_encode" , utf_8_encode , METH_VARARGS },
674718 {"utf_8_decode" , utf_8_decode , METH_VARARGS },
0 commit comments