##// END OF EJS Templates
Merge pull request #10135 from srinivasreddy/dead_code...
Matthias Bussonnier -
r23101:eadc81ae merge
parent child Browse files
Show More
@@ -35,7 +35,7 b' from IPython.utils.path import compress_user'
35 from IPython.utils.text import indent
35 from IPython.utils.text import indent
36 from IPython.utils.wildcard import list_namespace
36 from IPython.utils.wildcard import list_namespace
37 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
37 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
38 from IPython.utils.py3compat import cast_unicode, PY3
38 from IPython.utils.py3compat import cast_unicode
39 from IPython.utils.colorable import Colorable
39 from IPython.utils.colorable import Colorable
40 from IPython.utils.decorators import undoc
40 from IPython.utils.decorators import undoc
41
41
@@ -216,7 +216,7 b' def getargspec(obj):'
216 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
216 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
217 obj = obj.__call__
217 obj = obj.__call__
218
218
219 return inspect.getfullargspec(obj) if PY3 else inspect.getargspec(obj)
219 return inspect.getfullargspec(obj)
220
220
221
221
222 def format_argspec(argspec):
222 def format_argspec(argspec):
@@ -403,8 +403,7 b' class Inspector(Colorable):'
403
403
404 if inspect.isclass(obj):
404 if inspect.isclass(obj):
405 header = self.__head('Class constructor information:\n')
405 header = self.__head('Class constructor information:\n')
406 elif (not py3compat.PY3) and type(obj) is types.InstanceType:
406
407 obj = obj.__call__
408
407
409 output = self._getdef(obj,oname)
408 output = self._getdef(obj,oname)
410 if output is None:
409 if output is None:
@@ -636,13 +635,7 b' class Inspector(Colorable):'
636 # General Python objects
635 # General Python objects
637 append_field(_mime, 'Signature', 'definition', code_formatter)
636 append_field(_mime, 'Signature', 'definition', code_formatter)
638 append_field(_mime, 'Call signature', 'call_def', code_formatter)
637 append_field(_mime, 'Call signature', 'call_def', code_formatter)
639
640 append_field(_mime, 'Type', 'type_name')
638 append_field(_mime, 'Type', 'type_name')
641
642 # Base class for old-style instances
643 if (not py3compat.PY3) and isinstance(obj, types.InstanceType) and info['base_class']:
644 append_field(_mime, 'Base Class', 'base_class')
645
646 append_field(_mime, 'String form', 'string_form')
639 append_field(_mime, 'String form', 'string_form')
647
640
648 # Namespace
641 # Namespace
@@ -51,7 +51,7 b' def test_cache():'
51 def setUp():
51 def setUp():
52 # Check we're in a proper Python 2 environment (some imports, such
52 # Check we're in a proper Python 2 environment (some imports, such
53 # as GTK, can change the default encoding, which can hide bugs.)
53 # as GTK, can change the default encoding, which can hide bugs.)
54 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
54 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
55
55
56 def test_cache_unicode():
56 def test_cache_unicode():
57 cp = compilerop.CachingCompiler()
57 cp = compilerop.CachingCompiler()
@@ -22,7 +22,7 b' from IPython.core.history import HistoryManager, extract_hist_ranges'
22 from IPython.utils import py3compat
22 from IPython.utils import py3compat
23
23
24 def setUp():
24 def setUp():
25 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
25 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
26
26
27 def test_history():
27 def test_history():
28 ip = get_ipython()
28 ip = get_ipython()
@@ -17,6 +17,7 b' import sys'
17 import tempfile
17 import tempfile
18 import unittest
18 import unittest
19 from unittest import mock
19 from unittest import mock
20 from io import StringIO
20
21
21 from os.path import join
22 from os.path import join
22
23
@@ -30,12 +31,6 b' from IPython.testing.decorators import ('
30 from IPython.testing import tools as tt
31 from IPython.testing import tools as tt
31 from IPython.utils.process import find_cmd
32 from IPython.utils.process import find_cmd
32 from IPython.utils import py3compat
33 from IPython.utils import py3compat
33 from IPython.utils.py3compat import PY3
34
35 if PY3:
36 from io import StringIO
37 else:
38 from StringIO import StringIO
39
34
40 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
41 # Globals
36 # Globals
@@ -9,12 +9,8 b' import os'
9 import sys
9 import sys
10 import warnings
10 import warnings
11 from unittest import TestCase
11 from unittest import TestCase
12
12 from importlib import invalidate_caches
13 try:
13 from io import StringIO
14 from importlib import invalidate_caches # Required from Python 3.3
15 except ImportError:
16 def invalidate_caches():
17 pass
18
14
19 import nose.tools as nt
15 import nose.tools as nt
20
16
@@ -32,10 +28,6 b' from IPython.utils.io import capture_output'
32 from IPython.utils.tempdir import TemporaryDirectory
28 from IPython.utils.tempdir import TemporaryDirectory
33 from IPython.utils.process import find_cmd
29 from IPython.utils.process import find_cmd
34
30
35 if py3compat.PY3:
36 from io import StringIO
37 else:
38 from StringIO import StringIO
39
31
40
32
41 _ip = get_ipython()
33 _ip = get_ipython()
@@ -8,17 +8,12 b' Needs to be run by nose (to make ipython session available).'
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 import sys
10 import sys
11 from io import StringIO
11 from unittest import TestCase
12 from unittest import TestCase
12
13
13 import nose.tools as nt
14 import nose.tools as nt
14
15
15 from IPython.testing import tools as tt
16 from IPython.testing import tools as tt
16 from IPython.utils.py3compat import PY3
17
18 if PY3:
19 from io import StringIO
20 else:
21 from StringIO import StringIO
22
17
23 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
24 # Globals
19 # Globals
@@ -227,8 +227,7 b' def test_info():'
227 nt.assert_equal(i['docstring'], Call.__doc__)
227 nt.assert_equal(i['docstring'], Call.__doc__)
228 nt.assert_equal(i['source'], None)
228 nt.assert_equal(i['source'], None)
229 nt.assert_true(i['isclass'])
229 nt.assert_true(i['isclass'])
230 _self_py2 = '' if py3compat.PY3 else 'self, '
230 nt.assert_equal(i['init_definition'], "Call(x, y=1)")
231 nt.assert_equal(i['init_definition'], "Call(%sx, y=1)" % _self_py2)
232 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
231 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
233
232
234 i = inspector.info(Call, detail_level=1)
233 i = inspector.info(Call, detail_level=1)
@@ -244,15 +243,6 b' def test_info():'
244 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
243 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
245 nt.assert_equal(i['call_docstring'], Call.__call__.__doc__)
244 nt.assert_equal(i['call_docstring'], Call.__call__.__doc__)
246
245
247 # Test old-style classes, which for example may not have an __init__ method.
248 if not py3compat.PY3:
249 i = inspector.info(OldStyle)
250 nt.assert_equal(i['type_name'], 'classobj')
251
252 i = inspector.info(OldStyle())
253 nt.assert_equal(i['type_name'], 'instance')
254 nt.assert_equal(i['docstring'], OldStyle.__doc__)
255
256 def test_class_signature():
246 def test_class_signature():
257 info = inspector.info(HasSignature, 'HasSignature')
247 info = inspector.info(HasSignature, 'HasSignature')
258 nt.assert_equal(info['init_definition'], "HasSignature(test)")
248 nt.assert_equal(info['init_definition'], "HasSignature(test)")
@@ -16,7 +16,6 b' from IPython.testing import tools as tt'
16 from IPython.testing.decorators import onlyif_unicode_paths
16 from IPython.testing.decorators import onlyif_unicode_paths
17 from IPython.utils.syspathcontext import prepended_to_syspath
17 from IPython.utils.syspathcontext import prepended_to_syspath
18 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.utils.tempdir import TemporaryDirectory
19 from IPython.utils.py3compat import PY3
20
19
21 ip = get_ipython()
20 ip = get_ipython()
22
21
@@ -226,20 +225,17 b' except Exception:'
226 """
225 """
227
226
228 def test_direct_cause_error(self):
227 def test_direct_cause_error(self):
229 if PY3:
228 with tt.AssertPrints(["KeyError", "NameError", "direct cause"]):
230 with tt.AssertPrints(["KeyError", "NameError", "direct cause"]):
229 ip.run_cell(self.DIRECT_CAUSE_ERROR_CODE)
231 ip.run_cell(self.DIRECT_CAUSE_ERROR_CODE)
232
230
233 def test_exception_during_handling_error(self):
231 def test_exception_during_handling_error(self):
234 if PY3:
232 with tt.AssertPrints(["KeyError", "NameError", "During handling"]):
235 with tt.AssertPrints(["KeyError", "NameError", "During handling"]):
233 ip.run_cell(self.EXCEPTION_DURING_HANDLING_CODE)
236 ip.run_cell(self.EXCEPTION_DURING_HANDLING_CODE)
237
234
238 def test_suppress_exception_chaining(self):
235 def test_suppress_exception_chaining(self):
239 if PY3:
236 with tt.AssertNotPrints("ZeroDivisionError"), \
240 with tt.AssertNotPrints("ZeroDivisionError"), \
237 tt.AssertPrints("ValueError", suppress=False):
241 tt.AssertPrints("ValueError", suppress=False):
238 ip.run_cell(self.SUPPRESS_CHAINING_CODE)
242 ip.run_cell(self.SUPPRESS_CHAINING_CODE)
243
239
244
240
245 class RecursionTest(unittest.TestCase):
241 class RecursionTest(unittest.TestCase):
@@ -260,12 +260,9 b' class ModuleReloader(object):'
260 # superreload
260 # superreload
261 #------------------------------------------------------------------------------
261 #------------------------------------------------------------------------------
262
262
263 if PY3:
263
264 func_attrs = ['__code__', '__defaults__', '__doc__',
264 func_attrs = ['__code__', '__defaults__', '__doc__',
265 '__closure__', '__globals__', '__dict__']
265 '__closure__', '__globals__', '__dict__']
266 else:
267 func_attrs = ['func_code', 'func_defaults', 'func_doc',
268 'func_closure', 'func_globals', 'func_dict']
269
266
270
267
271 def update_function(old, new):
268 def update_function(old, new):
@@ -320,18 +317,9 b' UPDATE_RULES = ['
320 (lambda a, b: isinstance2(a, b, property),
317 (lambda a, b: isinstance2(a, b, property),
321 update_property),
318 update_property),
322 ]
319 ]
323
320 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
324
321 lambda a, b: update_function(a.__func__, b.__func__)),
325 if PY3:
322 ])
326 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
327 lambda a, b: update_function(a.__func__, b.__func__)),
328 ])
329 else:
330 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
331 update_class),
332 (lambda a, b: isinstance2(a, b, types.MethodType),
333 lambda a, b: update_function(a.__func__, b.__func__)),
334 ])
335
323
336
324
337 def update_generic(a, b):
325 def update_generic(a, b):
@@ -18,18 +18,13 b' import tempfile'
18 import shutil
18 import shutil
19 import random
19 import random
20 import time
20 import time
21 from io import StringIO
21
22
22 import nose.tools as nt
23 import nose.tools as nt
23 import IPython.testing.tools as tt
24 import IPython.testing.tools as tt
24
25
25 from IPython.extensions.autoreload import AutoreloadMagics
26 from IPython.extensions.autoreload import AutoreloadMagics
26 from IPython.core.events import EventManager, pre_run_cell
27 from IPython.core.events import EventManager, pre_run_cell
27 from IPython.utils.py3compat import PY3
28
29 if PY3:
30 from io import StringIO
31 else:
32 from StringIO import StringIO
33
28
34 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
35 # Test fixture
30 # Test fixture
@@ -15,7 +15,7 b' from IPython.utils.process import find_cmd, FindCmdError'
15 from traitlets.config import get_config
15 from traitlets.config import get_config
16 from traitlets.config.configurable import SingletonConfigurable
16 from traitlets.config.configurable import SingletonConfigurable
17 from traitlets import List, Bool, Unicode
17 from traitlets import List, Bool, Unicode
18 from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u, PY3
18 from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u
19
19
20
20
21 class LaTeXTool(SingletonConfigurable):
21 class LaTeXTool(SingletonConfigurable):
@@ -84,7 +84,7 b' import re'
84 import datetime
84 import datetime
85 from collections import deque
85 from collections import deque
86
86
87 from IPython.utils.py3compat import PY3, PYPY, cast_unicode
87 from IPython.utils.py3compat import PYPY, cast_unicode
88 from IPython.utils.encoding import get_stream_enc
88 from IPython.utils.encoding import get_stream_enc
89
89
90 from io import StringIO
90 from io import StringIO
@@ -108,15 +108,7 b' def _safe_getattr(obj, attr, default=None):'
108 except Exception:
108 except Exception:
109 return default
109 return default
110
110
111 if PY3:
111 CUnicodeIO = StringIO
112 CUnicodeIO = StringIO
113 else:
114 class CUnicodeIO(StringIO):
115 """StringIO that casts str to unicode on Python 2"""
116 def write(self, text):
117 return super(CUnicodeIO, self).write(
118 cast_unicode(text, encoding=get_stream_enc(sys.stdout)))
119
120
112
121 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
113 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
122 """
114 """
@@ -133,7 +133,7 b' import tempfile'
133 import ast
133 import ast
134 import warnings
134 import warnings
135 import shutil
135 import shutil
136
136 from io import StringIO
137
137
138 # Third-party
138 # Third-party
139 from docutils.parsers.rst import directives
139 from docutils.parsers.rst import directives
@@ -144,12 +144,6 b' from traitlets.config import Config'
144 from IPython import InteractiveShell
144 from IPython import InteractiveShell
145 from IPython.core.profiledir import ProfileDir
145 from IPython.core.profiledir import ProfileDir
146 from IPython.utils import io
146 from IPython.utils import io
147 from IPython.utils.py3compat import PY3
148
149 if PY3:
150 from io import StringIO
151 else:
152 from StringIO import StringIO
153
147
154 #-----------------------------------------------------------------------------
148 #-----------------------------------------------------------------------------
155 # Globals
149 # Globals
@@ -7,7 +7,7 b' from warnings import warn'
7
7
8 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
8 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
9 from IPython.utils import io
9 from IPython.utils import io
10 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
10 from IPython.utils.py3compat import cast_unicode_py2, input
11 from IPython.utils.terminal import toggle_set_term_title, set_term_title
11 from IPython.utils.terminal import toggle_set_term_title, set_term_title
12 from IPython.utils.process import abbrev_cwd
12 from IPython.utils.process import abbrev_cwd
13 from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union
13 from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union
@@ -59,10 +59,7 b' _style_overrides_linux = {'
59
59
60 def get_default_editor():
60 def get_default_editor():
61 try:
61 try:
62 ed = os.environ['EDITOR']
62 return os.environ['EDITOR']
63 if not PY3:
64 ed = ed.decode()
65 return ed
66 except KeyError:
63 except KeyError:
67 pass
64 pass
68 except UnicodeError:
65 except UnicodeError:
@@ -345,26 +342,7 b' class TerminalInteractiveShell(InteractiveShell):'
345 return
342 return
346
343
347 import win_unicode_console
344 import win_unicode_console
348
345 win_unicode_console.enable()
349 if PY3:
350 win_unicode_console.enable()
351 else:
352 # https://github.com/ipython/ipython/issues/9768
353 from win_unicode_console.streams import (TextStreamWrapper,
354 stdout_text_transcoded, stderr_text_transcoded)
355
356 class LenientStrStreamWrapper(TextStreamWrapper):
357 def write(self, s):
358 if isinstance(s, bytes):
359 s = s.decode(self.encoding, 'replace')
360
361 self.base.write(s)
362
363 stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded)
364 stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded)
365
366 win_unicode_console.enable(stdout=stdout_text_str,
367 stderr=stderr_text_str)
368
346
369 def init_io(self):
347 def init_io(self):
370 if sys.platform not in {'win32', 'cli'}:
348 if sys.platform not in {'win32', 'cli'}:
@@ -40,13 +40,8 b' will fix it for Tk.)'
40 """
40 """
41 import time
41 import time
42
42
43 from IPython.utils.py3compat import PY3
44
45 import _tkinter
43 import _tkinter
46 if PY3:
44 import tkinter
47 import tkinter
48 else:
49 import Tkinter as tkinter # Python 2
50
45
51 def inputhook(inputhook_context):
46 def inputhook(inputhook_context):
52 """
47 """
@@ -49,7 +49,7 b' from .ipunittest import ipdoctest, ipdocstring'
49 from IPython.external.decorators import *
49 from IPython.external.decorators import *
50
50
51 # For onlyif_cmd_exists decorator
51 # For onlyif_cmd_exists decorator
52 from IPython.utils.py3compat import which, PY2, PY3, PYPY
52 from IPython.utils.py3compat import which
53
53
54 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
55 # Classes and functions
55 # Classes and functions
@@ -26,6 +26,7 b' import os'
26 import re
26 import re
27 import sys
27 import sys
28 from importlib import import_module
28 from importlib import import_module
29 from io import StringIO
29
30
30 from testpath import modified_env
31 from testpath import modified_env
31
32
@@ -45,12 +46,7 b' from nose.plugins import doctests, Plugin'
45 from nose.util import anyp, tolist
46 from nose.util import anyp, tolist
46
47
47 # Our own imports
48 # Our own imports
48 from IPython.utils.py3compat import builtin_mod, PY3
49 from IPython.utils.py3compat import builtin_mod
49
50 if PY3:
51 from io import StringIO
52 else:
53 from StringIO import StringIO
54
50
55 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
56 # Module globals and other constants
52 # Module globals and other constants
@@ -322,15 +322,7 b' def check_pairs(func, pairs):'
322 assert out == expected, pair_fail_msg.format(name, inp, expected, out)
322 assert out == expected, pair_fail_msg.format(name, inp, expected, out)
323
323
324
324
325 if py3compat.PY3:
325 MyStringIO = StringIO
326 MyStringIO = StringIO
327 else:
328 # In Python 2, stdout/stderr can have either bytes or unicode written to them,
329 # so we need a class that can handle both.
330 class MyStringIO(StringIO):
331 def write(self, s):
332 s = py3compat.cast_unicode(s, encoding=DEFAULT_ENCODING)
333 super(MyStringIO, self).write(s)
334
326
335 _re_type = type(re.compile(r''))
327 _re_type = type(re.compile(r''))
336
328
@@ -13,6 +13,7 b' import warnings'
13 from contextlib import contextmanager
13 from contextlib import contextmanager
14 from unittest.mock import patch
14 from unittest.mock import patch
15 from os.path import join, abspath, split
15 from os.path import join, abspath, split
16 from imp import reload
16
17
17 from nose import SkipTest, with_setup
18 from nose import SkipTest, with_setup
18 import nose.tools as nt
19 import nose.tools as nt
@@ -29,27 +30,19 b' from IPython.utils.tempdir import TemporaryDirectory'
29
30
30 # Platform-dependent imports
31 # Platform-dependent imports
31 try:
32 try:
32 import winreg as wreg # Py 3
33 import winreg as wreg
33 except ImportError:
34 except ImportError:
35 #Fake _winreg module on non-windows platforms
36 import types
37 wr_name = "winreg"
38 sys.modules[wr_name] = types.ModuleType(wr_name)
34 try:
39 try:
35 import _winreg as wreg # Py 2
40 import winreg as wreg
36 except ImportError:
41 except ImportError:
37 #Fake _winreg module on none windows platforms
42 import _winreg as wreg
38 import types
39 wr_name = "winreg" if py3compat.PY3 else "_winreg"
40 sys.modules[wr_name] = types.ModuleType(wr_name)
41 try:
42 import winreg as wreg
43 except ImportError:
44 import _winreg as wreg
45 #Add entries that needs to be stubbed by the testing code
43 #Add entries that needs to be stubbed by the testing code
46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
44 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
47
45
48 try:
49 reload
50 except NameError: # Python 3
51 from imp import reload
52
53 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
54 # Globals
47 # Globals
55 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now