11import codecs
22import contextlib
33import io
4+ import os
45import re
56import sys
67import unittest
@@ -526,14 +527,13 @@ def test_asrawunicodeescapestring(self):
526527class CAPICodecs (unittest .TestCase ):
527528
528529 def setUp (self ):
529- self .enterContext (import_helper .isolated_modules ())
530- self .enterContext (import_helper .CleanImport ('codecs' ))
531- self .codecs = import_helper .import_module ('codecs' )
532530 # Encoding names are normalized internally by converting them
533531 # to lowercase and their hyphens are replaced by underscores.
534532 self .encoding_name = f'codec_reversed_{ id (self )} '
535- # make sure that our custom codec is not already registered
536- self .assertRaises (LookupError , self .codecs .lookup , self .encoding_name )
533+ # Make sure that our custom codec is not already registered (that
534+ # way we know whether we correctly unregistered the custom codec
535+ # after a test or not).
536+ self .assertRaises (LookupError , codecs .lookup , self .encoding_name )
537537 # create the search function without registering yet
538538 self ._create_custom_codec ()
539539
@@ -586,41 +586,47 @@ def search_function(encoding):
586586
587587 @contextlib .contextmanager
588588 def use_custom_encoder (self ):
589- self .assertRaises (LookupError , self . codecs .lookup , self .encoding_name )
590- self . codecs .register (self .search_function )
589+ self .assertRaises (LookupError , codecs .lookup , self .encoding_name )
590+ codecs .register (self .search_function )
591591 yield
592- self . codecs .unregister (self .search_function )
593- self .assertRaises (LookupError , self . codecs .lookup , self .encoding_name )
592+ codecs .unregister (self .search_function )
593+ self .assertRaises (LookupError , codecs .lookup , self .encoding_name )
594594
595595 def test_codec_register (self ):
596596 search_function , encoding = self .search_function , self .encoding_name
597597 # register the search function using the C API
598598 self .assertIsNone (_testcapi .codec_register (search_function ))
599- self .assertIs (self .codecs .lookup (encoding ), search_function (encoding ))
600- self .assertEqual (self .codecs .encode ('123' , encoding = encoding ), '321' )
599+ # in case the test failed before cleaning up
600+ self .addCleanup (codecs .unregister , self .search_function )
601+ self .assertIs (codecs .lookup (encoding ), search_function (encoding ))
602+ self .assertEqual (codecs .encode ('123' , encoding = encoding ), '321' )
601603 # unregister the search function using the regular API
602- self . codecs .unregister (search_function )
603- self .assertRaises (LookupError , self . codecs .lookup , encoding )
604+ codecs .unregister (search_function )
605+ self .assertRaises (LookupError , codecs .lookup , encoding )
604606
605607 def test_codec_unregister (self ):
606608 search_function , encoding = self .search_function , self .encoding_name
607- self .assertRaises (LookupError , self . codecs .lookup , encoding )
609+ self .assertRaises (LookupError , codecs .lookup , encoding )
608610 # register the search function using the regular API
609- self .codecs .register (search_function )
610- self .assertIsNotNone (self .codecs .lookup (encoding ))
611+ codecs .register (search_function )
612+ # in case the test failed before cleaning up
613+ self .addCleanup (codecs .unregister , self .search_function )
614+ self .assertIsNotNone (codecs .lookup (encoding ))
611615 # unregister the search function using the C API
612616 self .assertIsNone (_testcapi .codec_unregister (search_function ))
613- self .assertRaises (LookupError , self . codecs .lookup , encoding )
617+ self .assertRaises (LookupError , codecs .lookup , encoding )
614618
615619 def test_codec_known_encoding (self ):
616- self .assertRaises (LookupError , self . codecs .lookup , 'unknown-codec' )
620+ self .assertRaises (LookupError , codecs .lookup , 'unknown-codec' )
617621 self .assertFalse (_testcapi .codec_known_encoding ('unknown-codec' ))
618622 self .assertFalse (_testcapi .codec_known_encoding ('unknown_codec' ))
619623 self .assertFalse (_testcapi .codec_known_encoding ('UNKNOWN-codec' ))
620624
621625 encoding_name = self .encoding_name
622- self .assertRaises (LookupError , self .codecs .lookup , encoding_name )
623- self .codecs .register (self .search_function )
626+ self .assertRaises (LookupError , codecs .lookup , encoding_name )
627+
628+ codecs .register (self .search_function )
629+ self .addCleanup (codecs .unregister , self .search_function )
624630
625631 for name in [
626632 encoding_name ,
@@ -741,11 +747,6 @@ def test_codec_stream_writer(self):
741747
742748class CAPICodecErrors (unittest .TestCase ):
743749
744- def setUp (self ):
745- self .enterContext (import_helper .isolated_modules ())
746- self .enterContext (import_helper .CleanImport ('codecs' ))
747- self .codecs = import_helper .import_module ('codecs' )
748-
749750 def test_codec_register_error (self ):
750751 self .assertRaises (LookupError , _testcapi .codec_lookup_error , 'custom' )
751752
@@ -754,24 +755,25 @@ def error_handler(exc):
754755
755756 error_handler = mock .Mock (wraps = error_handler )
756757 _testcapi .codec_register_error ('custom' , error_handler )
758+ # self.addCleanup(codecs.unregister_error, 'custom')
757759
758- self .assertRaises (UnicodeEncodeError , self . codecs .encode ,
760+ self .assertRaises (UnicodeEncodeError , codecs .encode ,
759761 '\xff ' , 'ascii' , errors = 'custom' )
760762 error_handler .assert_called_once ()
761763 error_handler .reset_mock ()
762764
763- self .assertRaises (UnicodeDecodeError , self . codecs .decode ,
765+ self .assertRaises (UnicodeDecodeError , codecs .decode ,
764766 b'\xff ' , 'ascii' , errors = 'custom' )
765767 error_handler .assert_called_once ()
766768
767769 def test_codec_lookup_error (self ):
768770 codec_lookup_error = _testcapi .codec_lookup_error
769- self .assertIs (codec_lookup_error (NULL ), self . codecs .strict_errors )
770- self .assertIs (codec_lookup_error ('strict' ), self . codecs .strict_errors )
771- self .assertIs (codec_lookup_error ('ignore' ), self . codecs .ignore_errors )
772- self .assertIs (codec_lookup_error ('replace' ), self . codecs .replace_errors )
773- self .assertIs (codec_lookup_error ('xmlcharrefreplace' ), self . codecs .xmlcharrefreplace_errors )
774- self .assertIs (codec_lookup_error ('namereplace' ), self . codecs .namereplace_errors )
771+ self .assertIs (codec_lookup_error (NULL ), codecs .strict_errors )
772+ self .assertIs (codec_lookup_error ('strict' ), codecs .strict_errors )
773+ self .assertIs (codec_lookup_error ('ignore' ), codecs .ignore_errors )
774+ self .assertIs (codec_lookup_error ('replace' ), codecs .replace_errors )
775+ self .assertIs (codec_lookup_error ('xmlcharrefreplace' ), codecs .xmlcharrefreplace_errors )
776+ self .assertIs (codec_lookup_error ('namereplace' ), codecs .namereplace_errors )
775777 self .assertRaises (LookupError , codec_lookup_error , 'custom' )
776778
777779 def test_codec_error_handlers (self ):
0 commit comments