Show More
@@ -1,194 +1,191 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Compatibility tricks for Python 3. Mainly to do with unicode. |
|
3 | 3 | |
|
4 | 4 | This file is deprecated and will be removed in a future version. |
|
5 | 5 | """ |
|
6 | 6 | import functools |
|
7 | 7 | import os |
|
8 | 8 | import sys |
|
9 | 9 | import re |
|
10 | 10 | import shutil |
|
11 | 11 | import types |
|
12 | 12 | import platform |
|
13 | 13 | |
|
14 | 14 | from .encoding import DEFAULT_ENCODING |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | def decode(s, encoding=None): |
|
18 | 18 | encoding = encoding or DEFAULT_ENCODING |
|
19 | 19 | return s.decode(encoding, "replace") |
|
20 | 20 | |
|
21 | 21 | def encode(u, encoding=None): |
|
22 | 22 | encoding = encoding or DEFAULT_ENCODING |
|
23 | 23 | return u.encode(encoding, "replace") |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def cast_unicode(s, encoding=None): |
|
27 | 27 | if isinstance(s, bytes): |
|
28 | 28 | return decode(s, encoding) |
|
29 | 29 | return s |
|
30 | 30 | |
|
31 | 31 | def cast_bytes(s, encoding=None): |
|
32 | 32 | if not isinstance(s, bytes): |
|
33 | 33 | return encode(s, encoding) |
|
34 | 34 | return s |
|
35 | 35 | |
|
36 | 36 | def buffer_to_bytes(buf): |
|
37 | 37 | """Cast a buffer object to bytes""" |
|
38 | 38 | if not isinstance(buf, bytes): |
|
39 | 39 | buf = bytes(buf) |
|
40 | 40 | return buf |
|
41 | 41 | |
|
42 | 42 | def _modify_str_or_docstring(str_change_func): |
|
43 | 43 | @functools.wraps(str_change_func) |
|
44 | 44 | def wrapper(func_or_str): |
|
45 | 45 | if isinstance(func_or_str, (str,)): |
|
46 | 46 | func = None |
|
47 | 47 | doc = func_or_str |
|
48 | 48 | else: |
|
49 | 49 | func = func_or_str |
|
50 | 50 | doc = func.__doc__ |
|
51 | 51 | |
|
52 | 52 | # PYTHONOPTIMIZE=2 strips docstrings, so they can disappear unexpectedly |
|
53 | 53 | if doc is not None: |
|
54 | 54 | doc = str_change_func(doc) |
|
55 | 55 | |
|
56 | 56 | if func: |
|
57 | 57 | func.__doc__ = doc |
|
58 | 58 | return func |
|
59 | 59 | return doc |
|
60 | 60 | return wrapper |
|
61 | 61 | |
|
62 | 62 | def safe_unicode(e): |
|
63 | 63 | """unicode(e) with various fallbacks. Used for exceptions, which may not be |
|
64 | 64 | safe to call unicode() on. |
|
65 | 65 | """ |
|
66 | 66 | try: |
|
67 | 67 | return str(e) |
|
68 | 68 | except UnicodeError: |
|
69 | 69 | pass |
|
70 | 70 | |
|
71 | 71 | try: |
|
72 | 72 | return repr(e) |
|
73 | 73 | except UnicodeError: |
|
74 | 74 | pass |
|
75 | 75 | |
|
76 | 76 | return u'Unrecoverably corrupt evalue' |
|
77 | 77 | |
|
78 | 78 | # shutil.which from Python 3.4 |
|
79 | 79 | def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None): |
|
80 | 80 | """Given a command, mode, and a PATH string, return the path which |
|
81 | 81 | conforms to the given mode on the PATH, or None if there is no such |
|
82 | 82 | file. |
|
83 | 83 | |
|
84 | 84 | `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
|
85 | 85 | of os.environ.get("PATH"), or can be overridden with a custom search |
|
86 | 86 | path. |
|
87 | 87 | |
|
88 | 88 | This is a backport of shutil.which from Python 3.4 |
|
89 | 89 | """ |
|
90 | 90 | # Check that a given file can be accessed with the correct mode. |
|
91 | 91 | # Additionally check that `file` is not a directory, as on Windows |
|
92 | 92 | # directories pass the os.access check. |
|
93 | 93 | def _access_check(fn, mode): |
|
94 | 94 | return (os.path.exists(fn) and os.access(fn, mode) |
|
95 | 95 | and not os.path.isdir(fn)) |
|
96 | 96 | |
|
97 | 97 | # If we're given a path with a directory part, look it up directly rather |
|
98 | 98 | # than referring to PATH directories. This includes checking relative to the |
|
99 | 99 | # current directory, e.g. ./script |
|
100 | 100 | if os.path.dirname(cmd): |
|
101 | 101 | if _access_check(cmd, mode): |
|
102 | 102 | return cmd |
|
103 | 103 | return None |
|
104 | 104 | |
|
105 | 105 | if path is None: |
|
106 | 106 | path = os.environ.get("PATH", os.defpath) |
|
107 | 107 | if not path: |
|
108 | 108 | return None |
|
109 | 109 | path = path.split(os.pathsep) |
|
110 | 110 | |
|
111 | 111 | if sys.platform == "win32": |
|
112 | 112 | # The current directory takes precedence on Windows. |
|
113 | 113 | if not os.curdir in path: |
|
114 | 114 | path.insert(0, os.curdir) |
|
115 | 115 | |
|
116 | 116 | # PATHEXT is necessary to check on Windows. |
|
117 | 117 | pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
|
118 | 118 | # See if the given file matches any of the expected path extensions. |
|
119 | 119 | # This will allow us to short circuit when given "python.exe". |
|
120 | 120 | # If it does match, only test that one, otherwise we have to try |
|
121 | 121 | # others. |
|
122 | 122 | if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
|
123 | 123 | files = [cmd] |
|
124 | 124 | else: |
|
125 | 125 | files = [cmd + ext for ext in pathext] |
|
126 | 126 | else: |
|
127 | 127 | # On other platforms you don't have things like PATHEXT to tell you |
|
128 | 128 | # what file suffixes are executable, so just pass on cmd as-is. |
|
129 | 129 | files = [cmd] |
|
130 | 130 | |
|
131 | 131 | seen = set() |
|
132 | 132 | for dir in path: |
|
133 | 133 | normdir = os.path.normcase(dir) |
|
134 | 134 | if not normdir in seen: |
|
135 | 135 | seen.add(normdir) |
|
136 | 136 | for thefile in files: |
|
137 | 137 | name = os.path.join(dir, thefile) |
|
138 | 138 | if _access_check(name, mode): |
|
139 | 139 | return name |
|
140 | 140 | return None |
|
141 | 141 | |
|
142 | 142 | PY3 = True |
|
143 | 143 | |
|
144 | 144 | # keep reference to builtin_mod because the kernel overrides that value |
|
145 | 145 | # to forward requests to a frontend. |
|
146 | 146 | def input(prompt=''): |
|
147 | 147 | return builtin_mod.input(prompt) |
|
148 | 148 | |
|
149 | 149 | builtin_mod_name = "builtins" |
|
150 | 150 | import builtins as builtin_mod |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | which = shutil.which |
|
154 | 154 | |
|
155 | 155 | def isidentifier(s, dotted=False): |
|
156 | 156 | if dotted: |
|
157 | 157 | return all(isidentifier(a) for a in s.split(".")) |
|
158 | 158 | return s.isidentifier() |
|
159 | 159 | |
|
160 | 160 | getcwd = os.getcwd |
|
161 | 161 | |
|
162 | 162 | MethodType = types.MethodType |
|
163 | 163 | |
|
164 | 164 | def execfile(fname, glob, loc=None, compiler=None): |
|
165 | 165 | loc = loc if (loc is not None) else glob |
|
166 | 166 | with open(fname, 'rb') as f: |
|
167 | 167 | compiler = compiler or compile |
|
168 | 168 | exec(compiler(f.read(), fname, 'exec'), glob, loc) |
|
169 | 169 | |
|
170 | 170 | # Refactor print statements in doctests. |
|
171 | 171 | _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE) |
|
172 | def _print_statement_sub(match): | |
|
173 | expr = match.groups('expr') | |
|
174 | return "print(%s)" % expr | |
|
175 | 172 | |
|
176 | 173 | # Abstract u'abc' syntax: |
|
177 | 174 | @_modify_str_or_docstring |
|
178 | 175 | def u_format(s): |
|
179 | 176 | """"{u}'abc'" --> "'abc'" (Python 3) |
|
180 | 177 | |
|
181 | 178 | Accepts a string or a function, so it can be used as a decorator.""" |
|
182 | 179 | return s.format(u='') |
|
183 | 180 | |
|
184 | 181 | |
|
185 | 182 | PY2 = not PY3 |
|
186 | 183 | PYPY = platform.python_implementation() == "PyPy" |
|
187 | 184 | |
|
188 | 185 | # Cython still rely on that as a Dec 28 2019 |
|
189 | 186 | # See https://github.com/cython/cython/pull/3291 and |
|
190 | 187 | # https://github.com/ipython/ipython/issues/12068 |
|
191 | 188 | def no_code(x, encoding=None): |
|
192 | 189 | return x |
|
193 | 190 | unicode_to_str = cast_bytes_py2 = no_code |
|
194 | 191 |
General Comments 0
You need to be logged in to leave comments.
Login now