passagemath-repl 10.4.62__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. passagemath_repl-10.4.62.data/scripts/sage-cachegrind +25 -0
  2. passagemath_repl-10.4.62.data/scripts/sage-callgrind +16 -0
  3. passagemath_repl-10.4.62.data/scripts/sage-cleaner +230 -0
  4. passagemath_repl-10.4.62.data/scripts/sage-coverage +327 -0
  5. passagemath_repl-10.4.62.data/scripts/sage-eval +14 -0
  6. passagemath_repl-10.4.62.data/scripts/sage-fixdoctests +708 -0
  7. passagemath_repl-10.4.62.data/scripts/sage-inline-fortran +12 -0
  8. passagemath_repl-10.4.62.data/scripts/sage-ipynb2rst +50 -0
  9. passagemath_repl-10.4.62.data/scripts/sage-ipython +16 -0
  10. passagemath_repl-10.4.62.data/scripts/sage-massif +25 -0
  11. passagemath_repl-10.4.62.data/scripts/sage-notebook +267 -0
  12. passagemath_repl-10.4.62.data/scripts/sage-omega +25 -0
  13. passagemath_repl-10.4.62.data/scripts/sage-preparse +302 -0
  14. passagemath_repl-10.4.62.data/scripts/sage-run +27 -0
  15. passagemath_repl-10.4.62.data/scripts/sage-run-cython +10 -0
  16. passagemath_repl-10.4.62.data/scripts/sage-runtests +9 -0
  17. passagemath_repl-10.4.62.data/scripts/sage-startuptime.py +163 -0
  18. passagemath_repl-10.4.62.data/scripts/sage-valgrind +34 -0
  19. passagemath_repl-10.4.62.dist-info/METADATA +77 -0
  20. passagemath_repl-10.4.62.dist-info/RECORD +162 -0
  21. passagemath_repl-10.4.62.dist-info/WHEEL +5 -0
  22. passagemath_repl-10.4.62.dist-info/top_level.txt +1 -0
  23. sage/all__sagemath_repl.py +119 -0
  24. sage/doctest/__init__.py +4 -0
  25. sage/doctest/__main__.py +236 -0
  26. sage/doctest/all.py +4 -0
  27. sage/doctest/check_tolerance.py +261 -0
  28. sage/doctest/control.py +1727 -0
  29. sage/doctest/external.py +534 -0
  30. sage/doctest/fixtures.py +383 -0
  31. sage/doctest/forker.py +2665 -0
  32. sage/doctest/marked_output.py +102 -0
  33. sage/doctest/parsing.py +1708 -0
  34. sage/doctest/parsing_test.py +79 -0
  35. sage/doctest/reporting.py +733 -0
  36. sage/doctest/rif_tol.py +124 -0
  37. sage/doctest/sources.py +1657 -0
  38. sage/doctest/test.py +584 -0
  39. sage/doctest/tests/1second.rst +4 -0
  40. sage/doctest/tests/99seconds.rst +4 -0
  41. sage/doctest/tests/abort.rst +5 -0
  42. sage/doctest/tests/atexit.rst +7 -0
  43. sage/doctest/tests/fail_and_die.rst +6 -0
  44. sage/doctest/tests/initial.rst +15 -0
  45. sage/doctest/tests/interrupt.rst +7 -0
  46. sage/doctest/tests/interrupt_diehard.rst +14 -0
  47. sage/doctest/tests/keyboardinterrupt.rst +11 -0
  48. sage/doctest/tests/longtime.rst +5 -0
  49. sage/doctest/tests/nodoctest +5 -0
  50. sage/doctest/tests/random_seed.rst +4 -0
  51. sage/doctest/tests/show_skipped.rst +18 -0
  52. sage/doctest/tests/sig_on.rst +9 -0
  53. sage/doctest/tests/simple_failure.rst +8 -0
  54. sage/doctest/tests/sleep_and_raise.rst +106 -0
  55. sage/doctest/tests/tolerance.rst +31 -0
  56. sage/doctest/util.py +750 -0
  57. sage/interfaces/cleaner.py +48 -0
  58. sage/interfaces/quit.py +163 -0
  59. sage/misc/all__sagemath_repl.py +51 -0
  60. sage/misc/banner.py +235 -0
  61. sage/misc/benchmark.py +221 -0
  62. sage/misc/classgraph.py +131 -0
  63. sage/misc/copying.py +22 -0
  64. sage/misc/cython.py +694 -0
  65. sage/misc/dev_tools.py +745 -0
  66. sage/misc/edit_module.py +304 -0
  67. sage/misc/explain_pickle.py +3079 -0
  68. sage/misc/gperftools.py +361 -0
  69. sage/misc/inline_fortran.py +212 -0
  70. sage/misc/messaging.py +86 -0
  71. sage/misc/pager.py +21 -0
  72. sage/misc/profiler.py +179 -0
  73. sage/misc/python.py +70 -0
  74. sage/misc/remote_file.py +53 -0
  75. sage/misc/sage_eval.py +246 -0
  76. sage/misc/sage_input.py +3621 -0
  77. sage/misc/sagedoc.py +1742 -0
  78. sage/misc/sh.py +38 -0
  79. sage/misc/trace.py +90 -0
  80. sage/repl/__init__.py +16 -0
  81. sage/repl/all.py +15 -0
  82. sage/repl/attach.py +625 -0
  83. sage/repl/configuration.py +186 -0
  84. sage/repl/display/__init__.py +1 -0
  85. sage/repl/display/fancy_repr.py +354 -0
  86. sage/repl/display/formatter.py +318 -0
  87. sage/repl/display/jsmol_iframe.py +290 -0
  88. sage/repl/display/pretty_print.py +153 -0
  89. sage/repl/display/util.py +163 -0
  90. sage/repl/image.py +302 -0
  91. sage/repl/inputhook.py +91 -0
  92. sage/repl/interface_magic.py +298 -0
  93. sage/repl/interpreter.py +854 -0
  94. sage/repl/ipython_extension.py +593 -0
  95. sage/repl/ipython_kernel/__init__.py +1 -0
  96. sage/repl/ipython_kernel/__main__.py +4 -0
  97. sage/repl/ipython_kernel/all_jupyter.py +10 -0
  98. sage/repl/ipython_kernel/install.py +301 -0
  99. sage/repl/ipython_kernel/interact.py +278 -0
  100. sage/repl/ipython_kernel/kernel.py +217 -0
  101. sage/repl/ipython_kernel/widgets.py +466 -0
  102. sage/repl/ipython_kernel/widgets_sagenb.py +587 -0
  103. sage/repl/ipython_tests.py +163 -0
  104. sage/repl/load.py +326 -0
  105. sage/repl/preparse.py +2218 -0
  106. sage/repl/prompts.py +90 -0
  107. sage/repl/rich_output/__init__.py +4 -0
  108. sage/repl/rich_output/backend_base.py +648 -0
  109. sage/repl/rich_output/backend_doctest.py +316 -0
  110. sage/repl/rich_output/backend_emacs.py +151 -0
  111. sage/repl/rich_output/backend_ipython.py +596 -0
  112. sage/repl/rich_output/buffer.py +311 -0
  113. sage/repl/rich_output/display_manager.py +829 -0
  114. sage/repl/rich_output/example.avi +0 -0
  115. sage/repl/rich_output/example.canvas3d +1 -0
  116. sage/repl/rich_output/example.dvi +0 -0
  117. sage/repl/rich_output/example.flv +0 -0
  118. sage/repl/rich_output/example.gif +0 -0
  119. sage/repl/rich_output/example.jpg +0 -0
  120. sage/repl/rich_output/example.mkv +0 -0
  121. sage/repl/rich_output/example.mov +0 -0
  122. sage/repl/rich_output/example.mp4 +0 -0
  123. sage/repl/rich_output/example.ogv +0 -0
  124. sage/repl/rich_output/example.pdf +0 -0
  125. sage/repl/rich_output/example.png +0 -0
  126. sage/repl/rich_output/example.svg +54 -0
  127. sage/repl/rich_output/example.webm +0 -0
  128. sage/repl/rich_output/example.wmv +0 -0
  129. sage/repl/rich_output/example_jmol.spt.zip +0 -0
  130. sage/repl/rich_output/example_wavefront_scene.mtl +7 -0
  131. sage/repl/rich_output/example_wavefront_scene.obj +17 -0
  132. sage/repl/rich_output/output_basic.py +391 -0
  133. sage/repl/rich_output/output_browser.py +103 -0
  134. sage/repl/rich_output/output_catalog.py +54 -0
  135. sage/repl/rich_output/output_graphics.py +320 -0
  136. sage/repl/rich_output/output_graphics3d.py +345 -0
  137. sage/repl/rich_output/output_video.py +231 -0
  138. sage/repl/rich_output/preferences.py +432 -0
  139. sage/repl/rich_output/pretty_print.py +339 -0
  140. sage/repl/rich_output/test_backend.py +201 -0
  141. sage/repl/user_globals.py +214 -0
  142. sage/tests/__init__.py +1 -0
  143. sage/tests/all.py +3 -0
  144. sage/tests/article_heuberger_krenn_kropf_fsm-in-sage.py +630 -0
  145. sage/tests/arxiv_0812_2725.py +351 -0
  146. sage/tests/benchmark.py +1923 -0
  147. sage/tests/book_schilling_zabrocki_kschur_primer.py +795 -0
  148. sage/tests/book_stein_ent.py +651 -0
  149. sage/tests/book_stein_modform.py +558 -0
  150. sage/tests/cmdline.py +790 -0
  151. sage/tests/combinatorial_hopf_algebras.py +52 -0
  152. sage/tests/finite_poset.py +623 -0
  153. sage/tests/functools_partial_src.py +27 -0
  154. sage/tests/gosper-sum.py +218 -0
  155. sage/tests/lazy_imports.py +28 -0
  156. sage/tests/modular_group_cohomology.py +80 -0
  157. sage/tests/numpy.py +21 -0
  158. sage/tests/parigp.py +76 -0
  159. sage/tests/startup.py +27 -0
  160. sage/tests/symbolic-series.py +76 -0
  161. sage/tests/sympy.py +16 -0
  162. sage/tests/test_deprecation.py +31 -0
@@ -0,0 +1,593 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ r"""
3
+ Sage's IPython Extension
4
+
5
+ A Sage extension which adds sage-specific features:
6
+
7
+ * magics
8
+
9
+ - ``%crun``
10
+
11
+ - ``%runfile``
12
+
13
+ - ``%attach``
14
+
15
+ - ``%display``
16
+
17
+ - ``%mode`` (like ``%maxima``, etc.)
18
+
19
+ - ``%%cython``
20
+
21
+ - ``%%fortran``
22
+
23
+ * preparsing of input
24
+
25
+ * loading Sage library
26
+
27
+ * running init.sage
28
+
29
+ * changing prompt to Sage prompt
30
+
31
+ * Display hook
32
+
33
+ TESTS:
34
+
35
+ We test that preparsing is off for ``%runfile``, on for ``%time``::
36
+
37
+ sage: import os, re
38
+ sage: from sage.repl.interpreter import get_test_shell
39
+ sage: from sage.misc.temporary_file import tmp_dir
40
+ sage: shell = get_test_shell()
41
+ sage: TMP = tmp_dir()
42
+ sage: TMP = os.path.join(TMP, "12345", "temp")
43
+ sage: os.makedirs(TMP)
44
+
45
+ The temporary directory should have a name of the form
46
+ ``.../12345/...``, to demonstrate that file names are not
47
+ preparsed when calling ``%runfile`` ::
48
+
49
+ sage: bool(re.search('/12345/', TMP))
50
+ True
51
+ sage: tmp = os.path.join(TMP, 'run_cell.py')
52
+ sage: with open(tmp, 'w') as f:
53
+ ....: _ = f.write('a = 2\n')
54
+ sage: shell.run_cell('%runfile '+tmp)
55
+ sage: shell.run_cell('a')
56
+ 2
57
+
58
+ In contrast, input to the ``%time`` magic command is preparsed::
59
+
60
+ sage: shell.run_cell('%time 594.factor()')
61
+ CPU times: user ...
62
+ Wall time: ...
63
+ 2 * 3^3 * 11
64
+ sage: shell.quit()
65
+ """
66
+
67
+ from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
68
+
69
+ from sage.repl.load import load_wrap
70
+ from sage.env import SAGE_IMPORTALL, SAGE_STARTUP_FILE
71
+ from sage.misc.lazy_import import LazyImport
72
+ from sage.misc.misc import run_once
73
+
74
+
75
+ @magics_class
76
+ class SageMagics(Magics):
77
+
78
+ @line_magic
79
+ def crun(self, s):
80
+ r"""
81
+ Profile C function calls.
82
+
83
+ INPUT:
84
+
85
+ - ``s`` -- string; Sage command to profile
86
+
87
+ EXAMPLES::
88
+
89
+ sage: from sage.repl.interpreter import get_test_shell
90
+ sage: shell = get_test_shell()
91
+ sage: shell.run_cell('%crun sum(1/(1+n^2) for n in range(100))') # optional - gperftools
92
+ PROFILE: interrupts/evictions/bytes = ...
93
+ Using local file ...
94
+ Using local file ...
95
+ sage: shell.quit()
96
+ """
97
+ import sage.misc.gperftools
98
+ sage.misc.gperftools.crun(s, evaluator=self.shell.ex)
99
+
100
+ @line_magic
101
+ def runfile(self, s):
102
+ r"""
103
+ Execute the code contained in the file ``s``.
104
+
105
+ This is designed to be used from the command line as
106
+ ``%runfile /path/to/file``.
107
+
108
+ - ``s`` -- string; the file to be loaded
109
+
110
+ EXAMPLES::
111
+
112
+ sage: import os
113
+ sage: from sage.repl.interpreter import get_test_shell
114
+ sage: from sage.misc.temporary_file import tmp_dir
115
+ sage: shell = get_test_shell()
116
+ sage: tmp = os.path.join(tmp_dir(), 'run_cell.py')
117
+ sage: with open(tmp, 'w') as f:
118
+ ....: _ = f.write('a = 2\n')
119
+ sage: shell.run_cell('%runfile '+tmp)
120
+ sage: shell.run_cell('a')
121
+ 2
122
+ sage: shell.quit()
123
+ """
124
+ return self.shell.ex(load_wrap(s, attach=False))
125
+
126
+ @line_magic
127
+ def attach(self, s):
128
+ r"""
129
+ Attach the code contained in the file ``s``.
130
+
131
+ This is designed to be used from the command line as ``%attach
132
+ /path/to/file``.
133
+
134
+ - ``s`` -- string. The file to be attached
135
+
136
+ EXAMPLES::
137
+
138
+ sage: from sage.repl.interpreter import get_test_shell
139
+ sage: shell = get_test_shell()
140
+ sage: from tempfile import NamedTemporaryFile as NTF
141
+ sage: with NTF(mode='w+t', suffix='.py', delete=False) as f:
142
+ ....: _ = f.write('a = 2\n')
143
+ sage: shell.run_cell('%attach ' + f.name)
144
+ sage: shell.run_cell('a')
145
+ 2
146
+ sage: sleep(1) # filesystem timestamp granularity
147
+ sage: with open(f.name, 'w') as f: _ = f.write('a = 3\n')
148
+
149
+ Note that the doctests are never really at the command prompt, so
150
+ we call the input hook manually::
151
+
152
+ sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
153
+ sage: shell.run_cell('reload_attached_files_if_modified()')
154
+ ### reloading attached file ... modified at ... ###
155
+
156
+ sage: shell.run_cell('a')
157
+ 3
158
+ sage: shell.run_cell('detach(%r)' % f.name)
159
+ sage: shell.run_cell('attached_files()')
160
+ []
161
+ sage: os.remove(f.name)
162
+ sage: shell.quit()
163
+ """
164
+ return self.shell.ex(load_wrap(s, attach=True))
165
+
166
+ @line_magic
167
+ def iload(self, args):
168
+ """
169
+ A magic command to interactively load a file as in MAGMA.
170
+
171
+ - ``args`` -- string. The file to be interactively loaded
172
+
173
+ .. NOTE::
174
+
175
+ Currently, this cannot be completely doctested as it
176
+ relies on :func:`raw_input`.
177
+
178
+ EXAMPLES::
179
+
180
+ sage: ip = get_ipython() # not tested: works only in interactive shell
181
+ sage: ip.magic_iload('/dev/null') # not tested: works only in interactive shell
182
+ Interactively loading "/dev/null" # not tested: works only in interactive shell
183
+ """
184
+ content = self.shell.find_user_code(args).splitlines()
185
+
186
+ # we create a stack so e.g. having an iload inside of an iload
187
+ # will process the inner iload and then resume the outer iload
188
+ orig_readline = self.shell.pre_readline
189
+
190
+ def pre_readline():
191
+ if self.shell.rl_next_input is None:
192
+ self.shell.rl_next_input = content.pop(0)
193
+ self.shell.rl_do_indent = False
194
+ orig_readline()
195
+ if not content:
196
+ # restore original hook
197
+ self.shell.readline_startup_hook(orig_readline)
198
+ self.shell.pre_readline = orig_readline
199
+
200
+ self.shell.readline_startup_hook(pre_readline)
201
+ self.shell.pre_readline = pre_readline
202
+
203
+ print('Interactively loading "%s"' % args)
204
+
205
+ _magic_display_status = 'simple'
206
+
207
+ @line_magic
208
+ def display(self, args):
209
+ r"""
210
+ A magic command to switch between simple display and ASCII art display.
211
+
212
+ - ``args`` -- string. See
213
+ :mod:`sage.repl.rich_output.preferences`
214
+ for allowed values. If the mode is ``ascii_art``, it can
215
+ optionally be followed by a width.
216
+
217
+ How to use: if you want to activate the ASCII art mode::
218
+
219
+ sage: from sage.repl.interpreter import get_test_shell
220
+ sage: shell = get_test_shell()
221
+ sage: shell.run_cell('%display ascii_art')
222
+
223
+ That means you do not have to use :func:`ascii_art` to get an ASCII art
224
+ output::
225
+
226
+ sage: shell.run_cell("i = var('i')") # needs sage.symbolic
227
+ sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic
228
+ 10 9 8 7 6 5 4 3 2
229
+ 100*x + 81*x + 64*x + 49*x + 36*x + 25*x + 16*x + 9*x + 4*x + x
230
+
231
+ Then when you want to return to 'textual mode'::
232
+
233
+ sage: shell.run_cell('%display text plain')
234
+ sage: shell.run_cell('%display plain') # shortcut for "text plain"
235
+ sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic
236
+ 100*x^10 + 81*x^9 + 64*x^8 + 49*x^7 + 36*x^6 + 25*x^5 + 16*x^4 + 9*x^3 + 4*x^2 + x
237
+
238
+ Sometime you could have to use a special output width and you
239
+ could specify it::
240
+
241
+ sage: shell.run_cell('%display ascii_art')
242
+ sage: shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat
243
+ [
244
+ [ 1 4 1 3
245
+ [ 1 3 4 1 2 4 1 2 3 1 3 1 2 2 2
246
+ [ 1 2 3 4, 2 , 3 , 4 , 2 4, 3 4, 3 , 4 ,
247
+ <BLANKLINE>
248
+ 1 ]
249
+ 1 2 2 ]
250
+ 3 3 ]
251
+ 4 , 4 ]
252
+ sage: shell.run_cell('%display ascii_art 50')
253
+ sage: shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat
254
+ [
255
+ [
256
+ [ 1 3 4 1 2 4 1 2 3
257
+ [ 1 2 3 4, 2 , 3 , 4 ,
258
+ <BLANKLINE>
259
+ 1 ]
260
+ 1 4 1 3 1 2 2 ]
261
+ 1 3 1 2 2 2 3 3 ]
262
+ 2 4, 3 4, 3 , 4 , 4 , 4 ]
263
+
264
+ As yet another option, typeset mode. This is used in the emacs
265
+ interface::
266
+
267
+ sage: shell.run_cell('%display text latex')
268
+ sage: shell.run_cell('1/2')
269
+ 1/2
270
+
271
+ Switch back::
272
+
273
+ sage: shell.run_cell('%display default')
274
+
275
+ Switch graphics to default to vector or raster graphics file
276
+ formats::
277
+
278
+ sage: shell.run_cell('%display graphics vector')
279
+
280
+ TESTS::
281
+
282
+ sage: shell.run_cell('%display invalid_mode')
283
+ value must be unset (None) or one of ('plain', 'ascii_art', 'unicode_art', 'latex'), got invalid_mode
284
+ sage: shell.quit()
285
+ """
286
+ from sage.repl.rich_output import get_display_manager
287
+ dm = get_display_manager()
288
+ args = args.strip().split()
289
+ if not args:
290
+ print(dm.preferences)
291
+ return
292
+ arg0 = args[0]
293
+ # deprecated values
294
+ if arg0 == 'simple':
295
+ dm.preferences.text = 'plain'
296
+ elif arg0 == 'typeset':
297
+ dm.preferences.text = 'latex'
298
+ elif arg0 in ['ascii_art', 'unicode_art'] and len(args) > 1:
299
+ try:
300
+ max_width = int(args[1])
301
+ except ValueError:
302
+ max_width = 0
303
+ if max_width <= 0:
304
+ raise ValueError(
305
+ "max width must be a positive integer")
306
+ import sage.typeset.character_art as character_art
307
+ character_art.MAX_WIDTH = max_width
308
+ dm.preferences.text = arg0
309
+ # Unset all
310
+ elif arg0 in ['default', 'None']: # un-stringify "%display None"
311
+ for option in map(str, dm.preferences.available_options()):
312
+ delattr(dm.preferences, option)
313
+ # Normal argument handling
314
+ elif arg0 in map(str, dm.preferences.available_options()) and len(args) <= 2:
315
+ if len(args) == 1:
316
+ # "%display text" => get current value
317
+ print(getattr(dm.preferences, arg0))
318
+ else:
319
+ # "%display text latex" => set new value
320
+ assert len(args) == 2
321
+ if args[1] in ['default', 'None']:
322
+ delattr(dm.preferences, arg0)
323
+ else:
324
+ try:
325
+ setattr(dm.preferences, arg0, args[1])
326
+ except ValueError as err:
327
+ print(err) # do not show traceback
328
+ # If all else fails: assume text
329
+ else:
330
+ try:
331
+ dm.preferences.text = arg0
332
+ except ValueError as err:
333
+ print(err) # do not show traceback
334
+
335
+ @cell_magic
336
+ def cython(self, line, cell):
337
+ """
338
+ Cython cell magic.
339
+
340
+ This is syntactic sugar on the
341
+ :func:`~sage.misc.cython.cython_compile` function.
342
+
343
+ INPUT:
344
+
345
+ - ``line`` -- ignored
346
+
347
+ - ``cell`` -- string; the Cython source code to process
348
+
349
+ OUTPUT: none; the Cython code is compiled and loaded
350
+
351
+ EXAMPLES::
352
+
353
+ sage: from sage.repl.interpreter import get_test_shell
354
+ sage: shell = get_test_shell()
355
+ sage: shell.run_cell( # needs sage.misc.cython
356
+ ....: '''
357
+ ....: %%cython
358
+ ....: def f():
359
+ ....: print('test')
360
+ ....: ''')
361
+ sage: f() # needs sage.misc.cython
362
+ test
363
+ """
364
+ from sage.misc.cython import cython_compile
365
+ return cython_compile(cell)
366
+
367
+ @cell_magic
368
+ def fortran(self, line, cell):
369
+ """
370
+ Fortran cell magic.
371
+
372
+ This is syntactic sugar on the
373
+ :func:`~sage.misc.inline_fortran.fortran` function.
374
+
375
+ INPUT:
376
+
377
+ - ``line`` -- ignored
378
+
379
+ - ``cell`` -- string; the Cython source code to process
380
+
381
+ OUTPUT: none; the Fortran code is compiled and loaded
382
+
383
+ EXAMPLES::
384
+
385
+ sage: # needs numpy
386
+ sage: from sage.repl.interpreter import get_test_shell
387
+ sage: shell = get_test_shell()
388
+ sage: shell.run_cell('''
389
+ ....: %%fortran
390
+ ....: C FILE: FIB1.F
391
+ ....: SUBROUTINE FIB(A,N)
392
+ ....: C
393
+ ....: C CALCULATE FIRST N FIBONACCI NUMBERS
394
+ ....: C
395
+ ....: INTEGER N
396
+ ....: REAL*8 A(N)
397
+ ....: DO I=1,N
398
+ ....: IF (I.EQ.1) THEN
399
+ ....: A(I) = 0.0D0
400
+ ....: ELSEIF (I.EQ.2) THEN
401
+ ....: A(I) = 1.0D0
402
+ ....: ELSE
403
+ ....: A(I) = A(I-1) + A(I-2)
404
+ ....: ENDIF
405
+ ....: ENDDO
406
+ ....: END
407
+ ....: C END FILE FIB1.F
408
+ ....: ''')
409
+ sage: fib
410
+ <fortran ...>
411
+ sage: from numpy import array
412
+ sage: a = array(range(10), dtype=float)
413
+ sage: fib(a, 10)
414
+ sage: a
415
+ array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.])
416
+ """
417
+ from sage.misc.inline_fortran import fortran
418
+ return fortran(cell)
419
+
420
+
421
+ class SageCustomizations:
422
+
423
+ def __init__(self, shell=None):
424
+ """
425
+ Initialize the Sage plugin.
426
+ """
427
+ self.shell = shell
428
+
429
+ self.auto_magics = SageMagics(shell)
430
+ self.shell.register_magics(self.auto_magics)
431
+
432
+ self.shell.set_hook('editor', LazyImport("sage.misc.edit_module", "edit_devel"))
433
+
434
+ self.init_inspector()
435
+ self.init_line_transforms()
436
+
437
+ try:
438
+ import sage.all # until sage's import hell is fixed
439
+ except ImportError:
440
+ import sage.all__sagemath_repl
441
+
442
+ self.shell.verbose_quit = True
443
+
444
+ self.register_interface_magics()
445
+
446
+ if SAGE_IMPORTALL == 'yes':
447
+ self.init_environment()
448
+
449
+ def register_interface_magics(self):
450
+ """
451
+ Register magics for each of the Sage interfaces
452
+ """
453
+ from sage.repl.interface_magic import InterfaceMagic
454
+ InterfaceMagic.register_all(self.shell)
455
+
456
+ @staticmethod
457
+ def all_globals():
458
+ """
459
+ Return a Python module containing all globals which should be
460
+ made available to the user.
461
+
462
+ EXAMPLES::
463
+
464
+ sage: from sage.repl.ipython_extension import SageCustomizations
465
+ sage: SageCustomizations.all_globals()
466
+ <module 'sage.all_cmdline' ...>
467
+ """
468
+ try:
469
+ from sage import all_cmdline
470
+ except ImportError:
471
+ from sage import all__sagemath_repl as all_cmdline
472
+ return all_cmdline
473
+
474
+ def init_environment(self):
475
+ """
476
+ Set up Sage command-line environment
477
+ """
478
+ # import outside of cell so we don't get a traceback
479
+ from sage.repl.user_globals import initialize_globals
480
+ initialize_globals(self.all_globals(), self.shell.user_ns)
481
+ self.run_init()
482
+
483
+ def run_init(self):
484
+ """
485
+ Run Sage's initial startup file.
486
+ """
487
+ try:
488
+ with open(SAGE_STARTUP_FILE) as f:
489
+ self.shell.run_cell(f.read(), store_history=False)
490
+ except OSError:
491
+ pass
492
+
493
+ def init_inspector(self):
494
+ # Ideally, these would just be methods of the Inspector class
495
+ # that we could override; however, IPython looks them up in
496
+ # the global :class:`IPython.core.oinspect` module namespace.
497
+ # Thus, we have to monkey-patch.
498
+ import IPython.core.oinspect
499
+ IPython.core.oinspect.getdoc = LazyImport("sage.misc.sageinspect", "sage_getdoc")
500
+ IPython.core.oinspect.getsource = LazyImport("sage.misc.sagedoc", "my_getsource")
501
+ IPython.core.oinspect.find_file = LazyImport("sage.misc.sageinspect", "sage_getfile")
502
+ IPython.core.oinspect.getargspec = LazyImport("sage.misc.sageinspect", "sage_getargspec")
503
+
504
+ def init_line_transforms(self):
505
+ """
506
+ Set up transforms (like the preparser).
507
+
508
+ TESTS:
509
+
510
+ Check that :issue:`31951` is fixed::
511
+
512
+ sage: from IPython import get_ipython
513
+ sage: ip = get_ipython()
514
+ sage: ip.input_transformer_manager.check_complete(''' # indirect doctest
515
+ ....: for i in [1 .. 2]:
516
+ ....: a = 2''')
517
+ ('incomplete', 2)
518
+ sage: ip.input_transformer_manager.check_complete('''
519
+ ....: def foo(L)
520
+ ....: K.<a> = L''')
521
+ ('invalid', None)
522
+ sage: ip.input_transformer_manager.check_complete('''
523
+ ....: def foo(L):
524
+ ....: K.<a> = L''')
525
+ ('incomplete', 4)
526
+ sage: ip.input_transformer_manager.check_complete('''
527
+ ....: def foo(L):
528
+ ....: K.<a> = L''')
529
+ ('incomplete', 4)
530
+ sage: ip.input_transformer_manager.check_complete('''
531
+ ....: def foo(R):
532
+ ....: a = R.0''')
533
+ ('incomplete', 4)
534
+ sage: ip.input_transformer_manager.check_complete('''
535
+ ....: def foo(a):
536
+ ....: b = 2a''')
537
+ ('invalid', None)
538
+ sage: implicit_multiplication(True)
539
+ sage: ip.input_transformer_manager.check_complete('''
540
+ ....: def foo(a):
541
+ ....: b = 2a''')
542
+ ('incomplete', 4)
543
+ sage: ip.input_transformer_manager.check_complete('''
544
+ ....: def foo():
545
+ ....: f(x) = x^2''')
546
+ ('incomplete', 4)
547
+ sage: ip.input_transformer_manager.check_complete('''
548
+ ....: def foo():
549
+ ....: 2.factor()''')
550
+ ('incomplete', 4)
551
+ """
552
+ from IPython.core.inputtransformer2 import TransformerManager
553
+ from .interpreter import SagePromptTransformer, SagePreparseTransformer
554
+
555
+ self.shell.input_transformer_manager.cleanup_transforms.insert(1, SagePromptTransformer)
556
+ self.shell.input_transformers_post.append(SagePreparseTransformer)
557
+
558
+ # Create an input transformer that does Sage's special syntax in the first step.
559
+ # We append Sage's preparse to the cleanup step, so that ``check_complete`` recognizes
560
+ # Sage's special syntax.
561
+ # Behaviour is somewhat inconsistent, but the syntax is recognized as desired.
562
+ M = TransformerManager()
563
+ M.token_transformers = self.shell.input_transformer_manager.token_transformers
564
+ M.cleanup_transforms.insert(1, SagePromptTransformer)
565
+ M.cleanup_transforms.append(SagePreparseTransformer)
566
+ self.shell._check_complete_transformer = M
567
+ self.shell.input_transformer_manager.check_complete = M.check_complete
568
+
569
+
570
+ class SageJupyterCustomizations(SageCustomizations):
571
+ @staticmethod
572
+ def all_globals():
573
+ """
574
+ Return a Python module containing all globals which should be
575
+ made available to the user when running the Jupyter notebook.
576
+
577
+ EXAMPLES::
578
+
579
+ sage: from sage.repl.ipython_extension import SageJupyterCustomizations
580
+ sage: SageJupyterCustomizations.all_globals()
581
+ <module 'sage.repl.ipython_kernel.all_jupyter' ...>
582
+ """
583
+ from .ipython_kernel import all_jupyter
584
+ return all_jupyter
585
+
586
+
587
+ @run_once
588
+ def load_ipython_extension(ip):
589
+ """
590
+ Load the extension in IPython.
591
+ """
592
+ # this modifies ip
593
+ SageCustomizations(shell=ip)
@@ -0,0 +1 @@
1
+ # sage_setup: distribution = sagemath-repl
@@ -0,0 +1,4 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ from ipykernel.kernelapp import IPKernelApp
3
+ from sage.repl.ipython_kernel.kernel import SageKernel
4
+ IPKernelApp.launch_instance(kernel_class=SageKernel)
@@ -0,0 +1,10 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ """
3
+ All imports for Jupyter
4
+ """
5
+
6
+ from sage.all_cmdline import *
7
+
8
+ from sage.repl.ipython_kernel.widgets_sagenb import (input_box, text_control, slider,
9
+ range_slider, checkbox, selector, input_grid, color_selector)
10
+ from sage.repl.ipython_kernel.interact import interact