@@ -47,7 +47,7 @@ one argument, the encoding name in all lower case letters, and return\n\
4747a tuple of functions (encoder, decoder, stream_reader, stream_writer)." );
4848
4949static
50- PyObject * codecregister (PyObject * self , PyObject * args )
50+ PyObject * codec_register (PyObject * self , PyObject * args )
5151{
5252 PyObject * search_function ;
5353
@@ -71,7 +71,7 @@ Looks up a codec tuple in the Python codec registry and returns\n\
7171a tuple of functions." );
7272
7373static
74- PyObject * codeclookup (PyObject * self , PyObject * args )
74+ PyObject * codec_lookup (PyObject * self , PyObject * args )
7575{
7676 char * encoding ;
7777
@@ -84,6 +84,72 @@ PyObject *codeclookup(PyObject *self, PyObject *args)
8484 return NULL ;
8585}
8686
87+ PyDoc_STRVAR (encode__doc__ ,
88+ "encode(obj, [encoding[,errors]]) -> object\n\
89+ \n\
90+ Encodes obj using the codec registered for encoding. encoding defaults\n\
91+ to the default encoding. errors may be given to set a different error\n\
92+ handling scheme. Default is 'strict' meaning that encoding errors raise\n\
93+ a ValueError. Other possible values are 'ignore', 'replace' and\n\
94+ 'xmlcharrefreplace' as well as any other name registered with\n\
95+ codecs.register_error that can handle ValueErrors." );
96+
97+ static PyObject *
98+ codec_encode (PyObject * self , PyObject * args )
99+ {
100+ char * encoding = NULL ;
101+ char * errors = NULL ;
102+ PyObject * v ;
103+
104+ if (!PyArg_ParseTuple (args , "O|ss:encode" , & v , & encoding , & errors ))
105+ return NULL ;
106+
107+ if (encoding == NULL )
108+ encoding = PyUnicode_GetDefaultEncoding ();
109+
110+ /* Encode via the codec registry */
111+ v = PyCodec_Encode (v , encoding , errors );
112+ if (v == NULL )
113+ goto onError ;
114+ return v ;
115+
116+ onError :
117+ return NULL ;
118+ }
119+
120+ PyDoc_STRVAR (decode__doc__ ,
121+ "decode(obj, [encoding[,errors]]) -> object\n\
122+ \n\
123+ Decodes obj using the codec registered for encoding. encoding defaults\n\
124+ to the default encoding. errors may be given to set a different error\n\
125+ handling scheme. Default is 'strict' meaning that encoding errors raise\n\
126+ a ValueError. Other possible values are 'ignore' and 'replace'\n\
127+ as well as any other name registerd with codecs.register_error that is\n\
128+ able to handle ValueErrors." );
129+
130+ static PyObject *
131+ codec_decode (PyObject * self , PyObject * args )
132+ {
133+ char * encoding = NULL ;
134+ char * errors = NULL ;
135+ PyObject * v ;
136+
137+ if (!PyArg_ParseTuple (args , "O|ss:decode" , & v , & encoding , & errors ))
138+ return NULL ;
139+
140+ if (encoding == NULL )
141+ encoding = PyUnicode_GetDefaultEncoding ();
142+
143+ /* Decode via the codec registry */
144+ v = PyCodec_Decode (v , encoding , errors );
145+ if (v == NULL )
146+ goto onError ;
147+ return v ;
148+
149+ onError :
150+ return NULL ;
151+ }
152+
87153/* --- Helpers ------------------------------------------------------------ */
88154
89155static
@@ -765,10 +831,12 @@ static PyObject *lookup_error(PyObject *self, PyObject *args)
765831/* --- Module API --------------------------------------------------------- */
766832
767833static PyMethodDef _codecs_functions [] = {
768- {"register" , codecregister , METH_VARARGS ,
834+ {"register" , codec_register , METH_VARARGS ,
769835 register__doc__ },
770- {"lookup" , codeclookup , METH_VARARGS ,
836+ {"lookup" , codec_lookup , METH_VARARGS ,
771837 lookup__doc__ },
838+ {"encode" , codec_encode , METH_VARARGS },
839+ {"decode" , codec_decode , METH_VARARGS },
772840 {"escape_encode" , escape_encode , METH_VARARGS },
773841 {"escape_decode" , escape_decode , METH_VARARGS },
774842#ifdef Py_USING_UNICODE
0 commit comments