@@ -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
23622350def 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