| 845 | __all__.extend(("environb", "getenvb")) |
| 846 | |
| 847 | def _fscodec(): |
| 848 | encoding = sys.getfilesystemencoding() |
| 849 | errors = sys.getfilesystemencodeerrors() |
| 850 | |
| 851 | def fsencode(filename): |
| 852 | """Encode filename (an os.PathLike, bytes, or str) to the filesystem |
| 853 | encoding with 'surrogateescape' error handler, return bytes unchanged. |
| 854 | On Windows, use 'strict' error handler if the file system encoding is |
| 855 | 'mbcs' (which is the default encoding). |
| 856 | """ |
| 857 | filename = fspath(filename) # Does type-checking of `filename`. |
| 858 | if isinstance(filename, str): |
| 859 | return filename.encode(encoding, errors) |
| 860 | else: |
| 861 | return filename |
| 862 | |
| 863 | def fsdecode(filename): |
| 864 | """Decode filename (an os.PathLike, bytes, or str) from the filesystem |
| 865 | encoding with 'surrogateescape' error handler, return str unchanged. On |
| 866 | Windows, use 'strict' error handler if the file system encoding is |
| 867 | 'mbcs' (which is the default encoding). |
| 868 | """ |
| 869 | filename = fspath(filename) # Does type-checking of `filename`. |
| 870 | if isinstance(filename, bytes): |
| 871 | return filename.decode(encoding, errors) |
| 872 | else: |
| 873 | return filename |
| 874 | |
| 875 | return fsencode, fsdecode |
| 876 | |
| 877 | fsencode, fsdecode = _fscodec() |
| 878 | del _fscodec |