mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #8969: On Windows, use mbcs codec in strict mode to encode and decode
filenames and enable os.fsencode().
This commit is contained in:
parent
0f35e2c0f4
commit
313a120ab6
5 changed files with 33 additions and 22 deletions
21
Lib/os.py
21
Lib/os.py
|
@ -533,16 +533,19 @@ if supports_bytes_environ:
|
|||
return environb.get(key, default)
|
||||
__all__.append("getenvb")
|
||||
|
||||
if name != 'nt':
|
||||
def fsencode(value):
|
||||
"""Encode value for use in the file system, environment variables
|
||||
or the command line."""
|
||||
if isinstance(value, bytes):
|
||||
return value
|
||||
elif isinstance(value, str):
|
||||
return value.encode(sys.getfilesystemencoding(), 'surrogateescape')
|
||||
def fsencode(value):
|
||||
"""Encode value for use in the file system, environment variables
|
||||
or the command line."""
|
||||
if isinstance(value, bytes):
|
||||
return value
|
||||
elif isinstance(value, str):
|
||||
encoding = sys.getfilesystemencoding()
|
||||
if encoding == 'mbcs':
|
||||
return value.encode(encoding)
|
||||
else:
|
||||
raise TypeError("expect bytes or str, not %s" % type(value).__name__)
|
||||
return value.encode(encoding, 'surrogateescape')
|
||||
else:
|
||||
raise TypeError("expect bytes or str, not %s" % type(value).__name__)
|
||||
|
||||
def _exists(name):
|
||||
return name in globals()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue