Hi Robert B and all, Does anyone know a trick to access the global variables for the "main" scope?
In the attached patch (for 4.2; untested on 4.3), I am implementing the following in sage.misc.misc_c: def inject_variable(name, value): """ sage: from sage.misc.misc_c import inject_variable sage: inject_variable("a", 314) sage: a 314 """ G = globals() G[name] = value When called from the interpreter, this works just fine. However, the purpose is to call this function from library code, and then it breaks. More precisely, if I call this from a Python function, say `sage.misc.misc.inject_variable_test` as in the patch, then the globals() call returns instead the global variables for the module `sage.misc.misc`. Tips most welcome! Cheers, Nicolas -- Nicolas M. ThiƩry "Isil" <nthi...@users.sf.net> http://Nicolas.Thiery.name/ -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
#....: Implements centralized sage.misc.misc_c.inject_variable diff --git a/sage/misc/misc.py b/sage/misc/misc.py --- a/sage/misc/misc.py +++ b/sage/misc/misc.py @@ -2215,3 +2215,41 @@ def is_in_string(line, pos): i += 1 return in_quote() + + + +def inject_variable_test(i): + """ + sage: sage.misc.misc_c.inject_variable("ble", 2) + sage: ble + 2 + + sage: def inject_variable_test(i): + ... sage.misc.misc_c.inject_variable("bli", i) + sage: inject_variable_test(314) + sage: bli + 314 + sage: inject_variable_test(271) + doctest:...: RuntimeWarning: Redefining global value bli + sage: bli + 271 + sage: sage.misc.misc.inject_variable_test2 = inject_variable_test + sage: sage.misc.misc.inject_variable_test2(123) + doctest:...: RuntimeWarning: Redefining global value bli + sage: bli + 123 + + sage: sage.misc.misc.inject_variable_test(314) + sage: bla + 314 + sage: sage.misc.misc.inject_variable_test(271) + doctest:...: RuntimeWarning: Redefining global value bla + sage: bla + 271 + """ + import __main__ + print __main__.myglobals() + from sage.misc.misc_c import inject_variable + inject_variable("bla",i) + import sage + sage.calculus.var.var("truc") diff --git a/sage/misc/misc_c.pyx b/sage/misc/misc_c.pyx --- a/sage/misc/misc_c.pyx +++ b/sage/misc/misc_c.pyx @@ -13,6 +13,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** import sys +from warnings import warn include "../ext/stdsage.pxi" include "../ext/python_sequence.pxi" @@ -957,3 +958,19 @@ cpdef list normalize_index(object key, i else: raise TypeError, "index must be an integer or slice" return return_list + +cpdef inject_variable(name, value): + """ + sage: from sage.misc.misc_c import inject_variable + sage: inject_variable("a", 314) + sage: a + 314 + sage: inject_variable("a", 271) + doctest:...: RuntimeWarning: Redefining global value a + sage: a + 271 + """ + G = globals() + if name in G: + warn("Redefining global value %s"%name, RuntimeWarning, stacklevel = 1) + G[name] = value