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