@@ -487,12 +487,13 @@ def encodekey(key):
487487 data [encodekey (key )] = value
488488 else :
489489 # Where Env Var Names Can Be Mixed Case
490+ encoding = sys .getfilesystemencoding ()
490491 def encode (value ):
491492 if not isinstance (value , str ):
492493 raise TypeError ("str expected, not %s" % type (value ).__name__ )
493- return value .encode (sys . getfilesystemencoding () , 'surrogateescape' )
494+ return value .encode (encoding , 'surrogateescape' )
494495 def decode (value ):
495- return value .decode (sys . getfilesystemencoding () , 'surrogateescape' )
496+ return value .decode (encoding , 'surrogateescape' )
496497 encodekey = encode
497498 data = environ
498499 return _Environ (data ,
@@ -535,39 +536,43 @@ def getenvb(key, default=None):
535536
536537 __all__ .extend (("environb" , "getenvb" ))
537538
538- def fsencode (filename ):
539- """
540- Encode filename to the filesystem encoding with 'surrogateescape' error
541- handler, return bytes unchanged. On Windows, use 'strict' error handler if
542- the file system encoding is 'mbcs' (which is the default encoding).
543- """
544- if isinstance (filename , bytes ):
545- return filename
546- elif isinstance (filename , str ):
547- encoding = sys .getfilesystemencoding ()
548- if encoding == 'mbcs' :
549- return filename .encode (encoding )
550- else :
551- return filename .encode (encoding , 'surrogateescape' )
539+ def _fscodec ():
540+ encoding = sys .getfilesystemencoding ()
541+ if encoding == 'mbcs' :
542+ errors = None # strict
552543 else :
553- raise TypeError ( "expect bytes or str, not %s" % type ( filename ). __name__ )
544+ errors = 'surrogateescape'
554545
555- def fsdecode (filename ):
556- """
557- Decode filename from the filesystem encoding with 'surrogateescape' error
558- handler, return str unchanged. On Windows, use 'strict' error handler if
559- the file system encoding is 'mbcs' (which is the default encoding).
560- """
561- if isinstance (filename , str ):
562- return filename
563- elif isinstance (filename , bytes ):
564- encoding = sys .getfilesystemencoding ()
565- if encoding == 'mbcs' :
566- return filename .decode (encoding )
546+ def fsencode (filename ):
547+ """
548+ Encode filename to the filesystem encoding with 'surrogateescape' error
549+ handler, return bytes unchanged. On Windows, use 'strict' error handler if
550+ the file system encoding is 'mbcs' (which is the default encoding).
551+ """
552+ if isinstance (filename , bytes ):
553+ return filename
554+ elif isinstance (filename , str ):
555+ return filename .encode (encoding , errors )
567556 else :
568- return filename .decode (encoding , 'surrogateescape' )
569- else :
570- raise TypeError ("expect bytes or str, not %s" % type (filename ).__name__ )
557+ raise TypeError ("expect bytes or str, not %s" % type (filename ).__name__ )
558+
559+ def fsdecode (filename ):
560+ """
561+ Decode filename from the filesystem encoding with 'surrogateescape' error
562+ handler, return str unchanged. On Windows, use 'strict' error handler if
563+ the file system encoding is 'mbcs' (which is the default encoding).
564+ """
565+ if isinstance (filename , str ):
566+ return filename
567+ elif isinstance (filename , bytes ):
568+ return filename .decode (encoding , errors )
569+ else :
570+ raise TypeError ("expect bytes or str, not %s" % type (filename ).__name__ )
571+
572+ return fsencode , fsdecode
573+
574+ fsencode , fsdecode = _fscodec ()
575+ del _fscodec
571576
572577def _exists (name ):
573578 return name in globals ()
0 commit comments