@@ -615,6 +615,50 @@ def test_getfilesystemencoding(self):
615615 expected = None
616616 self .check_fsencoding (fs_encoding , expected )
617617
618+ @unittest .skipIf (sys .platform == 'win32' ,
619+ 'test specific to UNIX' )
620+ def test_c_locale_surrogateescape (self ):
621+ # Force the POSIX locale
622+ env = os .environ .copy ()
623+ env ["LC_ALL" ] = "C"
624+ code = '\n ' .join ((
625+ 'import codecs, sys' ,
626+ 'def dump(name):' ,
627+ ' std = getattr(sys, name)' ,
628+ ' encoding = codecs.lookup(std.encoding).name' ,
629+ ' print("%s: %s:%s" % (name, encoding, std.errors))' ,
630+ 'dump("stdin")' ,
631+ 'dump("stdout")' ,
632+ 'dump("stderr")' ,
633+ ))
634+ p = subprocess .Popen ([sys .executable , "-I" , "-c" , code ],
635+ stdout = subprocess .PIPE , env = env )
636+ out = p .communicate ()[0 ]
637+ self .assertEqual (out ,
638+ b'stdin: ascii:surrogateescape\n '
639+ b'stdout: ascii:surrogateescape\n '
640+ b'stderr: ascii:backslashreplace\n ' )
641+
642+ # replace the default error handler
643+ env ['PYTHONIOENCODING' ] = ':strict'
644+ p = subprocess .Popen ([sys .executable , "-c" , code ],
645+ stdout = subprocess .PIPE , env = env )
646+ out = p .communicate ()[0 ]
647+ self .assertEqual (out ,
648+ b'stdin: ascii:strict\n '
649+ b'stdout: ascii:strict\n '
650+ b'stderr: ascii:backslashreplace\n ' )
651+
652+ # force the encoding
653+ env ['PYTHONIOENCODING' ] = 'iso8859-1'
654+ p = subprocess .Popen ([sys .executable , "-c" , code ],
655+ stdout = subprocess .PIPE , env = env )
656+ out = p .communicate ()[0 ]
657+ self .assertEqual (out ,
658+ b'stdin: iso8859-1:surrogateescape\n '
659+ b'stdout: iso8859-1:surrogateescape\n '
660+ b'stderr: iso8859-1:backslashreplace\n ' )
661+
618662 def test_implementation (self ):
619663 # This test applies to all implementations equally.
620664
0 commit comments