@@ -243,20 +243,15 @@ PyObject *codec_getitem(const char *encoding, int index)
243243 return v ;
244244}
245245
246- /* Helper function to create an incremental codec. */
247-
246+ /* Helper functions to create an incremental codec. */
248247static
249- PyObject * codec_getincrementalcodec ( const char * encoding ,
250- const char * errors ,
251- const char * attrname )
248+ PyObject * codec_makeincrementalcodec ( PyObject * codec_info ,
249+ const char * errors ,
250+ const char * attrname )
252251{
253- PyObject * codecs , * ret , * inccodec ;
252+ PyObject * ret , * inccodec ;
254253
255- codecs = _PyCodec_Lookup (encoding );
256- if (codecs == NULL )
257- return NULL ;
258- inccodec = PyObject_GetAttrString (codecs , attrname );
259- Py_DECREF (codecs );
254+ inccodec = PyObject_GetAttrString (codec_info , attrname );
260255 if (inccodec == NULL )
261256 return NULL ;
262257 if (errors )
@@ -267,6 +262,21 @@ PyObject *codec_getincrementalcodec(const char *encoding,
267262 return ret ;
268263}
269264
265+ static
266+ PyObject * codec_getincrementalcodec (const char * encoding ,
267+ const char * errors ,
268+ const char * attrname )
269+ {
270+ PyObject * codec_info , * ret ;
271+
272+ codec_info = _PyCodec_Lookup (encoding );
273+ if (codec_info == NULL )
274+ return NULL ;
275+ ret = codec_makeincrementalcodec (codec_info , errors , attrname );
276+ Py_DECREF (codec_info );
277+ return ret ;
278+ }
279+
270280/* Helper function to create a stream codec. */
271281
272282static
@@ -290,6 +300,24 @@ PyObject *codec_getstreamcodec(const char *encoding,
290300 return streamcodec ;
291301}
292302
303+ /* Helpers to work with the result of _PyCodec_Lookup
304+
305+ */
306+ PyObject * _PyCodecInfo_GetIncrementalDecoder (PyObject * codec_info ,
307+ const char * errors )
308+ {
309+ return codec_makeincrementalcodec (codec_info , errors ,
310+ "incrementaldecoder" );
311+ }
312+
313+ PyObject * _PyCodecInfo_GetIncrementalEncoder (PyObject * codec_info ,
314+ const char * errors )
315+ {
316+ return codec_makeincrementalcodec (codec_info , errors ,
317+ "incrementalencoder" );
318+ }
319+
320+
293321/* Convenience APIs to query the Codec registry.
294322
295323 All APIs return a codec object with incremented refcount.
@@ -447,15 +475,12 @@ PyObject *PyCodec_Decode(PyObject *object,
447475}
448476
449477/* Text encoding/decoding API */
450- static
451- PyObject * codec_getitem_checked (const char * encoding ,
452- const char * operation_name ,
453- int index )
478+ PyObject * _PyCodec_LookupTextEncoding (const char * encoding ,
479+ const char * alternate_command )
454480{
455481 _Py_IDENTIFIER (_is_text_encoding );
456482 PyObject * codec ;
457483 PyObject * attr ;
458- PyObject * v ;
459484 int is_text_codec ;
460485
461486 codec = _PyCodec_Lookup (encoding );
@@ -482,27 +507,44 @@ PyObject *codec_getitem_checked(const char *encoding,
482507 Py_DECREF (codec );
483508 PyErr_Format (PyExc_LookupError ,
484509 "'%.400s' is not a text encoding; "
485- "use codecs.%s() to handle arbitrary codecs" ,
486- encoding , operation_name );
510+ "use %s to handle arbitrary codecs" ,
511+ encoding , alternate_command );
487512 return NULL ;
488513 }
489514 }
490515 }
491516
517+ /* This appears to be a valid text encoding */
518+ return codec ;
519+ }
520+
521+
522+ static
523+ PyObject * codec_getitem_checked (const char * encoding ,
524+ const char * alternate_command ,
525+ int index )
526+ {
527+ PyObject * codec ;
528+ PyObject * v ;
529+
530+ codec = _PyCodec_LookupTextEncoding (encoding , alternate_command );
531+ if (codec == NULL )
532+ return NULL ;
533+
492534 v = PyTuple_GET_ITEM (codec , index );
493- Py_DECREF (codec );
494535 Py_INCREF (v );
536+ Py_DECREF (codec );
495537 return v ;
496538}
497539
498540static PyObject * _PyCodec_TextEncoder (const char * encoding )
499541{
500- return codec_getitem_checked (encoding , "encode" , 0 );
542+ return codec_getitem_checked (encoding , "codecs. encode() " , 0 );
501543}
502544
503545static PyObject * _PyCodec_TextDecoder (const char * encoding )
504546{
505- return codec_getitem_checked (encoding , "decode" , 1 );
547+ return codec_getitem_checked (encoding , "codecs. decode() " , 1 );
506548}
507549
508550PyObject * _PyCodec_EncodeText (PyObject * object ,
0 commit comments