@@ -659,44 +659,64 @@ PyObject *PyCodec_LookupError(const char *name)
659
659
return handler ;
660
660
}
661
661
662
- static void wrong_exception_type (PyObject * exc )
662
+
663
+ static inline void
664
+ wrong_exception_type (PyObject * exc )
663
665
{
664
666
PyErr_Format (PyExc_TypeError ,
665
- "don't know how to handle %.200s in error callback" ,
666
- Py_TYPE (exc )-> tp_name );
667
+ "don't know how to handle %T in error callback" , exc );
667
668
}
668
669
670
+
671
+ #define _PyIsUnicodeEncodeError (EXC ) \
672
+ PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeEncodeError)
673
+ #define _PyIsUnicodeDecodeError (EXC ) \
674
+ PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeDecodeError)
675
+ #define _PyIsUnicodeTranslateError (EXC ) \
676
+ PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeTranslateError)
677
+
678
+
679
+ // --- handler: 'strict' ------------------------------------------------------
680
+
669
681
PyObject * PyCodec_StrictErrors (PyObject * exc )
670
682
{
671
- if (PyExceptionInstance_Check (exc ))
683
+ if (PyExceptionInstance_Check (exc )) {
672
684
PyErr_SetObject (PyExceptionInstance_Class (exc ), exc );
673
- else
685
+ }
686
+ else {
674
687
PyErr_SetString (PyExc_TypeError , "codec must pass exception instance" );
688
+ }
675
689
return NULL ;
676
690
}
677
691
678
692
679
- PyObject * PyCodec_IgnoreErrors (PyObject * exc )
693
+ // --- handler: 'ignore' ------------------------------------------------------
694
+
695
+ static PyObject *
696
+ _PyCodec_IgnoreError (PyObject * exc , int as_bytes )
680
697
{
681
698
Py_ssize_t end ;
682
-
683
- if ( PyObject_TypeCheck ( exc , ( PyTypeObject * ) PyExc_UnicodeEncodeError )) {
684
- if ( PyUnicodeEncodeError_GetEnd ( exc , & end ))
685
- return NULL ;
699
+ if ( _PyUnicodeError_GetParams ( exc , NULL , NULL , NULL ,
700
+ & end , NULL , as_bytes ) < 0 )
701
+ {
702
+ return NULL ;
686
703
}
687
- else if (PyObject_TypeCheck (exc , (PyTypeObject * )PyExc_UnicodeDecodeError )) {
688
- if (PyUnicodeDecodeError_GetEnd (exc , & end ))
689
- return NULL ;
704
+ return Py_BuildValue ("(Nn)" , Py_GetConstant (Py_CONSTANT_EMPTY_STR ), end );
705
+ }
706
+
707
+
708
+ PyObject * PyCodec_IgnoreErrors (PyObject * exc )
709
+ {
710
+ if (_PyIsUnicodeEncodeError (exc ) || _PyIsUnicodeTranslateError (exc )) {
711
+ return _PyCodec_IgnoreError (exc , false);
690
712
}
691
- else if (PyObject_TypeCheck (exc , (PyTypeObject * )PyExc_UnicodeTranslateError )) {
692
- if (PyUnicodeTranslateError_GetEnd (exc , & end ))
693
- return NULL ;
713
+ else if (_PyIsUnicodeDecodeError (exc )) {
714
+ return _PyCodec_IgnoreError (exc , true);
694
715
}
695
716
else {
696
717
wrong_exception_type (exc );
697
718
return NULL ;
698
719
}
699
- return Py_BuildValue ("(Nn)" , Py_GetConstant (Py_CONSTANT_EMPTY_STR ), end );
700
720
}
701
721
702
722
@@ -1368,13 +1388,17 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
1368
1388
}
1369
1389
1370
1390
1371
- static PyObject * strict_errors (PyObject * self , PyObject * exc )
1391
+ // --- Codecs registry handlers -----------------------------------------------
1392
+
1393
+ static inline PyObject *
1394
+ strict_errors (PyObject * Py_UNUSED (self ), PyObject * exc )
1372
1395
{
1373
1396
return PyCodec_StrictErrors (exc );
1374
1397
}
1375
1398
1376
1399
1377
- static PyObject * ignore_errors (PyObject * self , PyObject * exc )
1400
+ static inline PyObject *
1401
+ ignore_errors (PyObject * Py_UNUSED (self ), PyObject * exc )
1378
1402
{
1379
1403
return PyCodec_IgnoreErrors (exc );
1380
1404
}
0 commit comments