##// END OF EJS Templates
Remove redundant Python 2 code
Hugo -
Show More
@@ -21,5 +21,6 b' __pycache__'
21 21 .DS_Store
22 22 \#*#
23 23 .#*
24 .cache
24 25 .coverage
25 26 *.swp
@@ -218,8 +218,8 b' class ScriptMagics(Magics):'
218 218 print("Error while terminating subprocess (pid=%i): %s" \
219 219 % (p.pid, e))
220 220 return
221 out = py3compat.bytes_to_str(out)
222 err = py3compat.bytes_to_str(err)
221 out = py3compat.decode(out)
222 err = py3compat.decode(err)
223 223 if args.out:
224 224 self.shell.user_ns[args.out] = out
225 225 else:
@@ -10,7 +10,6 b' from traitlets import Unicode'
10 10
11 11 from IPython.core.application import BaseIPythonApplication
12 12 from IPython.testing import decorators as dec
13 from IPython.utils import py3compat
14 13 from IPython.utils.tempdir import TemporaryDirectory
15 14
16 15
@@ -22,7 +22,6 b' import nose.tools as nt'
22 22
23 23 # Our own imports
24 24 from IPython.core import compilerop
25 from IPython.utils import py3compat
26 25
27 26 #-----------------------------------------------------------------------------
28 27 # Test functions
@@ -17,7 +17,6 b' from os.path import join'
17 17 import nose.tools as nt
18 18
19 19 from IPython.core.completerlib import magic_run_completer, module_completion
20 from IPython.utils import py3compat
21 20 from IPython.utils.tempdir import TemporaryDirectory
22 21 from IPython.testing.decorators import onlyif_unicode_paths
23 22
@@ -19,7 +19,6 b' import nose.tools as nt'
19 19 from traitlets.config.loader import Config
20 20 from IPython.utils.tempdir import TemporaryDirectory
21 21 from IPython.core.history import HistoryManager, extract_hist_ranges
22 from IPython.utils import py3compat
23 22
24 23 def setUp():
25 24 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
@@ -17,7 +17,6 b' import sys'
17 17 import tempfile
18 18 import unittest
19 19 from unittest import mock
20 from io import StringIO
21 20
22 21 from os.path import join
23 22
@@ -30,7 +29,6 b' from IPython.testing.decorators import ('
30 29 )
31 30 from IPython.testing import tools as tt
32 31 from IPython.utils.process import find_cmd
33 from IPython.utils import py3compat
34 32
35 33 #-----------------------------------------------------------------------------
36 34 # Globals
@@ -19,10 +19,8 b' from IPython.core.magic import (Magics, magics_class, line_magic,'
19 19 register_line_cell_magic)
20 20 from decorator import decorator
21 21 from IPython import get_ipython
22 from IPython.testing.decorators import skipif
23 22 from IPython.testing.tools import AssertPrints, AssertNotPrints
24 23 from IPython.utils.path import compress_user
25 from IPython.utils import py3compat
26 24
27 25
28 26 #-----------------------------------------------------------------------------
@@ -40,7 +38,7 b' ip = get_ipython()'
40 38 # defined, if any code is inserted above, the following line will need to be
41 39 # updated. Do NOT insert any whitespace between the next line and the function
42 40 # definition below.
43 THIS_LINE_NUMBER = 43 # Put here the actual number of this line
41 THIS_LINE_NUMBER = 41 # Put here the actual number of this line
44 42
45 43 from unittest import TestCase
46 44
@@ -3,7 +3,6 b' import nose.tools as nt'
3 3
4 4 from IPython.core.splitinput import split_user_input, LineInfo
5 5 from IPython.testing import tools as tt
6 from IPython.utils import py3compat
7 6
8 7 tests = [
9 8 ('x=1', ('', '', 'x', '=1')),
@@ -1136,7 +1136,6 b' class VerboseTB(TBTools):'
1136 1136 colorsnormal = colors.Normal # used a lot
1137 1137 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1138 1138 structured_traceback_parts = [head]
1139 if py3compat.PY3:
1140 1139 chained_exceptions_tb_offset = 0
1141 1140 lines_of_context = 3
1142 1141 formatted_exceptions = formatted_exception
@@ -1163,8 +1162,6 b' class VerboseTB(TBTools):'
1163 1162 # the first exception should be on top
1164 1163 for formatted_exception in reversed(formatted_exceptions):
1165 1164 structured_traceback_parts += formatted_exception
1166 else:
1167 structured_traceback_parts += formatted_exception[0]
1168 1165
1169 1166 return structured_traceback_parts
1170 1167
@@ -116,7 +116,6 b' import traceback'
116 116 import types
117 117 import weakref
118 118 from importlib import import_module
119 from IPython.utils.py3compat import PY3
120 119 from imp import reload
121 120
122 121 from IPython.utils import openpy
@@ -11,7 +11,7 b' import random'
11 11
12 12 # Our own
13 13 from IPython.core.error import UsageError
14 from IPython.utils.py3compat import cast_bytes, str_to_bytes
14 from IPython.utils.py3compat import encode
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Globals
@@ -65,7 +65,7 b" def passwd(passphrase=None, algorithm='sha1'):"
65 65
66 66 h = hashlib.new(algorithm)
67 67 salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
68 h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))
68 h.update(encode(passphrase, 'utf-8') + encode(salt, 'ascii'))
69 69
70 70 return ':'.join((algorithm, salt, h.hexdigest()))
71 71
@@ -109,6 +109,6 b' def passwd_check(hashed_passphrase, passphrase):'
109 109 if len(pw_digest) == 0:
110 110 return False
111 111
112 h.update(cast_bytes(passphrase, 'utf-8') + cast_bytes(salt, 'ascii'))
112 h.update(encode(passphrase, 'utf-8') + encode(salt, 'ascii'))
113 113
114 114 return h.hexdigest() == pw_digest
@@ -7,7 +7,7 b' from warnings import warn'
7 7
8 8 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
9 9 from IPython.utils import io
10 from IPython.utils.py3compat import input, cast_unicode_py2
10 from IPython.utils.py3compat import input
11 11 from IPython.utils.terminal import toggle_set_term_title, set_term_title
12 12 from IPython.utils.process import abbrev_cwd
13 13 from traitlets import (
@@ -233,7 +233,7 b' class TerminalInteractiveShell(InteractiveShell):'
233 233 prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
234 234 prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
235 235 while isp.push_accepts_more():
236 line = cast_unicode_py2(input(prompt_text))
236 line = input(prompt_text)
237 237 isp.push(line)
238 238 prompt_text = prompt_continuation
239 239 return isp.source_reset()
@@ -31,8 +31,9 b" we've copied verbatim from numpy."
31 31 # Copyright (c) IPython Development Team.
32 32 # Distributed under the terms of the Modified BSD License.
33 33
34 import sys
35 34 import os
35 import shutil
36 import sys
36 37 import tempfile
37 38 import unittest
38 39 import warnings
@@ -48,9 +49,6 b' from .ipunittest import ipdoctest, ipdocstring'
48 49 # numpy.testing.decorators, we expose all of it here.
49 50 from IPython.external.decorators import *
50 51
51 # For onlyif_cmd_exists decorator
52 from IPython.utils.py3compat import which
53
54 52 #-----------------------------------------------------------------------------
55 53 # Classes and functions
56 54 #-----------------------------------------------------------------------------
@@ -360,7 +358,7 b' def onlyif_cmds_exist(*commands):'
360 358 Decorator to skip test when at least one of `commands` is not found.
361 359 """
362 360 for cmd in commands:
363 if not which(cmd):
361 if not shutil.which(cmd):
364 362 return skip("This test runs only if command '{0}' "
365 363 "is installed".format(cmd))
366 364 return null_deco
@@ -372,7 +370,7 b' def onlyif_any_cmd_exists(*commands):'
372 370 warnings.warn("The function `onlyif_any_cmd_exists` is deprecated since IPython 4.0",
373 371 DeprecationWarning, stacklevel=2)
374 372 for cmd in commands:
375 if which(cmd):
373 if shutil.which(cmd):
376 374 return null_deco
377 375 return skip("This test runs only if one of the commands {0} "
378 376 "is installed".format(commands))
@@ -11,13 +11,13 b' done.'
11 11
12 12 import builtins as builtin_mod
13 13 import sys
14 import types
14 15 import warnings
15 16
16 17 from . import tools
17 18
18 19 from IPython.core import page
19 20 from IPython.utils import io
20 from IPython.utils import py3compat
21 21 from IPython.terminal.interactiveshell import TerminalInteractiveShell
22 22
23 23
@@ -104,9 +104,9 b' def start_ipython():'
104 104 # Modify the IPython system call with one that uses getoutput, so that we
105 105 # can capture subcommands and print them to Python's stdout, otherwise the
106 106 # doctest machinery would miss them.
107 shell.system = py3compat.MethodType(xsys, shell)
107 shell.system = types.MethodType(xsys, shell)
108 108
109 shell._showtraceback = py3compat.MethodType(_showtraceback, shell)
109 shell._showtraceback = types.MethodType(_showtraceback, shell)
110 110
111 111 # IPython is ready, now clean up some global state...
112 112
@@ -34,7 +34,7 b' from nose.plugins import Plugin'
34 34 from nose.util import safe_str
35 35
36 36 from IPython import version_info
37 from IPython.utils.py3compat import bytes_to_str
37 from IPython.utils.py3compat import decode
38 38 from IPython.utils.importstring import import_item
39 39 from IPython.testing.plugin.ipdoctest import IPythonDoctest
40 40 from IPython.external.decorators import KnownFailure, knownfailureif
@@ -299,7 +299,7 b' class StreamCapturer(Thread):'
299 299 with self.buffer_lock:
300 300 self.buffer.write(chunk)
301 301 if self.echo:
302 sys.stdout.write(bytes_to_str(chunk))
302 sys.stdout.write(decode(chunk))
303 303
304 304 os.close(self.readfd)
305 305 os.close(self.writefd)
@@ -24,7 +24,7 b' from .iptest import ('
24 24 have, test_group_names as py_test_group_names, test_sections, StreamCapturer,
25 25 )
26 26 from IPython.utils.path import compress_user
27 from IPython.utils.py3compat import bytes_to_str
27 from IPython.utils.py3compat import decode
28 28 from IPython.utils.sysinfo import get_sys_info
29 29 from IPython.utils.tempdir import TemporaryDirectory
30 30
@@ -371,7 +371,7 b' def run_iptestall(options):'
371 371 print(justify('Test group: ' + controller.section, res_string))
372 372 if res:
373 373 controller.print_extra_info()
374 print(bytes_to_str(controller.stdout))
374 print(decode(controller.stdout))
375 375 failed.append(controller)
376 376 if res == -signal.SIGINT:
377 377 print("Interrupted")
@@ -33,7 +33,6 b' from IPython.utils.process import get_output_error_code'
33 33 from IPython.utils.text import list_strings
34 34 from IPython.utils.io import temp_pyfile, Tee
35 35 from IPython.utils import py3compat
36 from IPython.utils.encoding import DEFAULT_ENCODING
37 36
38 37 from . import decorators as dec
39 38 from . import skipdoctest
@@ -210,8 +209,8 b' def ipexec(fname, options=None, commands=()):'
210 209 if not isinstance(v, str):
211 210 print(k, v)
212 211 p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env)
213 out, err = p.communicate(input=py3compat.str_to_bytes('\n'.join(commands)) or None)
214 out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
212 out, err = p.communicate(input=py3compat.encode('\n'.join(commands)) or None)
213 out, err = py3compat.decode(out), py3compat.decode(err)
215 214 # `import readline` causes 'ESC[?1034h' to be output sometimes,
216 215 # so strip that out before doing comparisons
217 216 if out:
@@ -28,7 +28,7 b' def _find_cmd(cmd):'
28 28 for path in paths:
29 29 filename = os.path.join(path, cmd)
30 30 if System.IO.File.Exists(filename):
31 return py3compat.bytes_to_str(filename)
31 return py3compat.decode(filename)
32 32 raise OSError("command %r not found" % cmd)
33 33
34 34 def system(cmd):
@@ -130,7 +130,7 b' def getoutput(cmd):'
130 130 out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT)
131 131 if out is None:
132 132 return ''
133 return py3compat.bytes_to_str(out)
133 return py3compat.decode(out)
134 134
135 135
136 136 def getoutputerror(cmd):
@@ -172,7 +172,7 b' def get_output_error_code(cmd):'
172 172 if out_err is None:
173 173 return '', '', p.returncode
174 174 out, err = out_err
175 return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err), p.returncode
175 return py3compat.decode(out), py3compat.decode(err), p.returncode
176 176
177 177 def arg_split(s, posix=False, strict=True):
178 178 """Split a command line's arguments in a shell-like manner.
@@ -36,7 +36,7 b' def _find_cmd(cmd):'
36 36
37 37 path = sp.Popen(['/usr/bin/env', 'which', cmd],
38 38 stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
39 return py3compat.bytes_to_str(path)
39 return py3compat.decode(path)
40 40
41 41
42 42 class ProcessHandler(object):
@@ -152,7 +152,7 b' def getoutput(cmd):'
152 152
153 153 if out is None:
154 154 out = b''
155 return py3compat.bytes_to_str(out)
155 return py3compat.decode(out)
156 156
157 157 try:
158 158 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
@@ -15,9 +15,6 b' This file is meant to be used by process.py'
15 15 import os, sys, threading
16 16 import ctypes, msvcrt
17 17
18 # local imports
19 from . import py3compat
20
21 18 # Win32 API types needed for the API calls
22 19 from ctypes import POINTER
23 20 from ctypes.wintypes import HANDLE, HLOCAL, LPVOID, WORD, DWORD, BOOL, \
@@ -8,6 +8,7 b' Utilities for working with external processes.'
8 8
9 9
10 10 import os
11 import shutil
11 12 import sys
12 13
13 14 if sys.platform == 'win32':
@@ -18,7 +19,6 b' else:'
18 19 from ._process_posix import system, getoutput, arg_split, check_pid
19 20
20 21 from ._process_common import getoutputerror, get_output_error_code, process_handler
21 from . import py3compat
22 22
23 23
24 24 class FindCmdError(Exception):
@@ -44,7 +44,7 b' def find_cmd(cmd):'
44 44 cmd : str
45 45 The command line program to look for.
46 46 """
47 path = py3compat.which(cmd)
47 path = shutil.which(cmd)
48 48 if path is None:
49 49 raise FindCmdError('command could not be found: %s' % cmd)
50 50 return path
@@ -143,7 +143,6 b' def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None):'
143 143 return name
144 144 return None
145 145
146 if sys.version_info[0] >= 3:
147 146 PY3 = True
148 147
149 148 # keep reference to builtin_mod because the kernel overrides that value
@@ -211,86 +210,6 b' if sys.version_info[0] >= 3:'
211 210 """Get a function's closure attribute"""
212 211 return f.__closure__
213 212
214 else:
215 PY3 = False
216
217 # keep reference to builtin_mod because the kernel overrides that value
218 # to forward requests to a frontend.
219 def input(prompt=''):
220 return builtin_mod.raw_input(prompt)
221
222 builtin_mod_name = "__builtin__"
223 import __builtin__ as builtin_mod
224
225 str_to_unicode = decode
226 unicode_to_str = encode
227 str_to_bytes = no_code
228 bytes_to_str = no_code
229 cast_bytes_py2 = cast_bytes
230 cast_unicode_py2 = cast_unicode
231 buffer_to_bytes_py2 = buffer_to_bytes
232
233 string_types = (str, unicode)
234 unicode_type = unicode
235
236 import re
237 _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
238 def isidentifier(s, dotted=False):
239 if dotted:
240 return all(isidentifier(a) for a in s.split("."))
241 return bool(_name_re.match(s))
242
243 xrange = xrange
244 def iteritems(d): return d.iteritems()
245 def itervalues(d): return d.itervalues()
246 getcwd = os.getcwdu
247
248 def MethodType(func, instance):
249 return types.MethodType(func, instance, type(instance))
250
251 def doctest_refactor_print(func_or_str):
252 return func_or_str
253
254 def get_closure(f):
255 """Get a function's closure attribute"""
256 return f.func_closure
257
258 which = _shutil_which
259
260 # Abstract u'abc' syntax:
261 @_modify_str_or_docstring
262 def u_format(s):
263 """"{u}'abc'" --> "u'abc'" (Python 2)
264
265 Accepts a string or a function, so it can be used as a decorator."""
266 return s.format(u='u')
267
268 if sys.platform == 'win32':
269 def execfile(fname, glob=None, loc=None, compiler=None):
270 loc = loc if (loc is not None) else glob
271 scripttext = builtin_mod.open(fname).read()+ '\n'
272 # compile converts unicode filename to str assuming
273 # ascii. Let's do the conversion before calling compile
274 if isinstance(fname, unicode):
275 filename = unicode_to_str(fname)
276 else:
277 filename = fname
278 compiler = compiler or compile
279 exec(compiler(scripttext, filename, 'exec'), glob, loc)
280
281 else:
282 def execfile(fname, glob=None, loc=None, compiler=None):
283 if isinstance(fname, unicode):
284 filename = fname.encode(sys.getfilesystemencoding())
285 else:
286 filename = fname
287 where = [ns for ns in [glob, loc] if ns is not None]
288 if compiler is None:
289 builtin_mod.execfile(filename, *where)
290 else:
291 scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
292 exec(compiler(scripttext, filename, 'exec'), glob, loc)
293
294 213
295 214 PY2 = not PY3
296 215 PYPY = platform.python_implementation() == "PyPy"
@@ -91,11 +91,7 b" Expfloat = r'[0-9]+' + Exponent"
91 91 Floatnumber = group(Pointfloat, Expfloat)
92 92 Imagnumber = group(r'[0-9]+[jJ]', Floatnumber + r'[jJ]')
93 93 Number = group(Imagnumber, Floatnumber, Intnumber)
94
95 if sys.version_info.minor >= 3:
96 94 StringPrefix = r'(?:[bB][rR]?|[rR][bB]?|[uU])?'
97 else:
98 StringPrefix = r'(?:[bB]?[rR]?)?'
99 95
100 96 # Tail end of ' string.
101 97 Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
@@ -165,8 +161,6 b' for t in ("\'", \'"\','
165 161 "bR'", 'bR"', "BR'", 'BR"' ):
166 162 single_quoted[t] = t
167 163
168 if sys.version_info.minor >= 3:
169 # Python 3.3
170 164 for _prefix in ['rb', 'rB', 'Rb', 'RB', 'u', 'U']:
171 165 _t2 = _prefix+'"""'
172 166 endprogs[_t2] = double3prog
@@ -40,12 +40,6 b' def oscmd(s):'
40 40 print(">", s)
41 41 os.system(s)
42 42
43 # Py3 compatibility hacks, without assuming IPython itself is installed with
44 # the full py3compat machinery.
45
46 try:
47 execfile
48 except NameError:
49 43 def execfile(fname, globs, locs=None):
50 44 locs = locs or globs
51 45 exec(compile(open(fname).read(), fname, "exec"), globs, locs)
@@ -10,7 +10,7 b''
10 10 ; tox -- --all -j # Runs "iptest --all -j" in every environment
11 11
12 12 [tox]
13 envlist = py{36,35,34,33,27,py}
13 envlist = py{36,35,34,33,py}
14 14 skip_missing_interpreters = True
15 15 toxworkdir = /tmp/tox_ipython
16 16
@@ -19,7 +19,7 b' toxworkdir = /tmp/tox_ipython'
19 19 ; Other IPython/testing dependencies should be in setup.py, not here
20 20 deps =
21 21 pypy: https://bitbucket.org/pypy/numpy/get/master.zip
22 py{36,35,34,33,27}: matplotlib
22 py{36,35,34,33}: matplotlib
23 23 .[test]
24 24
25 25 ; It's just to avoid loading the IPython package in the current directory
General Comments 0
You need to be logged in to leave comments. Login now