1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
diff --git a/src/sage/doctest/forker.py b/src/sage/doctest/forker.py
index 2654016..d3daed6 100644
--- a/src/sage/doctest/forker.py
+++ b/src/sage/doctest/forker.py
@@ -116,10 +116,6 @@ def init_sage():
from sage.structure.debug_options import debug
debug.refine_category_hash_check = True
- # Disable IPython colors during doctests
- from sage.repl.interpreter import DEFAULT_SAGE_CONFIG
- DEFAULT_SAGE_CONFIG.TerminalInteractiveShell.colors = 'NoColor'
-
# We import readline before forking, otherwise Pdb doesn't work
# os OS X: http://trac.sagemath.org/14289
import readline
@@ -1088,15 +1084,13 @@ class SageDocTestRunner(doctest.DocTestRunner):
sage: _ = sage0.eval("import doctest, sys, os, multiprocessing, subprocess")
sage: _ = sage0.eval("from sage.doctest.parsing import SageOutputChecker")
sage: _ = sage0.eval("import sage.doctest.forker as sdf")
- sage: _ = sage0.eval("sdf.init_sage()")
sage: _ = sage0.eval("from sage.doctest.control import DocTestDefaults")
sage: _ = sage0.eval("DD = DocTestDefaults(debug=True)")
sage: _ = sage0.eval("ex1 = doctest.Example('a = 17', '')")
sage: _ = sage0.eval("ex2 = doctest.Example('2*a', '1')")
sage: _ = sage0.eval("DT = doctest.DocTest([ex1,ex2], globals(), 'doubling', None, 0, None)")
sage: _ = sage0.eval("DTR = sdf.SageDocTestRunner(SageOutputChecker(), verbose=False, sage_options=DD, optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS)")
- sage: sage0._prompt = r"debug: "
- sage: print(sage0.eval("DTR.run(DT, clear_globs=False)")) # indirect doctest
+ sage: print(sage0.eval("sdf.init_sage(); DTR.run(DT, clear_globs=False)")) # indirect doctest
**********************************************************************
Line 1, in doubling
Failed example:
@@ -1110,7 +1104,6 @@ class SageDocTestRunner(doctest.DocTestRunner):
...
sage: sage0.eval("a")
'...17'
- sage: sage0._prompt = "sage: "
sage: sage0.eval("quit")
'Returning to doctests...TestResults(failed=1, attempted=2)'
"""
@@ -1144,13 +1137,14 @@ class SageDocTestRunner(doctest.DocTestRunner):
print(src)
if ex.want:
print(doctest._indent(ex.want[:-1]))
- from sage.repl.interpreter import DEFAULT_SAGE_CONFIG
+ from sage.repl.configuration import sage_ipython_config
+ from sage.repl.prompts import DebugPrompts
from IPython.terminal.embed import InteractiveShellEmbed
- import copy
- cfg = copy.deepcopy(DEFAULT_SAGE_CONFIG)
- prompt_config = cfg.PromptManager
- prompt_config.in_template = 'debug: '
- prompt_config.in2_template = '.....: '
+ cfg = sage_ipython_config.default()
+ # Currently this doesn't work: prompts only work in pty
+ # We keep simple_prompt=True, prompts will be "In [0]:"
+ # cfg.InteractiveShell.prompts_class = DebugPrompts
+ # cfg.InteractiveShell.simple_prompt = False
shell = InteractiveShellEmbed(config=cfg, banner1='', user_ns=dict(globs))
shell(header='', stack_depth=2)
except KeyboardInterrupt:
@@ -1248,6 +1242,7 @@ class SageDocTestRunner(doctest.DocTestRunner):
sage: _ = sage0.eval("ex = doctest.Example('E = EllipticCurve([0,0]); E', 'A singular Elliptic Curve')")
sage: _ = sage0.eval("DT = doctest.DocTest([ex], globals(), 'singular_curve', None, 0, None)")
sage: _ = sage0.eval("DTR = sdf.SageDocTestRunner(SageOutputChecker(), verbose=False, sage_options=DD, optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS)")
+ sage: old_prompt = sage0._prompt
sage: sage0._prompt = r"\(Pdb\) "
sage: sage0.eval("DTR.run(DT, clear_globs=False)") # indirect doctest
'... ArithmeticError("invariants " + str(ainvs) + " define a singular curve")'
@@ -1257,7 +1252,7 @@ class SageDocTestRunner(doctest.DocTestRunner):
'...EllipticCurve_field.__init__(self, K, ainvs)'
sage: sage0.eval("p ainvs")
'(0, 0, 0, 0, 0)'
- sage: sage0._prompt = "sage: "
+ sage: sage0._prompt = old_prompt
sage: sage0.eval("quit")
'TestResults(failed=1, attempted=1)'
"""
diff --git a/src/sage/doctest/test.py b/src/sage/doctest/test.py
index d69136f..a6cc4d0 100644
--- a/src/sage/doctest/test.py
+++ b/src/sage/doctest/test.py
@@ -360,7 +360,7 @@ Test the ``--debug`` option::
s...: b = 5
s...: a + b
8
- debug:
+ In [1]:
<BLANKLINE>
Returning to doctests...
**********************************************************************
diff --git a/src/sage/interfaces/sage0.py b/src/sage/interfaces/sage0.py
index 87bfc0b..77cd151 100644
--- a/src/sage/interfaces/sage0.py
+++ b/src/sage/interfaces/sage0.py
@@ -18,6 +18,7 @@ from __future__ import absolute_import
import cPickle
import os
+import re
from .expect import Expect, ExpectElement, FunctionElement
import sage.repl.preparse
@@ -146,10 +147,17 @@ class Sage(ExtraTabCompletion, Expect):
if init_code is None:
init_code = ['from sage.all import *', 'import cPickle']
else:
- # Disable the IPython history (implemented as SQLite database)
- # to avoid problems with locking.
- command = "sage-ipython --HistoryManager.hist_file=:memory: --colors=NoColor"
- prompt = "sage: "
+ command = ' '.join([
+ 'sage-ipython',
+ # Disable the IPython history (implemented as SQLite database)
+ # to avoid problems with locking.
+ '--HistoryManager.hist_file=:memory:',
+ # Disable everything that prints ANSI codes
+ '--colors=NoColor',
+ '--no-term-title',
+ '--simple-prompt',
+ ])
+ prompt = re.compile('In \[\d+\]: ')
if init_code is None:
init_code = ['import cPickle']
diff --git a/src/sage/misc/trace.py b/src/sage/misc/trace.py
index 0da17e7..efcaa33 100644
--- a/src/sage/misc/trace.py
+++ b/src/sage/misc/trace.py
@@ -54,6 +54,7 @@ def trace(code, preparse=True):
sage: import pexpect
sage: s = pexpect.spawn('sage')
sage: _ = s.sendline("trace('print(factor(10))'); print(3+97)")
+ sage: _ = s.expect('ipdb>', timeout=90)
sage: _ = s.sendline("s"); _ = s.sendline("c");
sage: _ = s.expect('100', timeout=90)
diff --git a/src/sage/repl/configuration.py b/src/sage/repl/configuration.py
new file mode 100644
index 0000000..5034039
--- /dev/null
+++ b/src/sage/repl/configuration.py
@@ -0,0 +1,152 @@
+r"""
+Sage's IPython Configuration
+"""
+
+#*****************************************************************************
+# Copyright (C) 2016 Volker Braun <vbraun.name@gmail.com>
+#
+# Distributed under the terms of the GNU General Public License (GPL)
+# as published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+# http://www.gnu.org/licenses/
+#*****************************************************************************
+
+from __future__ import absolute_import
+
+import sys
+import copy
+from traitlets.config.loader import Config
+
+from sage.repl.prompts import SagePrompts
+
+
+# Name of the Sage IPython extension
+SAGE_EXTENSION = 'sage'
+
+
+class SageIpythonConfiguration(object):
+
+ def _doctest_mode(self):
+ """
+ Whether we are in doctest mode
+
+ This returns ``True`` during doctests.
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config._doctest_mode()
+ True
+ """
+ from sage.doctest import DOCTEST_MODE
+ return DOCTEST_MODE
+
+ def _allow_ansi(self):
+ """
+ Whether to allow ANSI escape sequences
+
+ This returns ``False`` during doctests to avoid ANSI escape
+ sequences.
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config._allow_ansi()
+ False
+ """
+ return (not self._doctest_mode()) and sys.stdout.isatty()
+
+ def colors(self):
+ """
+ Return the IPython color palette
+
+ This returns ``'NoColor'`` during doctests to avoid ANSI escape
+ sequences.
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config.simple_prompt()
+ True
+ """
+ return 'LightBG' if self._allow_ansi() else 'NoColor'
+
+ def simple_prompt(self):
+ """
+ Return whether to use the simple prompt
+
+ This returns ``True`` during doctests to avoid ANSI escape sequences.
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config.simple_prompt()
+ True
+ """
+ return not self._allow_ansi()
+
+ def term_title(self):
+ """
+ Return whether to set the terminal title
+
+ This returns false during doctests to avoid ANSI escape sequences.
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config.term_title()
+ False
+ """
+ return self._allow_ansi()
+
+ def default(self):
+ """
+ Return a new default configuration object
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config.default()
+ {'InteractiveShell': {'colors': ...
+ """
+ from sage.repl.interpreter import SageTerminalInteractiveShell
+ cfg = Config(
+ TerminalIPythonApp=Config(
+ display_banner=False,
+ verbose_crash=True,
+ test_shell=False,
+ shell_class=SageTerminalInteractiveShell,
+ ),
+ InteractiveShell=Config(
+ prompts_class=SagePrompts,
+ ast_node_interactivity='all',
+ colors=self.colors(),
+ simple_prompt=self.simple_prompt(),
+ term_title=self.term_title(),
+ confirm_exit=False,
+ separate_in=''
+ ),
+ InteractiveShellApp=Config(extensions=[SAGE_EXTENSION]),
+ )
+ if self._doctest_mode():
+ # Using the file-backed history causes problems in parallel tests
+ cfg.HistoryManager = Config(hist_file=':memory:')
+ return cfg
+
+ def copy(self):
+ """
+ Return a copy of the current configuration
+
+ EXAMPLES::
+
+ sage: from sage.repl.configuration import sage_ipython_config
+ sage: sage_ipython_config.copy()
+ {'InteractiveShell': {'colors': ...
+ """
+ try:
+ return copy.deepcopy(get_ipython().config)
+ except NameError:
+ return self.default()
+
+
+sage_ipython_config = SageIpythonConfiguration()
diff --git a/src/sage/repl/interpreter.py b/src/sage/repl/interpreter.py
index e0499c7..1f4eda7 100644
--- a/src/sage/repl/interpreter.py
+++ b/src/sage/repl/interpreter.py
@@ -102,18 +102,15 @@ Check that Cython source code appears in tracebacks::
#*****************************************************************************
-import copy
import os
import re
-import sys
from sage.repl.preparse import preparse
+from sage.repl.prompts import SagePrompts, InterfacePrompts
-from traitlets.config.loader import Config
from traitlets import Bool, Type
from sage.env import SAGE_LOCAL
-
-SAGE_EXTENSION = 'sage'
+from sage.repl.configuration import sage_ipython_config, SAGE_EXTENSION
def embedded():
"""
@@ -370,31 +367,7 @@ class SageTestShell(SageShellOverride, TerminalInteractiveShell):
rc = super(SageTestShell, self).run_cell(*args, **kwds)
-###################################################################
-# Default configuration
-###################################################################
-
-DEFAULT_SAGE_CONFIG = Config(
- PromptManager = Config(
- in_template = 'sage: ',
- in2_template = '....: ',
- justify = False,
- out_template = ''),
- TerminalIPythonApp = Config(
- display_banner = False,
- verbose_crash = True,
- test_shell = False,
- shell_class = SageTerminalInteractiveShell,
- ),
- InteractiveShell = Config(
- ast_node_interactivity = 'all',
- colors = 'LightBG' if sys.stdout.isatty() else 'NoColor',
- confirm_exit = False,
- separate_in = ''),
- InteractiveShellApp = Config(extensions=[SAGE_EXTENSION]),
-)
-
-
+
###################################################################
# Transformers used in the SageInputSplitter
###################################################################
@@ -614,19 +587,17 @@ 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 ]
- <IPython.core.interactiveshell.ExecutionResult object at 0x...>
- """
- try:
- cfg = copy.deepcopy(get_ipython().config)
- except NameError:
- cfg = copy.deepcopy(DEFAULT_SAGE_CONFIG)
- cfg.PromptManager['in_template'] = interface.name() + ': '
- cfg.PromptManager['in2_template'] = len(interface.name())*'.' + ': '
+ <repr(<IPython.core.interactiveshell.ExecutionResult at 0x...>) failed:
+ AttributeError: type object 'ExecutionResult' has no attribute '__qualname__'>
+ Note that the repr error is https://github.com/ipython/ipython/issues/9756
+ """
+ cfg = sage_ipython_config.copy()
ipshell = InteractiveShellEmbed(config=cfg,
banner1='\n --> Switching to %s <--\n\n'%interface,
- exit_msg = '\n --> Exiting back to Sage <--\n')
+ exit_msg='\n --> Exiting back to Sage <--\n')
ipshell.interface = interface
+ ipshell.prompts = InterfacePrompts(interface.name())
while ipshell.prefilter_manager.transformers:
ipshell.prefilter_manager.transformers.pop()
@@ -669,7 +640,7 @@ def get_test_shell():
sage: out + err
''
"""
- config = copy.deepcopy(DEFAULT_SAGE_CONFIG)
+ config = sage_ipython_config.default()
config.TerminalIPythonApp.test_shell = True
config.TerminalIPythonApp.shell_class = SageTestShell
app = SageTerminalApp.instance(config=config)
@@ -748,12 +719,9 @@ class SageTerminalApp(TerminalIPythonApp):
sage: os.environ['IPYTHONDIR'] = IPYTHONDIR
"""
super(SageTerminalApp, self).load_config_file(*args, **kwds)
-
- newconfig = copy.deepcopy(DEFAULT_SAGE_CONFIG)
-
+ newconfig = sage_ipython_config.default()
# merge in the config loaded from file
newconfig.merge(self.config)
-
self.config = newconfig
def init_shell(self):
@@ -767,7 +735,7 @@ class SageTerminalApp(TerminalIPythonApp):
EXAMPLES::
- sage: from sage.repl.interpreter import SageTerminalApp, DEFAULT_SAGE_CONFIG
+ sage: from sage.repl.interpreter import SageTerminalApp
sage: app = SageTerminalApp.instance()
sage: app.shell
<sage.repl.interpreter.SageTestShell object at 0x...>
@@ -776,7 +744,6 @@ class SageTerminalApp(TerminalIPythonApp):
self.shell = self.shell_class.instance(
parent=self,
config=self.config,
- display_banner=False,
profile_dir=self.profile_dir,
ipython_dir=self.ipython_dir)
self.shell.configurables.append(self)
diff --git a/src/sage/repl/ipython_tests.py b/src/sage/repl/ipython_tests.py
index 0fa568d..2bb34cd 100644
--- a/src/sage/repl/ipython_tests.py
+++ b/src/sage/repl/ipython_tests.py
@@ -100,6 +100,12 @@ Next, test the pinfo2 magic for Cython code::
...
File: .../sage/tests/stl_vector.pyx
Type: type
+
+Test that there are no warnings being ignored internally::
+
+ sage: import warnings
+ sage: warnings.simplefilter('error'); get_test_shell()
+ <sage.repl.interpreter.SageTestShell object at 0x...>
'''
diff --git a/src/sage/repl/prompts.py b/src/sage/repl/prompts.py
new file mode 100644
index 0000000..e885730
--- /dev/null
+++ b/src/sage/repl/prompts.py
@@ -0,0 +1,92 @@
+r"""
+Sage Commandline Prompts
+"""
+
+#*****************************************************************************
+# Copyright (C) 2016 Volker Braun <vbraun.name@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+# http://www.gnu.org/licenses/
+#*****************************************************************************
+
+from pygments.token import Token
+from IPython.terminal.prompts import Prompts
+
+
+class SagePrompts(Prompts):
+
+ def in_prompt_tokens(self, cli=None):
+ return [
+ (Token.Prompt, 'sage: '),
+ ]
+
+ def continuation_prompt_tokens(self, cli=None, width=None):
+ return [
+ (Token.Prompt, '....: '),
+ ]
+
+ def rewrite_prompt_tokens(self):
+ return [
+ (Token.Prompt, '----> '),
+ ]
+
+ def out_prompt_tokens(self):
+ return [
+ (Token.OutPrompt, ''),
+ ]
+
+
+class InterfacePrompts(Prompts):
+
+ def __init__(self, interface_name):
+ self.__name = interface_name
+ self.__width = len(interface_name)
+
+ def in_prompt_tokens(self, cli=None):
+ return [
+ (Token.Prompt, self.__name + ': '),
+ ]
+
+ def continuation_prompt_tokens(self, cli=None, width=None):
+ return [
+ (Token.Prompt, '.' * self.__width + ': '),
+ ]
+
+ def rewrite_prompt_tokens(self):
+ return [
+ (Token.Prompt, '-' * self.__width + '> '),
+ ]
+
+ def out_prompt_tokens(self):
+ return [
+ (Token.OutPrompt, ''),
+ ]
+
+
+class DebugPrompts(Prompts):
+
+ def in_prompt_tokens(self, cli=None):
+ return [
+ (Token.Prompt, 'debug: '),
+ ]
+
+ def continuation_prompt_tokens(self, cli=None, width=None):
+ return [
+ (Token.Prompt, '.....: '),
+ ]
+
+ def rewrite_prompt_tokens(self):
+ return [
+ (Token.Prompt, '-----> '),
+ ]
+
+ def out_prompt_tokens(self):
+ return [
+ (Token.OutPrompt, ''),
+ ]
+
+
+
diff --git a/src/sage/tests/cmdline.py b/src/sage/tests/cmdline.py
index 0d15d82..417ec5e 100644
--- a/src/sage/tests/cmdline.py
+++ b/src/sage/tests/cmdline.py
@@ -385,7 +385,7 @@ def test_executable(args, input="", timeout=100.0, **kwds):
**********************************************************************
Previously executed commands:
s...: assert True == False
- debug:
+ In [1]:
<BLANKLINE>
Returning to doctests...
**********************************************************************
|