On Dec 7, 2007 9:32 AM, pgdoyle <[EMAIL PROTECTED]> wrote: > > Consider this python program: > > ---------------- > def foo(): > return 'foo' > > print foo() > > def mumble(): > print 'mumble',foo() > > mumble() > ---------------- > > If put this in a file foo.py and type `python foo.py' it prints > > foo > mumble foo > > Same thing if I type 'sage foo.py'. Same thing if I put it in a cell > of a sage notebook and evaluate with sage. > But if switch the evaluation option for the notebook from sage to > python I get an error: > > foo > mumble > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/home/doyle/.sage/sage_notebook/worksheets/admin/2/code/ > 68.py", line 12, in <module> > print mumble()''', '/home/doyle/.sage/sage_notebook/worksheets/ > admin/2/cells/5') > File "/usr/local/sage/local/lib/python2.5/site-packages/sage/server/ > support.py", line 258, in syseval > return system.eval(cmd, locals = sage_globals) > File "/usr/local/sage/local/lib/python2.5/site-packages/sage/misc/ > python.py", line 21, in eval > eval(z, globals, locals) > File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ > sage/", line 1, in <module> > > File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ > sage/", line 7, in mumble > > NameError: global name 'foo' is not defined > > I get the same error if I change the notebook evaluation option back > to sage, put %python at the beginning of the cell, and evaluate. > > Is this a bug or a feature?
This is definitely a bug. Thanks for reporting it! I've put it here: http://trac.sagemath.org/sage_trac/ticket/1423 I only have a minute at the moment, but just want to remark that when you do %python in the notebook or use python mode, here is the Python code that actually evaluates the input to the cell: sage: python.eval?? File: /Users/was/s/local/lib/python2.5/site-packages/sage/misc/python.py Source Code (starting at line 5): def eval(self, x, globals={}, locals={}): x = x.strip() y = x.split('\n') if len(y) == 0: return '' s = '\n'.join(y[:-1]) + '\n' t = y[-1] try: z = compile(t + '\n', '', 'single') except SyntaxError: s += '\n' + t z = None #else: # s += '\n' + t eval(compile(s, '', 'exec'), globals, locals) if not z is None: eval(z, globals, locals) return '' Looking at that code, does anybody see why the second global foo gets ignored inside the function body of mumble? I don't immediately see why. William --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~----------~----~----~----~------~----~------~--~---