Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/numpy/compat/py3k.py: 47%
57 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2Python 3.X compatibility tools.
4While this file was originally intended for Python 2 -> 3 transition,
5it is now used to create a compatibility layer between different
6minor versions of Python 3.
8While the active version of numpy may not support a given version of python, we
9allow downstream libraries to continue to use these shims for forward
10compatibility with numpy while they transition their code to newer versions of
11Python.
12"""
13__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
14 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
15 'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
16 'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path',
17 'pickle', 'contextlib_nullcontext', 'os_fspath', 'os_PathLike']
19import sys
20import os
21from pathlib import Path
22import io
23try:
24 import pickle5 as pickle
25except ImportError:
26 import pickle
28long = int
29integer_types = (int,)
30basestring = str
31unicode = str
32bytes = bytes
34def asunicode(s):
35 if isinstance(s, bytes):
36 return s.decode('latin1')
37 return str(s)
39def asbytes(s):
40 if isinstance(s, bytes):
41 return s
42 return str(s).encode('latin1')
44def asstr(s):
45 if isinstance(s, bytes):
46 return s.decode('latin1')
47 return str(s)
49def isfileobj(f):
50 return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
52def open_latin1(filename, mode='r'):
53 return open(filename, mode=mode, encoding='iso-8859-1')
55def sixu(s):
56 return s
58strchar = 'U'
60def getexception():
61 return sys.exc_info()[1]
63def asbytes_nested(x):
64 if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
65 return [asbytes_nested(y) for y in x]
66 else:
67 return asbytes(x)
69def asunicode_nested(x):
70 if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
71 return [asunicode_nested(y) for y in x]
72 else:
73 return asunicode(x)
75def is_pathlib_path(obj):
76 """
77 Check whether obj is a `pathlib.Path` object.
79 Prefer using ``isinstance(obj, os.PathLike)`` instead of this function.
80 """
81 return isinstance(obj, Path)
83# from Python 3.7
84class contextlib_nullcontext:
85 """Context manager that does no additional processing.
87 Used as a stand-in for a normal context manager, when a particular
88 block of code is only sometimes used with a normal context manager:
90 cm = optional_cm if condition else nullcontext()
91 with cm:
92 # Perform operation, using optional_cm if condition is True
94 .. note::
95 Prefer using `contextlib.nullcontext` instead of this context manager.
96 """
98 def __init__(self, enter_result=None):
99 self.enter_result = enter_result
101 def __enter__(self):
102 return self.enter_result
104 def __exit__(self, *excinfo):
105 pass
108def npy_load_module(name, fn, info=None):
109 """
110 Load a module. Uses ``load_module`` which will be deprecated in python
111 3.12. An alternative that uses ``exec_module`` is in
112 numpy.distutils.misc_util.exec_mod_from_location
114 .. versionadded:: 1.11.2
116 Parameters
117 ----------
118 name : str
119 Full module name.
120 fn : str
121 Path to module file.
122 info : tuple, optional
123 Only here for backward compatibility with Python 2.*.
125 Returns
126 -------
127 mod : module
129 """
130 # Explicitly lazy import this to avoid paying the cost
131 # of importing importlib at startup
132 from importlib.machinery import SourceFileLoader
133 return SourceFileLoader(name, fn).load_module()
136os_fspath = os.fspath
137os_PathLike = os.PathLike