@@ -200,148 +200,109 @@ PyObject *args_tuple(PyObject *object,
200200 return args ;
201201}
202202
203- /* Build a codec by calling factory(stream[,errors]) or just
204- factory(errors) depending on whether the given parameters are
205- non-NULL. */
203+ /* Helper function to get a codec item */
206204
207205static
208- PyObject * build_stream_codec (PyObject * factory ,
209- PyObject * stream ,
210- const char * errors )
211- {
212- PyObject * args , * codec ;
213-
214- args = args_tuple (stream , errors );
215- if (args == NULL )
216- return NULL ;
217-
218- codec = PyEval_CallObject (factory , args );
219- Py_DECREF (args );
220- return codec ;
221- }
222-
223- /* Convenience APIs to query the Codec registry.
224-
225- All APIs return a codec object with incremented refcount.
226-
227- */
228-
229- PyObject * PyCodec_Encoder (const char * encoding )
206+ PyObject * codec_getitem (const char * encoding , int index )
230207{
231208 PyObject * codecs ;
232209 PyObject * v ;
233210
234211 codecs = _PyCodec_Lookup (encoding );
235212 if (codecs == NULL )
236- goto onError ;
237- v = PyTuple_GET_ITEM (codecs ,0 );
213+ return NULL ;
214+ v = PyTuple_GET_ITEM (codecs , index );
238215 Py_DECREF (codecs );
239216 Py_INCREF (v );
240217 return v ;
241-
242- onError :
243- return NULL ;
244218}
245219
246- PyObject * PyCodec_Decoder (const char * encoding )
247- {
248- PyObject * codecs ;
249- PyObject * v ;
250-
251- codecs = _PyCodec_Lookup (encoding );
252- if (codecs == NULL )
253- goto onError ;
254- v = PyTuple_GET_ITEM (codecs ,1 );
255- Py_DECREF (codecs );
256- Py_INCREF (v );
257- return v ;
220+ /* Helper function to create an incremental codec. */
258221
259- onError :
260- return NULL ;
261- }
262-
263- PyObject * PyCodec_IncrementalEncoder (const char * encoding ,
264- const char * errors )
222+ static
223+ PyObject * codec_getincrementalcodec (const char * encoding ,
224+ const char * errors ,
225+ const char * attrname )
265226{
266- PyObject * codecs , * ret , * encoder ;
227+ PyObject * codecs , * ret , * inccodec ;
267228
268229 codecs = _PyCodec_Lookup (encoding );
269230 if (codecs == NULL )
270- goto onError ;
271- encoder = PyObject_GetAttrString (codecs , "incrementalencoder" );
272- if (encoder == NULL ) {
231+ return NULL ;
232+ inccodec = PyObject_GetAttrString (codecs , attrname );
233+ if (inccodec == NULL ) {
273234 Py_DECREF (codecs );
274235 return NULL ;
275236 }
276237 if (errors )
277- ret = PyObject_CallFunction (encoder , "O " , errors );
238+ ret = PyObject_CallFunction (inccodec , "s " , errors );
278239 else
279- ret = PyObject_CallFunction (encoder , NULL );
280- Py_DECREF (encoder );
240+ ret = PyObject_CallFunction (inccodec , NULL );
241+ Py_DECREF (inccodec );
281242 Py_DECREF (codecs );
282243 return ret ;
283-
284- onError :
285- return NULL ;
286244}
287245
288- PyObject * PyCodec_IncrementalDecoder (const char * encoding ,
289- const char * errors )
246+ /* Helper function to create a stream codec. */
247+
248+ static
249+ PyObject * codec_getstreamcodec (const char * encoding ,
250+ PyObject * stream ,
251+ const char * errors ,
252+ const int index )
290253{
291- PyObject * codecs , * ret , * decoder ;
254+ PyObject * codecs , * streamcodec ;
292255
293256 codecs = _PyCodec_Lookup (encoding );
294257 if (codecs == NULL )
295- goto onError ;
296- decoder = PyObject_GetAttrString (codecs , "incrementaldecoder" );
297- if (decoder == NULL ) {
298- Py_DECREF (codecs );
299258 return NULL ;
300- }
301- if (errors )
302- ret = PyObject_CallFunction (decoder , "O" , errors );
303- else
304- ret = PyObject_CallFunction (decoder , NULL );
305- Py_DECREF (decoder );
259+
260+ streamcodec = PyEval_CallFunction (
261+ PyTuple_GET_ITEM (codecs , index ), "Os" , stream , errors );
306262 Py_DECREF (codecs );
307- return ret ;
263+ return streamcodec ;
264+ }
308265
309- onError :
310- return NULL ;
266+ /* Convenience APIs to query the Codec registry.
267+
268+ All APIs return a codec object with incremented refcount.
269+
270+ */
271+
272+ PyObject * PyCodec_Encoder (const char * encoding )
273+ {
274+ return codec_getitem (encoding , 0 );
275+ }
276+
277+ PyObject * PyCodec_Decoder (const char * encoding )
278+ {
279+ return codec_getitem (encoding , 1 );
280+ }
281+
282+ PyObject * PyCodec_IncrementalEncoder (const char * encoding ,
283+ const char * errors )
284+ {
285+ return codec_getincrementalcodec (encoding , errors , "incrementalencoder" );
286+ }
287+
288+ PyObject * PyCodec_IncrementalDecoder (const char * encoding ,
289+ const char * errors )
290+ {
291+ return codec_getincrementalcodec (encoding , errors , "incrementaldecoder" );
311292}
312293
313294PyObject * PyCodec_StreamReader (const char * encoding ,
314295 PyObject * stream ,
315296 const char * errors )
316297{
317- PyObject * codecs , * ret ;
318-
319- codecs = _PyCodec_Lookup (encoding );
320- if (codecs == NULL )
321- goto onError ;
322- ret = build_stream_codec (PyTuple_GET_ITEM (codecs ,2 ),stream ,errors );
323- Py_DECREF (codecs );
324- return ret ;
325-
326- onError :
327- return NULL ;
298+ return codec_getstreamcodec (encoding , stream , errors , 2 );
328299}
329300
330301PyObject * PyCodec_StreamWriter (const char * encoding ,
331302 PyObject * stream ,
332303 const char * errors )
333304{
334- PyObject * codecs , * ret ;
335-
336- codecs = _PyCodec_Lookup (encoding );
337- if (codecs == NULL )
338- goto onError ;
339- ret = build_stream_codec (PyTuple_GET_ITEM (codecs ,3 ),stream ,errors );
340- Py_DECREF (codecs );
341- return ret ;
342-
343- onError :
344- return NULL ;
305+ return codec_getstreamcodec (encoding , stream , errors , 3 );
345306}
346307
347308/* Encode an object (e.g. an Unicode object) using the given encoding
0 commit comments