|
| 1 | +""" |
| 2 | +Compatibility module for high-level h5py |
| 3 | +""" |
| 4 | +import sys |
| 5 | +import six |
| 6 | + |
| 7 | + |
| 8 | +try: |
| 9 | + from os import fspath |
| 10 | +except ImportError: |
| 11 | + def fspath(path): |
| 12 | + """ |
| 13 | + Return the string representation of the path. |
| 14 | + If str or bytes is passed in, it is returned unchanged. |
| 15 | + This code comes from PEP 519, modified to support earlier versions of |
| 16 | + python. |
| 17 | +
|
| 18 | + This is required for python < 3.6. |
| 19 | + """ |
| 20 | + if isinstance(path, (six.text_type, six.binary_type)): |
| 21 | + return path |
| 22 | + |
| 23 | + # Work from the object's type to match method resolution of other magic |
| 24 | + # methods. |
| 25 | + path_type = type(path) |
| 26 | + try: |
| 27 | + return path_type.__fspath__(path) |
| 28 | + except AttributeError: |
| 29 | + if hasattr(path_type, '__fspath__'): |
| 30 | + raise |
| 31 | + try: |
| 32 | + import pathlib |
| 33 | + except ImportError: |
| 34 | + pass |
| 35 | + else: |
| 36 | + if isinstance(path, pathlib.PurePath): |
| 37 | + return six.text_type(path) |
| 38 | + |
| 39 | + raise TypeError("expected str, bytes or os.PathLike object, not " |
| 40 | + + path_type.__name__) |
| 41 | + |
| 42 | +# This is from python 3.5 stdlib (hence lacks PEP 519 changes) |
| 43 | +# This was introduced into python 3.2, so python < 3.2 does not have this |
| 44 | +# Effectively, this is only required for python 2.6 and 2.7, and can be removed |
| 45 | +# once support for them is dropped |
| 46 | +def _fscodec(): |
| 47 | + encoding = sys.getfilesystemencoding() |
| 48 | + if encoding == 'mbcs': |
| 49 | + errors = 'strict' |
| 50 | + else: |
| 51 | + errors = 'surrogateescape' |
| 52 | + |
| 53 | + def fsencode(filename): |
| 54 | + """ |
| 55 | + Encode filename to the filesystem encoding with 'surrogateescape' error |
| 56 | + handler, return bytes unchanged. On Windows, use 'strict' error handler if |
| 57 | + the file system encoding is 'mbcs' (which is the default encoding). |
| 58 | + """ |
| 59 | + if isinstance(filename, six.binary_type): |
| 60 | + return filename |
| 61 | + elif isinstance(filename, six.text_type): |
| 62 | + return filename.encode(encoding, errors) |
| 63 | + else: |
| 64 | + raise TypeError("expect bytes or str, not %s" % type(filename).__name__) |
| 65 | + |
| 66 | + def fsdecode(filename): |
| 67 | + """ |
| 68 | + Decode filename from the filesystem encoding with 'surrogateescape' error |
| 69 | + handler, return str unchanged. On Windows, use 'strict' error handler if |
| 70 | + the file system encoding is 'mbcs' (which is the default encoding). |
| 71 | + """ |
| 72 | + if isinstance(filename, six.text_type): |
| 73 | + return filename |
| 74 | + elif isinstance(filename, six.binary_type): |
| 75 | + return filename.decode(encoding, errors) |
| 76 | + else: |
| 77 | + raise TypeError("expect bytes or str, not %s" % type(filename).__name__) |
| 78 | + |
| 79 | + return fsencode, fsdecode |
| 80 | + |
| 81 | +_fsencode, _fsdecode = _fscodec() |
| 82 | +del _fscodec |
| 83 | + |
| 84 | +try: |
| 85 | + from os import fsencode |
| 86 | +except ImportError: |
| 87 | + fsencode = _fsencode |
| 88 | + |
| 89 | +try: |
| 90 | + from os import fsdecode |
| 91 | +except ImportError: |
| 92 | + fsdecode = _fsdecode |
0 commit comments