py3k.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """
  2. Python 3.X compatibility tools.
  3. While this file was originally intended for Python 2 -> 3 transition,
  4. it is now used to create a compatibility layer between different
  5. minor versions of Python 3.
  6. While the active version of numpy may not support a given version of python, we
  7. allow downstream libraries to continue to use these shims for forward
  8. compatibility with numpy while they transition their code to newer versions of
  9. Python.
  10. """
  11. __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
  12. 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
  13. 'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
  14. 'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path',
  15. 'pickle', 'contextlib_nullcontext', 'os_fspath', 'os_PathLike']
  16. import sys
  17. import os
  18. from pathlib import Path
  19. import io
  20. import abc
  21. from abc import ABC as abc_ABC
  22. try:
  23. import pickle5 as pickle
  24. except ImportError:
  25. import pickle
  26. long = int
  27. integer_types = (int,)
  28. basestring = str
  29. unicode = str
  30. bytes = bytes
  31. def asunicode(s):
  32. if isinstance(s, bytes):
  33. return s.decode('latin1')
  34. return str(s)
  35. def asbytes(s):
  36. if isinstance(s, bytes):
  37. return s
  38. return str(s).encode('latin1')
  39. def asstr(s):
  40. if isinstance(s, bytes):
  41. return s.decode('latin1')
  42. return str(s)
  43. def isfileobj(f):
  44. return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
  45. def open_latin1(filename, mode='r'):
  46. return open(filename, mode=mode, encoding='iso-8859-1')
  47. def sixu(s):
  48. return s
  49. strchar = 'U'
  50. def getexception():
  51. return sys.exc_info()[1]
  52. def asbytes_nested(x):
  53. if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
  54. return [asbytes_nested(y) for y in x]
  55. else:
  56. return asbytes(x)
  57. def asunicode_nested(x):
  58. if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
  59. return [asunicode_nested(y) for y in x]
  60. else:
  61. return asunicode(x)
  62. def is_pathlib_path(obj):
  63. """
  64. Check whether obj is a `pathlib.Path` object.
  65. Prefer using ``isinstance(obj, os.PathLike)`` instead of this function.
  66. """
  67. return isinstance(obj, Path)
  68. # from Python 3.7
  69. class contextlib_nullcontext:
  70. """Context manager that does no additional processing.
  71. Used as a stand-in for a normal context manager, when a particular
  72. block of code is only sometimes used with a normal context manager:
  73. cm = optional_cm if condition else nullcontext()
  74. with cm:
  75. # Perform operation, using optional_cm if condition is True
  76. .. note::
  77. Prefer using `contextlib.nullcontext` instead of this context manager.
  78. """
  79. def __init__(self, enter_result=None):
  80. self.enter_result = enter_result
  81. def __enter__(self):
  82. return self.enter_result
  83. def __exit__(self, *excinfo):
  84. pass
  85. def npy_load_module(name, fn, info=None):
  86. """
  87. Load a module.
  88. .. versionadded:: 1.11.2
  89. Parameters
  90. ----------
  91. name : str
  92. Full module name.
  93. fn : str
  94. Path to module file.
  95. info : tuple, optional
  96. Only here for backward compatibility with Python 2.*.
  97. Returns
  98. -------
  99. mod : module
  100. """
  101. # Explicitly lazy import this to avoid paying the cost
  102. # of importing importlib at startup
  103. from importlib.machinery import SourceFileLoader
  104. return SourceFileLoader(name, fn).load_module()
  105. os_fspath = os.fspath
  106. os_PathLike = os.PathLike