diff --git a/src/sage/combinat/combinatorial_map.py b/src/sage/combinat/combinatorial_map.py index 03075615bd..0be1240a94 100644 --- a/src/sage/combinat/combinatorial_map.py +++ b/src/sage/combinat/combinatorial_map.py @@ -257,8 +257,8 @@ class CombinatorialMap(object): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: p = Permutation([1,3,2,4]) sage: p.left_tableau.__repr__() 'Combinatorial map: Robinson-Schensted insertion tableau' @@ -272,8 +272,8 @@ class CombinatorialMap(object): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: p = Permutation([1,3,2,4]) sage: cm = p.left_tableau; cm Combinatorial map: Robinson-Schensted insertion tableau @@ -293,8 +293,8 @@ class CombinatorialMap(object): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: p = Permutation([1,3,2,4]) sage: p.left_tableau #indirect doctest Combinatorial map: Robinson-Schensted insertion tableau @@ -309,8 +309,8 @@ class CombinatorialMap(object): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: p = Permutation([1,3,2,4]) sage: cm = type(p).left_tableau; cm Combinatorial map: Robinson-Schensted insertion tableau @@ -335,8 +335,8 @@ class CombinatorialMap(object): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: from sage.combinat.permutation import Permutation sage: pi = Permutation([1,3,2]) sage: f = pi.reverse @@ -400,8 +400,8 @@ def combinatorial_maps_in_class(cls): EXAMPLES:: sage: sage.combinat.combinatorial_map.combinatorial_map = sage.combinat.combinatorial_map.combinatorial_map_wrapper - sage: import imp - sage: _ = imp.reload(sage.combinat.permutation) + sage: import importlib + sage: _ = importlib.reload(sage.combinat.permutation) sage: from sage.combinat.combinatorial_map import combinatorial_maps_in_class sage: p = Permutation([1,3,2,4]) sage: cmaps = combinatorial_maps_in_class(p) diff --git a/src/sage/parallel/use_fork.py b/src/sage/parallel/use_fork.py index c3b5e59fe0..7c4207901b 100644 --- a/src/sage/parallel/use_fork.py +++ b/src/sage/parallel/use_fork.py @@ -279,7 +279,7 @@ class p_iter_fork(object): sage: F._subprocess(operator.add, tmp_dir(), (1, 2)) sage: sys.stdout = saved_stdout """ - import imp, os, sys + import importlib, os, sys from sage.misc.persist import save # Make it so all stdout is sent to a file so it can @@ -291,7 +291,7 @@ class p_iter_fork(object): # pid has changed (forcing a reload of # misc). import sage.misc.misc - imp.reload(sage.misc.misc) + importlib.reload(sage.misc.misc) # The pexpect interfaces (and objects defined in them) are # not valid. diff --git a/src/sage/all.py b/src/sage/all.py index c87c9372e9..72db7b85bb 100644 --- a/src/sage/all.py +++ b/src/sage/all.py @@ -19,7 +19,7 @@ We exclude the dependencies and check to see that there are no others except for the known bad apples:: sage: allowed = [ - ....: 'IPython', 'prompt_toolkit', # sage dependencies + ....: 'IPython', 'prompt_toolkit', 'jedi', # sage dependencies ....: 'threading', 'multiprocessing', # doctest dependencies ....: '__main__', 'sage.doctest', # doctesting ....: 'signal', 'enum', # may appear in Python 3 diff --git a/src/sage/repl/attach.py b/src/sage/repl/attach.py index c350ec33af..4605951df2 100644 --- a/src/sage/repl/attach.py +++ b/src/sage/repl/attach.py @@ -249,13 +249,13 @@ def reset_load_attach_path(): sage: reset_load_attach_path(); load_attach_path() ['.'] sage: os.environ['SAGE_LOAD_ATTACH_PATH'] = '/veni/vidi:vici:' - sage: import imp - sage: imp.reload(sage.repl.attach) # Simulate startup + sage: import importlib + sage: importlib.reload(sage.repl.attach) # Simulate startup sage: load_attach_path() ['.', '/veni/vidi', 'vici'] sage: del os.environ['SAGE_LOAD_ATTACH_PATH'] - sage: imp.reload(sage.repl.preparse) # Simulate startup + sage: importlib.reload(sage.repl.preparse) # Simulate startup sage: reset_load_attach_path(); load_attach_path() ['.'] @@ -595,12 +595,7 @@ def reload_attached_files_if_modified(): basename = os.path.basename(filename) timestr = time.strftime('%T', mtime) notice = '### reloading attached file {0} modified at {1} ###'.format(basename, timestr) - if ip and ip.pt_cli: - with ip.pt_cli.patch_stdout_context(raw=True): - print(notice) - code = load_wrap(filename, attach=True) - ip.run_cell(code) - elif ip: + if ip: print(notice) code = load_wrap(filename, attach=True) ip.run_cell(code) diff --git a/src/sage/repl/display/fancy_repr.py b/src/sage/repl/display/fancy_repr.py index 818498fbae..af33f92126 100644 --- a/src/sage/repl/display/fancy_repr.py +++ b/src/sage/repl/display/fancy_repr.py @@ -15,10 +15,12 @@ Representations of objects. import types from IPython.lib.pretty import ( - _safe_getattr, _baseclass_reprs, + _safe_getattr, _type_pprinters, ) +_baseclass_reprs = (object.__repr__,) + from sage.repl.display.util import format_list diff --git a/src/sage/repl/interpreter.py b/src/sage/repl/interpreter.py index 5c56e6bf89..a96c350791 100644 --- a/src/sage/repl/interpreter.py +++ b/src/sage/repl/interpreter.py @@ -78,7 +78,7 @@ Check that Cython source code appears in tracebacks:: dummy line ... ZeroDivisionError...Traceback (most recent call last) - in () + in ... ----> 1 Integer(1)/Integer(0) .../sage/rings/integer.pyx in sage.rings.integer.Integer...div... (.../cythonized/sage/rings/integer.c:...)() ... @@ -364,22 +364,19 @@ class SageTestShell(SageShellOverride, TerminalInteractiveShell): ################################################################### # Transformers used in the SageInputSplitter ################################################################### -from IPython.core.inputtransformer import (CoroutineInputTransformer, - StatelessInputTransformer, - _strip_prompts) +from IPython.core.inputtransformer2 import PromptStripper -@StatelessInputTransformer.wrap -def SagePreparseTransformer(line): +def SagePreparseTransformer(lines): r""" EXAMPLES:: sage: from sage.repl.interpreter import SagePreparseTransformer - sage: spt = SagePreparseTransformer() - sage: spt.push('1+1r+2.3^2.3r') - "Integer(1)+1+RealNumber('2.3')**2.3" + sage: spt = SagePreparseTransformer + sage: spt(['1+1r+2.3^2.3r']) + ["Integer(1)+1+RealNumber('2.3')**2.3"] sage: preparser(False) - sage: spt.push('2.3^2') - '2.3^2' + sage: spt(['2.3^2']) + ['2.3^2'] TESTS: @@ -400,57 +397,15 @@ def SagePreparseTransformer(line): sage: shell.quit() """ - if _do_preparse and not line.startswith('%'): - return preparse(line) - else: - return line - -@CoroutineInputTransformer.wrap -def SagePromptTransformer(): - r""" - Strip the sage:/....: prompts of Sage. - - EXAMPLES:: - - sage: from sage.repl.interpreter import SagePromptTransformer - sage: spt = SagePromptTransformer() - sage: spt.push("sage: 2 + 2") - '2 + 2' - sage: spt.push('') - '' - sage: spt.push("....: 2+2") - '2+2' - - This should strip multiple prompts: see :trac:`16297`:: - - sage: spt.push("sage: sage: 2+2") - '2+2' - sage: spt.push(" sage: ....: 2+2") - '2+2' - - The prompt contains a trailing space. Extra spaces between the - last prompt and the remainder should not be stripped:: - - sage: spt.push(" sage: ....: 2+2") - ' 2+2' - - We test that the input transformer is enabled on the Sage command - line:: - - sage: from sage.repl.interpreter import get_test_shell - sage: shell = get_test_shell() - sage: shell.run_cell('sage: a = 123') # single line - sage: shell.run_cell('sage: a = [\n... 123]') # old-style multi-line - sage: shell.run_cell('sage: a = [\n....: 123]') # new-style multi-line - - We test that :trac:`16196` is resolved:: + lines_out = [] + for line in lines: + if _do_preparse and not line.startswith('%'): + lines_out += [preparse(line)] + else: + lines_out += [line] + return lines_out - sage: shell.run_cell(' sage: 1+1') - 2 - sage: shell.quit() - """ - _sage_prompt_re = re.compile(r'^(\s*(:?sage: |\.\.\.\.: ))+') - return _strip_prompts(_sage_prompt_re) +SagePromptTransformer = PromptStripper(prompt_re=re.compile(r'^(\s*(:?sage: |\.\.\.\.: ))+')) ################### # Interface shell # @@ -612,7 +567,7 @@ def interface_shell_embed(interface): sage: shell = interface_shell_embed(gap) sage: shell.run_cell('List( [1..10], IsPrime )') [ false, true, true, false, true, false, true, false, false, false ] - + """ cfg = sage_ipython_config.copy() ipshell = InteractiveShellEmbed(config=cfg, diff --git a/src/sage/repl/ipython_extension.py b/src/sage/repl/ipython_extension.py index 21d21afd3d..0c58dd1d26 100644 --- a/src/sage/repl/ipython_extension.py +++ b/src/sage/repl/ipython_extension.py @@ -509,10 +509,8 @@ class SageCustomizations(object): from .interpreter import (SagePreparseTransformer, SagePromptTransformer) - for s in (self.shell.input_splitter, self.shell.input_transformer_manager): - s.physical_line_transforms.insert(1, SagePromptTransformer()) - s.python_line_transforms.append(SagePreparseTransformer()) - + self.shell.input_transformers_cleanup.insert(1, SagePromptTransformer) + self.shell.input_transformers_post.append(SagePreparseTransformer) class SageJupyterCustomizations(SageCustomizations): @staticmethod diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 0dc5e1f4cc..afb3396755 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -7732,13 +7732,13 @@ class ANBinaryExpr(ANDescr): decrease it before we return:: sage: import sys; sys.getrecursionlimit() - 1000 + 3000 sage: s = SymmetricFunctions(QQ).schur() sage: a=s([3,2]).expand(8)(flatten([[QQbar.zeta(3)^d for d in range(3)], [QQbar.zeta(5)^d for d in range(5)]])) sage: a.exactify(); a # long time 0 sage: sys.getrecursionlimit() - 1000 + 3000 """ import sys diff --git a/src/sage/repl/interface_magic.py b/src/sage/repl/interface_magic.py index a2b9988315..15da4c0bab 100644 --- a/src/sage/repl/interface_magic.py +++ b/src/sage/repl/interface_magic.py @@ -260,7 +260,7 @@ class InterfaceMagic(object): 2 120 sage: shell.run_cell('%%gap foo\n1+1;\n') - File "", line unknown + ...File "", line unknown SyntaxError: Interface magics have no options, got "foo" sage: shell.run_cell('%%gap?') diff --git a/src/sage/repl/ipython_tests.py b/src/sage/repl/ipython_tests.py index d342ba7fac..ab9504337e 100644 --- a/src/sage/repl/ipython_tests.py +++ b/src/sage/repl/ipython_tests.py @@ -42,6 +42,7 @@ Next, test the pinfo magic for Cython code:: Init docstring: ...ee help(type(...)) for...signature... File: .../sage/tests/stl_vector.pyx Type: type + ... Next, test the ``pinfo`` magic for ``R`` interface code, see :trac:`26906`:: @@ -67,6 +68,7 @@ calls when you ask for the double-questionmark help, like `foo??` :: sage: shell.run_cell(u'from sage.repl.ipython_tests import dummy') sage: shell.run_cell(u'%pinfo2 dummy') Signature: dummy(argument, optional=None) + ... Source: def dummy(argument, optional=None): """ @@ -116,6 +118,7 @@ Next, test the pinfo2 magic for Cython code:: ... File: .../sage/tests/stl_vector.pyx Type: type + ... Next, test the ``pinfo2`` magic for ``R`` interface code, see :trac:`26906`:: diff --git a/src/sage/repl/display/formatter.py b/src/sage/repl/display/formatter.py index 5ffdb3d374..0d28873e2a 100644 --- a/src/sage/repl/display/formatter.py +++ b/src/sage/repl/display/formatter.py @@ -173,8 +173,8 @@ class SageDisplayFormatter(DisplayFormatter): sage: shell.run_cell('ipython_image') sage: shell.run_cell('get_ipython().display_formatter.format(ipython_image)') - ({u'image/png': ...'\x89PNG...', - u'text/plain': u''}, + ({'image/png': ...', + 'text/plain': ''}, {}) Test that IPython images still work even in latex output mode::