Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/all.py
8815 views
1
"""
2
all.py -- much of sage is imported into this module, so you don't
3
have to import everything individually.
4
5
TESTS:
6
7
This is to test :trac:`10570`. If the number of stackframes at startup
8
changes due to a patch you made, please check that this was an
9
intended effect of your patch.
10
11
::
12
13
sage: import gc
14
sage: import inspect
15
sage: from sage import *
16
sage: frames = [x for x in gc.get_objects() if inspect.isframe(x)]
17
18
We exclude the known files and check to see that there are no others::
19
20
sage: import os
21
sage: allowed = [os.path.join("lib","python","threading.py")]
22
sage: allowed.append(os.path.join("lib","python","multiprocessing"))
23
sage: allowed.append(os.path.join("sage","doctest"))
24
sage: allowed.append(os.path.join("bin","sage-runtests"))
25
sage: allowed.append(os.path.join("site-packages","IPython"))
26
sage: allowed.append(os.path.join("bin","sage-ipython"))
27
sage: allowed.append("<ipython console>")
28
sage: allowed.append("<doctest sage.all[3]>")
29
sage: allowed.append(os.path.join("sage","combinat","species","generating_series.py"))
30
sage: for i in frames:
31
....: filename, lineno, funcname, linelist, indx = inspect.getframeinfo(i)
32
....: for nm in allowed:
33
....: if nm in filename:
34
....: break
35
....: else:
36
....: print filename
37
....:
38
39
Check that the Sage Notebook is not imported at startup (see
40
:trac:`15335`)::
41
42
sage: sagenb
43
Traceback (most recent call last):
44
...
45
NameError: name 'sagenb' is not defined
46
47
Check lazy import of ``interacts``::
48
49
sage: type(interacts)
50
<type 'sage.misc.lazy_import.LazyImport'>
51
sage: interacts
52
<module 'sage.interacts.all' from '...'>
53
"""
54
55
#*****************************************************************************
56
# Copyright (C) 2005-2012 William Stein <[email protected]>
57
#
58
# Distributed under the terms of the GNU General Public License (GPL)
59
# as published by the Free Software Foundation; either version 2 of
60
# the License, or (at your option) any later version.
61
#
62
# http://www.gnu.org/licenses/
63
#
64
#*****************************************************************************
65
66
import os, sys
67
import operator
68
69
from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
70
71
if sys.version_info[:2] < (2, 5):
72
print >>sys.stderr, "Sage requires Python 2.5 or newer"
73
sys.exit(1)
74
75
###################################################################
76
77
import sage.ext.c_lib
78
sage.ext.c_lib._init_csage()
79
sig_on_count = sage.ext.c_lib._sig_on_reset
80
81
from time import sleep
82
83
from sage.ext.c_lib import AlarmInterrupt, SignalError
84
85
import sage.misc.lazy_import
86
from sage.misc.all import * # takes a while
87
88
from sage.misc.sh import sh
89
90
from sage.libs.all import *
91
from sage.doctest.all import *
92
try:
93
from sage.dev.all import *
94
except ImportError:
95
pass # dev scripts are disabled
96
97
from sage.rings.all import *
98
from sage.matrix.all import *
99
100
# This must come before Calculus -- it initializes the Pynac library.
101
import sage.symbolic.pynac
102
103
from sage.modules.all import *
104
from sage.monoids.all import *
105
from sage.algebras.all import *
106
from sage.modular.all import *
107
from sage.schemes.all import *
108
from sage.graphs.all import *
109
from sage.groups.all import *
110
from sage.databases.all import *
111
from sage.structure.all import *
112
from sage.categories.all import *
113
from sage.sets.all import *
114
from sage.probability.all import *
115
from sage.interfaces.all import *
116
117
from sage.symbolic.all import *
118
119
from sage.functions.all import *
120
from sage.calculus.all import *
121
122
from sage.server.all import *
123
import sage.tests.all as tests
124
125
from sage.crypto.all import *
126
import sage.crypto.mq as mq
127
128
from sage.plot.all import *
129
from sage.plot.plot3d.all import *
130
131
from sage.coding.all import *
132
from sage.combinat.all import *
133
134
from sage.lfunctions.all import *
135
136
from sage.geometry.all import *
137
from sage.geometry.triangulation.all import *
138
from sage.geometry.riemannian_manifolds.all import *
139
140
from sage.dynamics.all import *
141
142
from sage.homology.all import *
143
144
from sage.quadratic_forms.all import *
145
146
from sage.gsl.all import *
147
148
from sage.games.all import *
149
150
from sage.media.all import *
151
152
from sage.logic.all import *
153
154
from sage.numerical.all import *
155
156
from sage.stats.all import *
157
import sage.stats.all as stats
158
159
import sage.finance.all as finance
160
161
from sage.parallel.all import *
162
163
from sage.ext.fast_callable import fast_callable
164
from sage.ext.fast_eval import fast_float
165
166
sage.misc.lazy_import.lazy_import('sage.sandpiles.all', '*', globals())
167
168
from sage.tensor.all import *
169
170
from sage.matroids.all import *
171
172
# Lazily import notebook functions and interacts (#15335)
173
lazy_import('sagenb.notebook.notebook_object', 'notebook')
174
lazy_import('sagenb.notebook.notebook_object', 'inotebook')
175
lazy_import('sagenb.notebook.sage_email', 'email')
176
lazy_import('sagenb.notebook.interact', 'interact')
177
lazy_import('sage.interacts', 'all', 'interacts')
178
from sage.interacts.debugger import debug
179
180
from copy import copy, deepcopy
181
182
# The code executed here uses a large amount of Sage components
183
from sage.rings.qqbar import _init_qqbar
184
_init_qqbar()
185
186
#Deprecate the is_* functions from the top level
187
#All of these functions should be removed from the top level
188
#after a few releases, and this code should be removed.
189
#--Mike Hansen 9/25/2008
190
message = "\nUsing %(name)s from the top level is deprecated since it was designed to be used by developers rather than end users.\nIt most likely does not do what you would expect it to do. If you really need to use it, import it from the module that it is defined in."
191
sage.misc.superseded.deprecated_callable_import(
192
10107, None, globals(), locals(),
193
[name for name in globals().keys() if name.startswith('is_') and name[3].isupper()],
194
message)
195
196
del message, name
197
198
199
###########################################################
200
#### WARNING:
201
# DO *not* import numpy / matplotlib / networkx here!!
202
# Each takes a surprisingly long time to initialize,
203
# and that initialization should be done more on-the-fly
204
# when they are first needed.
205
###########################################################
206
207
###################################################################
208
209
# maximize memory resources
210
#try:
211
# import resource # unix only...
212
# resource.setrlimit(resource.RLIMIT_AS, (-1,-1))
213
#except Exception:
214
# pass
215
216
# very useful 2-letter shortcuts
217
CC = ComplexField()
218
QQ = RationalField()
219
RR = RealField() # default real field
220
ZZ = IntegerRing()
221
# NOTE: QQ, RR, and ZZ are used by the pre-parser, and should not be
222
# overwritten by the user, unless they want to change the meaning of
223
# int and real in the interpreter (which is a potentially valid thing
224
# to do, and doesn't mess up anything else in the Sage library).
225
# E.g., typing "int = ZZ" in the Sage interpreter makes int literals
226
# acts as Python ints again.
227
228
229
230
# Some shorter shortcuts:
231
# Q = QQ
232
# Z = ZZ
233
# C = CC
234
#i = CC.gen(0)
235
true = True
236
false = False
237
238
oo = infinity
239
#x = PolynomialRing(QQ,'x').gen()
240
241
from sage.misc.copying import license
242
copying = license
243
copyright = license
244
245
_cpu_time_ = cputime()
246
_wall_time_ = walltime()
247
248
def quit_sage(verbose=True):
249
"""
250
If you use Sage in library mode, you should call this function
251
when your application quits.
252
253
It makes sure any child processes are also killed, etc.
254
"""
255
if verbose:
256
t1 = cputime(_cpu_time_)
257
t1m = int(t1/60); t1s=t1-t1m*60
258
t2 = walltime(_wall_time_)
259
t2m = int(t2/60); t2s=t2-t2m*60
260
print "Exiting Sage (CPU time %sm%.2fs, Wall time %sm%.2fs)."%(
261
t1m,t1s,t2m,t2s)
262
263
import gc
264
gc.collect()
265
266
from sage.interfaces.quit import expect_quitall
267
expect_quitall(verbose=verbose)
268
269
import sage.matrix.matrix_mod2_dense
270
sage.matrix.matrix_mod2_dense.free_m4ri()
271
272
import sage.libs.flint.flint
273
sage.libs.flint.flint.free_flint_stack()
274
275
# stop the twisted reactor
276
try:
277
from twisted.internet import reactor
278
if reactor.running:
279
reactor.callFromThread(reactor.stop)
280
except ImportError:
281
pass
282
283
# Free globally allocated mpir integers.
284
import sage.rings.integer
285
sage.rings.integer.free_integer_pool()
286
sage.rings.integer.clear_mpz_globals()
287
import sage.algebras.quatalg.quaternion_algebra_element
288
sage.algebras.quatalg.quaternion_algebra_element._clear_globals()
289
290
from sage.libs.all import symmetrica
291
symmetrica.end()
292
293
from sage.ext.interactive_constructors_c import inject_on, inject_off
294
295
sage.structure.sage_object.register_unpickle_override('sage.categories.category', 'Sets', Sets)
296
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'HeckeModules', HeckeModules)
297
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Objects', Objects)
298
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Rings', Rings)
299
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Fields', Fields)
300
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'VectorSpaces', VectorSpaces)
301
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Schemes_over_base', sage.categories.schemes.Schemes_over_base)
302
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'ModularAbelianVarieties', ModularAbelianVarieties)
303
#sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', '', )
304
305
# Cache the contents of star imports.
306
sage.misc.lazy_import.save_cache_file()
307
308
309
### Debugging for Singular, see trac #10903
310
# from sage.libs.singular.ring import poison_currRing
311
# sys.settrace(poison_currRing)
312
313
314
# Write a file indicating that Sage was started up successfully.
315
# This is called by the sage-starts script.
316
def _write_started_file():
317
"""
318
Write a ``sage-started.txt`` file if it does not exist. The
319
contents of this file do not matter, only its existence.
320
321
The non-existence of this file will be used as a trigger to run
322
``sage-starts`` during the Sage build.
323
324
TESTS:
325
326
Check that the file exists when Sage is running::
327
328
sage: started_file = os.path.join(SAGE_LOCAL, 'etc', 'sage-started.txt')
329
sage: os.path.isfile(started_file)
330
True
331
"""
332
started_file = os.path.join(SAGE_LOCAL, 'etc', 'sage-started.txt')
333
# Do nothing if the file already exists
334
if os.path.isfile(started_file):
335
return
336
337
# Current time with a resolution of 1 second
338
import datetime
339
t = datetime.datetime.now().replace(microsecond=0)
340
341
O = open(started_file, 'w')
342
O.write("Sage %s was started at %s\n"%(sage.version.version, t))
343
O.close()
344
345
346
# Set a new random number seed as the very last thing
347
# (so that printing initial_seed() and using that seed
348
# in set_random_seed() will result in the same sequence you got at
349
# Sage startup).
350
set_random_seed()
351
352
# From now on it is ok to resolve lazy imports
353
sage.misc.lazy_import.finish_startup()
354
355