os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
keep a local copy of the fileystem encoding, instead of calling sys.getfilesystemencoding() each time. The filesystem encoding is now constant.
This commit is contained in:
parent
2062937aff
commit
df6d6cb0fa
69
Lib/os.py
69
Lib/os.py
|
@ -487,12 +487,13 @@ def _createenviron():
|
||||||
data[encodekey(key)] = value
|
data[encodekey(key)] = value
|
||||||
else:
|
else:
|
||||||
# Where Env Var Names Can Be Mixed Case
|
# Where Env Var Names Can Be Mixed Case
|
||||||
|
encoding = sys.getfilesystemencoding()
|
||||||
def encode(value):
|
def encode(value):
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
raise TypeError("str expected, not %s" % type(value).__name__)
|
raise TypeError("str expected, not %s" % type(value).__name__)
|
||||||
return value.encode(sys.getfilesystemencoding(), 'surrogateescape')
|
return value.encode(encoding, 'surrogateescape')
|
||||||
def decode(value):
|
def decode(value):
|
||||||
return value.decode(sys.getfilesystemencoding(), 'surrogateescape')
|
return value.decode(encoding, 'surrogateescape')
|
||||||
encodekey = encode
|
encodekey = encode
|
||||||
data = environ
|
data = environ
|
||||||
return _Environ(data,
|
return _Environ(data,
|
||||||
|
@ -535,39 +536,43 @@ if supports_bytes_environ:
|
||||||
|
|
||||||
__all__.extend(("environb", "getenvb"))
|
__all__.extend(("environb", "getenvb"))
|
||||||
|
|
||||||
def fsencode(filename):
|
def _fscodec():
|
||||||
"""
|
encoding = sys.getfilesystemencoding()
|
||||||
Encode filename to the filesystem encoding with 'surrogateescape' error
|
if encoding == 'mbcs':
|
||||||
handler, return bytes unchanged. On Windows, use 'strict' error handler if
|
errors = None # strict
|
||||||
the file system encoding is 'mbcs' (which is the default encoding).
|
|
||||||
"""
|
|
||||||
if isinstance(filename, bytes):
|
|
||||||
return filename
|
|
||||||
elif isinstance(filename, str):
|
|
||||||
encoding = sys.getfilesystemencoding()
|
|
||||||
if encoding == 'mbcs':
|
|
||||||
return filename.encode(encoding)
|
|
||||||
else:
|
|
||||||
return filename.encode(encoding, 'surrogateescape')
|
|
||||||
else:
|
else:
|
||||||
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
|
errors = 'surrogateescape'
|
||||||
|
|
||||||
def fsdecode(filename):
|
def fsencode(filename):
|
||||||
"""
|
"""
|
||||||
Decode filename from the filesystem encoding with 'surrogateescape' error
|
Encode filename to the filesystem encoding with 'surrogateescape' error
|
||||||
handler, return str unchanged. On Windows, use 'strict' error handler if
|
handler, return bytes unchanged. On Windows, use 'strict' error handler if
|
||||||
the file system encoding is 'mbcs' (which is the default encoding).
|
the file system encoding is 'mbcs' (which is the default encoding).
|
||||||
"""
|
"""
|
||||||
if isinstance(filename, str):
|
if isinstance(filename, bytes):
|
||||||
return filename
|
return filename
|
||||||
elif isinstance(filename, bytes):
|
elif isinstance(filename, str):
|
||||||
encoding = sys.getfilesystemencoding()
|
return filename.encode(encoding, errors)
|
||||||
if encoding == 'mbcs':
|
|
||||||
return filename.decode(encoding)
|
|
||||||
else:
|
else:
|
||||||
return filename.decode(encoding, 'surrogateescape')
|
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
|
||||||
else:
|
|
||||||
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
|
def fsdecode(filename):
|
||||||
|
"""
|
||||||
|
Decode filename from the filesystem encoding with 'surrogateescape' error
|
||||||
|
handler, return str unchanged. On Windows, use 'strict' error handler if
|
||||||
|
the file system encoding is 'mbcs' (which is the default encoding).
|
||||||
|
"""
|
||||||
|
if isinstance(filename, str):
|
||||||
|
return filename
|
||||||
|
elif isinstance(filename, bytes):
|
||||||
|
return filename.decode(encoding, errors)
|
||||||
|
else:
|
||||||
|
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
|
||||||
|
|
||||||
|
return fsencode, fsdecode
|
||||||
|
|
||||||
|
fsencode, fsdecode = _fscodec()
|
||||||
|
del _fscodec
|
||||||
|
|
||||||
def _exists(name):
|
def _exists(name):
|
||||||
return name in globals()
|
return name in globals()
|
||||||
|
|
Loading…
Reference in New Issue