Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 9ccd889

Browse files
committed
Use user_globals instead of Cython's old_style_globals
1 parent b0a4317 commit 9ccd889

File tree

17 files changed

+85
-123
lines changed

17 files changed

+85
-123
lines changed

src/sage/calculus/var.pyx

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
"""
2+
Symbolic variables
3+
"""
4+
5+
#*****************************************************************************
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 2 of the License, or
9+
# (at your option) any later version.
10+
# http://www.gnu.org/licenses/
11+
#*****************************************************************************
12+
113
from sage.symbolic.function_factory import function as new_function
214
from sage.symbolic.ring import SR
15+
from sage.repl.user_globals import set_global, get_globals
316

417
def var(*args, **kwds):
518
r"""
@@ -109,40 +122,17 @@ def var(*args, **kwds):
109122
<type 'sage.symbolic.expression.Expression'>
110123
sage: parent(theta)
111124
Symbolic Ring
112-
113-
TESTS::
114-
115-
sage: var('q',ns=False)
116-
Traceback (most recent call last):
117-
...
118-
NotImplementedError: The new (Pynac) symbolics are now the only symbolics; please do not use keyword `ns` any longer.
119-
sage: q
120-
Traceback (most recent call last):
121-
...
122-
NameError: name 'q' is not defined
123-
sage: var('q',ns=1)
124-
doctest:...: DeprecationWarning: The new (Pynac) symbolics are now the only symbolics; please do not use keyword 'ns' any longer.
125-
See http://trac.sagemath.org/6559 for details.
126-
q
127125
"""
128126
if len(args)==1:
129127
name = args[0]
130128
else:
131129
name = args
132-
G = globals() # this is the reason the code must be in Cython.
133-
if 'ns' in kwds:
134-
if kwds['ns']:
135-
from sage.misc.superseded import deprecation
136-
deprecation(6559, "The new (Pynac) symbolics are now the only symbolics; please do not use keyword 'ns' any longer.")
137-
else:
138-
raise NotImplementedError("The new (Pynac) symbolics are now the only symbolics; please do not use keyword `ns` any longer.")
139-
kwds.pop('ns')
140130
v = SR.var(name, **kwds)
141131
if isinstance(v, tuple):
142132
for x in v:
143-
G[repr(x)] = x
133+
set_global(repr(x), x)
144134
else:
145-
G[repr(v)] = v
135+
set_global(repr(v), v)
146136
return v
147137

148138
def function(s, *args, **kwds):
@@ -226,13 +216,12 @@ def function(s, *args, **kwds):
226216
if len(args) > 0:
227217
return function(s, **kwds)(*args)
228218

229-
G = globals() # this is the reason the code must be in Cython.
230219
v = new_function(s, **kwds)
231220
if isinstance(v, tuple):
232221
for x in v:
233-
G[repr(x)] = x
222+
set_global(repr(x), x)
234223
else:
235-
G[repr(v)] = v
224+
set_global(repr(v), v)
236225
return v
237226

238227

@@ -259,7 +248,7 @@ def clear_vars():
259248
sage: k
260249
15
261250
"""
262-
G = globals()
251+
G = get_globals()
263252
from sage.symbolic.ring import is_SymbolicVariable
264253
for i in range(65,65+26) + range(97,97+26):
265254
if chr(i) in G and is_SymbolicVariable(G[chr(i)]):
@@ -271,6 +260,3 @@ def clear_vars():
271260
G[chr(i)].pyobject()
272261
except TypeError:
273262
del G[chr(i)]
274-
275-
276-

src/sage/combinat/ncsf_qsym/tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
means the partition obtained by sorting the composition `c`::
142142
143143
sage: SymmetricFunctions(QQ).inject_shorthands()
144-
doctest:...: RuntimeWarning: redefining global value `e`
144+
doctest:...: RuntimeWarning: redefining global value 'e'
145145
sage: m[2,1]
146146
m[2, 1]
147147
sage: M(m[2,1])

src/sage/combinat/quickref.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
6161
sage: Sym = SymmetricFunctions(QQ); Sym.inject_shorthands()
6262
...
63-
doctest:...: RuntimeWarning: redefining global value `e`
63+
doctest:...: RuntimeWarning: redefining global value 'e'
6464
sage: m( ( h[2,1] * (1 + 3 * p[2,1]) ) + s[2](s[3]) )
6565
3*m[1, 1, 1] + ... + 10*m[5, 1] + 4*m[6]
6666

src/sage/combinat/sf/sf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ class SymmetricFunctions(UniqueRepresentation, Parent):
185185
::
186186
187187
sage: Sym.inject_shorthands()
188-
doctest:...: RuntimeWarning: redefining global value `h`
189-
doctest:...: RuntimeWarning: redefining global value `s`
190-
doctest:...: RuntimeWarning: redefining global value `e`
191-
doctest:...: RuntimeWarning: redefining global value `m`
192-
doctest:...: RuntimeWarning: redefining global value `p`
188+
doctest:...: RuntimeWarning: redefining global value 'h'
189+
doctest:...: RuntimeWarning: redefining global value 's'
190+
doctest:...: RuntimeWarning: redefining global value 'e'
191+
doctest:...: RuntimeWarning: redefining global value 'm'
192+
doctest:...: RuntimeWarning: redefining global value 'p'
193193
194194
We can then do conversions from one basis to another::
195195

src/sage/ext/interactive_constructors_c.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# inject variables into the global module scope.
33

44
import sage.rings.all
5+
import sage.repl.user_globals
56

67
_verbose=True
78
_inject_mode_off = False
@@ -66,15 +67,15 @@ def inject_on(verbose=True):
6667
global _original_constructors
6768
_original_constructors = {}
6869
import sage.ext.interactive_constructors_c
69-
G = globals()
70+
G = sage.repl.user_globals.get_globals()
7071
if verbose:
7172
print "Redefining:",
7273
for X in sorted(sage.ext.interactive_constructors_c.__dict__.keys()):
7374
if not 'inject' in X and X[0] != '_' and X[:4] != 'sage':
7475
if verbose:
7576
print X,
7677
try:
77-
_original_constructors[X] = G[X] #sage.ext.interactive_constructors_c.__dict__[X]
78+
_original_constructors[X] = G[X]
7879
except KeyError:
7980
pass
8081
G[X] = sage.ext.interactive_constructors_c.__dict__[X]
@@ -84,8 +85,8 @@ def inject_on(verbose=True):
8485
def inject_off():
8586
global _original_constructors
8687
if not _original_constructors is None:
87-
for X in _original_constructors.keys():
88-
globals()[X] = _original_constructors[X]
88+
for X in _original_constructors:
89+
sage.repl.user_globals.set_global(X, _original_constructors[X])
8990

9091
cdef _inject(X, do):
9192
if do:

src/sage/misc/citation.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sage.misc.all import tmp_filename
22
from sage.env import SAGE_ROOT
3+
from sage.repl.user_globals import get_globals
34

45
systems = {}
56
systems['PARI'] = ['sage.libs.pari', 'sage.interfaces.gp']
@@ -79,7 +80,7 @@ def get_systems(cmd):
7980

8081
#Run the command and get the stats
8182
filename = tmp_filename()
82-
cProfile.runctx(cmd, globals(), {}, filename)
83+
cProfile.runctx(cmd, get_globals(), {}, filename)
8384
stats = pstats.Stats(filename)
8485

8586
#Strings is a list of method names and modules which get run

src/sage/misc/cython_c.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ def cython(code,
5757
compiled once.
5858
"""
5959
tmpfile = sage.misc.temporary_file.tmp_filename(ext=".spyx")
60-
open(tmpfile,'w').write(code)
61-
sage.server.support.cython_import_all(tmpfile, globals(),
60+
with open(tmpfile, 'w') as f:
61+
f.write(code)
62+
from sage.repl.user_globals import get_globals
63+
sage.server.support.cython_import_all(tmpfile, get_globals(),
6264
verbose=verbose, compile_message=compile_message,
6365
use_cache=use_cache,
6466
create_local_c_file=False)

src/sage/misc/dev_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def runsnake(command):
7676
"""
7777
import cProfile, os
7878
from sage.misc.temporary_file import tmp_filename
79-
from sage.misc.misc import get_main_globals
79+
from sage.repl.user_globals import get_globals
8080
from sage.repl.preparse import preparse
8181
tmpfile = tmp_filename()
82-
cProfile.runctx(preparse(command.lstrip().rstrip()), get_main_globals(), locals(), filename=tmpfile)
82+
cProfile.runctx(preparse(command.lstrip().rstrip()), get_globals(), locals(), filename=tmpfile)
8383
os.system("/usr/bin/python -E `which runsnake` %s &"%tmpfile)
8484

8585
def import_statement_string(module, names, lazy):

src/sage/misc/fpickle.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def unpickle_function(pickled):
6868
sage: unpickle_function(pickle_function(f))(3,5)
6969
15
7070
"""
71+
from sage.repl.user_globals import get_globals
7172
recovered = cPickle.loads(pickled)
72-
return types.FunctionType(recovered, globals())
73-
73+
return types.FunctionType(recovered, get_globals())
7474

7575

7676
def call_pickled_function(fpargs):

src/sage/misc/misc.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,8 @@ def get_main_globals():
23152315
23162316
sage: from sage.misc.misc import get_main_globals
23172317
sage: G = get_main_globals()
2318+
doctest:...: DeprecationWarning: get_main_globals is deprecated, use sage.repl.user_globals.get_globals() instead
2319+
See http://trac.sagemath.org/18083 for details.
23182320
sage: bla = 1
23192321
sage: G['bla']
23202322
1
@@ -2334,29 +2336,15 @@ def get_main_globals():
23342336
....: G['blo'] = 42
23352337
sage: bli = 14
23362338
sage: f()
2339+
doctest:...: DeprecationWarning: get_main_globals is deprecated, use sage.repl.user_globals.get_globals() instead
2340+
See http://trac.sagemath.org/18083 for details.
23372341
sage: blo
23382342
42
2339-
2340-
ALGORITHM:
2341-
2342-
The main global namespace is discovered by going up the frame
2343-
stack until the frame for the :mod:`__main__` module is found.
2344-
Should this frame not be found (this should not occur in normal
2345-
operation), an exception "ValueError: call stack is not deep
2346-
enough" will be raised by ``_getframe``.
2347-
2348-
See :meth:`inject_variable_test` for a real test that this works
2349-
within deeply nested calls in a function defined in a Python
2350-
module.
23512343
"""
2352-
import sys
2353-
depth = 0
2354-
while True:
2355-
G = sys._getframe(depth).f_globals
2356-
if G.get("__name__", None) == "__main__":
2357-
break
2358-
depth += 1
2359-
return G
2344+
from sage.misc.superseded import deprecation
2345+
deprecation(18083, "get_main_globals is deprecated, use sage.repl.user_globals.get_globals() instead")
2346+
from sage.repl.user_globals import get_globals
2347+
return get_globals()
23602348

23612349

23622350
def inject_variable(name, value):
@@ -2378,7 +2366,7 @@ def inject_variable(name, value):
23782366
A warning is issued the first time an existing value is overwritten::
23792367
23802368
sage: inject_variable("a", 271)
2381-
doctest:...: RuntimeWarning: redefining global value `a`
2369+
doctest:...: RuntimeWarning: redefining global value 'a'
23822370
sage: a
23832371
271
23842372
sage: inject_variable("a", 272)
@@ -2398,9 +2386,10 @@ def inject_variable(name, value):
23982386
# Using globals() does not work, even in Cython, because
23992387
# inject_variable is called not only from the interpreter, but
24002388
# also from functions in various modules.
2401-
G = get_main_globals()
2389+
from sage.repl.user_globals import get_globals
2390+
G = get_globals()
24022391
if name in G:
2403-
warn("redefining global value `%s`"%name, RuntimeWarning, stacklevel = 2)
2392+
warn("redefining global value {!r}".format(name), RuntimeWarning, stacklevel=2)
24042393
G[name] = value
24052394

24062395

@@ -2421,7 +2410,7 @@ def inject_variable_test(name, value, depth):
24212410
sage: a2
24222411
314
24232412
sage: inject_variable_test("a2", 271, 2)
2424-
doctest:...: RuntimeWarning: redefining global value `a2`
2413+
doctest:...: RuntimeWarning: redefining global value 'a2'
24252414
sage: a2
24262415
271
24272416

0 commit comments

Comments
 (0)