@@ -564,6 +564,63 @@ def test_executable(self):
564564 p .wait ()
565565 self .assertIn (executable , ["b''" , repr (sys .executable .encode ("ascii" , "backslashreplace" ))])
566566
567+ def test_getfilesystemencoding (self ):
568+ import codecs
569+
570+ def check_fsencoding (fs_encoding , expected = None ):
571+ self .assertIsNotNone (fs_encoding )
572+ if sys .platform == 'darwin' :
573+ self .assertEqual (fs_encoding , 'utf-8' )
574+ codecs .lookup (fs_encoding )
575+ if expected :
576+ self .assertEqual (fs_encoding , expected )
577+
578+ fs_encoding = sys .getfilesystemencoding ()
579+ check_fsencoding (fs_encoding )
580+
581+ def get_fsencoding (env ):
582+ output = subprocess .check_output (
583+ [sys .executable , "-c" ,
584+ "import sys; print(sys.getfilesystemencoding())" ],
585+ env = env )
586+ return output .rstrip ().decode ('ascii' )
587+
588+ try :
589+ sys .executable .encode ('ascii' )
590+ except UnicodeEncodeError :
591+ # Python doesn't start with ASCII locale if its path is not ASCII,
592+ # see issue #8611
593+ pass
594+ else :
595+ # Even in C locale
596+ env = os .environ .copy ()
597+ env ['LANG' ] = 'C'
598+ try :
599+ del env ['PYTHONFSENCODING' ]
600+ except KeyError :
601+ pass
602+ check_fsencoding (get_fsencoding (env ), 'ascii' )
603+
604+ # Filesystem encoding is hardcoded on Windows and Mac OS X
605+ if sys .platform not in ('win32' , 'darwin' ):
606+ for encoding in ('ascii' , 'cp850' , 'iso8859-1' , 'utf-8' ):
607+ env = os .environ .copy ()
608+ env ['PYTHONFSENCODING' ] = encoding
609+ check_fsencoding (get_fsencoding (env ), encoding )
610+
611+
612+ def test_setfilesystemencoding (self ):
613+ old = sys .getfilesystemencoding ()
614+ try :
615+ sys .setfilesystemencoding ("iso-8859-1" )
616+ self .assertEqual (sys .getfilesystemencoding (), "iso-8859-1" )
617+ finally :
618+ sys .setfilesystemencoding (old )
619+ try :
620+ self .assertRaises (LookupError , sys .setfilesystemencoding , "xxx" )
621+ finally :
622+ sys .setfilesystemencoding (old )
623+
567624
568625class SizeofTest (unittest .TestCase ):
569626
@@ -863,62 +920,6 @@ def test_pythontypes(self):
863920 # sys.flags
864921 check (sys .flags , size (vh ) + self .P * len (sys .flags ))
865922
866- def test_getfilesystemencoding (self ):
867- import codecs
868-
869- def check_fsencoding (fs_encoding , expected = None ):
870- self .assertIsNotNone (fs_encoding )
871- if sys .platform == 'darwin' :
872- self .assertEqual (fs_encoding , 'utf-8' )
873- codecs .lookup (fs_encoding )
874- if expected :
875- self .assertEqual (fs_encoding , expected )
876-
877- fs_encoding = sys .getfilesystemencoding ()
878- check_fsencoding (fs_encoding )
879-
880- def get_fsencoding (env ):
881- output = subprocess .check_output (
882- [sys .executable , "-c" ,
883- "import sys; print(sys.getfilesystemencoding())" ],
884- env = env )
885- return output .rstrip ().decode ('ascii' )
886-
887- try :
888- sys .executable .encode ('ascii' )
889- except UnicodeEncodeError :
890- # Python doesn't start with ASCII locale if its path is not ASCII,
891- # see issue #8611
892- pass
893- else :
894- # Even in C locale
895- env = os .environ .copy ()
896- env ['LANG' ] = 'C'
897- try :
898- del env ['PYTHONFSENCODING' ]
899- except KeyError :
900- pass
901- check_fsencoding (get_fsencoding (env ), 'ascii' )
902-
903- # Filesystem encoding is hardcoded on Windows and Mac OS X
904- if sys .platform not in ('win32' , 'darwin' ):
905- for encoding in ('ascii' , 'cp850' , 'iso8859-1' , 'utf-8' ):
906- env = os .environ .copy ()
907- env ['PYTHONFSENCODING' ] = encoding
908- check_fsencoding (get_fsencoding (env ), encoding )
909-
910-
911- def test_setfilesystemencoding (self ):
912- old = sys .getfilesystemencoding ()
913- try :
914- sys .setfilesystemencoding ("iso-8859-1" )
915- self .assertEqual (sys .getfilesystemencoding (), "iso-8859-1" )
916- finally :
917- sys .setfilesystemencoding (old )
918- try :
919- self .assertRaises (LookupError , sys .setfilesystemencoding , "xxx" )
920- finally :
921- sys .setfilesystemencoding (old )
922923
923924def test_main ():
924925 test .support .run_unittest (SysModuleTest , SizeofTest )
0 commit comments