Opened 4 years ago

Closed 3 years ago

#10933 closed defect (fixed)

time of magma command fails inside function

Reported by: mariah Owned by: was
Priority: major Milestone: sage-4.7
Component: interfaces Keywords:
Cc: Merged in: sage-4.7.alpha5
Authors: Maarten Derickx Reviewers: Martin Raum
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

def foo(str):
     time magma(str)

foo('1')

fails with

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (18, 0))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/<ipython console> in <module>()

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/<ipython console> in foo(str)

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/local/lib/python2.6/site-packages/IPython/iplib.pyc in ipmagic(self, arg_s)
    951         else:
    952             magic_args = self.var_expand(magic_args,1)
--> 953             return fn(magic_args)
    954 
    955     def ipalias(self,arg_s):

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/local/lib/python2.6/site-packages/IPython/Magic.pyc in magic_time(self, parameter_s)
   1904         if mode=='eval':
   1905             st = clk()
-> 1906             out = eval(code,glob)
   1907             end = clk()
   1908         else:

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/<timed eval> in <module>()

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/local/lib/python2.6/site-packages/sage/interfaces/magma.pyc in __call__(self, x, gens)
    735             pass
    736         
--> 737         A = Expect.__call__(self, x)
    738         if has_cache:
    739             x._magma_cache[self] = A

/home/mariah/sage/sage-4.6.2-x86_64-Linux-core2-fc/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc in __call__(self, x, name)
   1110                 return cls(self, str(x), name=name)
   1111             except TypeError, msg2:
-> 1112                 raise TypeError, msg
   1113 
   1114     def _coerce_from_special_method(self, x):

TypeError: unable to coerce element into magma
sage: 

Attachments (1)

trac_10933.patch (1.3 KB) - added by mderickx 3 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 3 years ago by was

This isn't a magma issue per se. I get a similar issue with gp:

def foo(str):
     time gp(str)

foo('1')

comment:2 Changed 3 years ago by was

sage: def foo(s):
....:     time gp(s)
....:     
sage: foo('1')
NameError: name 's' is not defined

This might also be relevant:

sage: print preparse("""def foo(str):\n   time gp(str)\n\nprint gp('1')""", do_time=True)
def foo(str):
   __time__=misc.cputime(); __wall__=misc.walltime(); gp(str); print "Time: CPU %.2f s, Wall: %.2f s"%(misc.cputime(__time__), misc.walltime(__wall__))

print gp('1')
sage: def foo(str):
....:        __time__=misc.cputime(); __wall__=misc.walltime(); gp(str); print "Time: CPU %.2f s, Wall: %.2f s"%(misc.cputime(__time__), misc.walltime(__wall__))
....: 
sage: 
sage: foo('1')
Time: CPU 0.00 s, Wall: 0.00 s

The next thing I might do is look into exactly what Python is executing... there is something funny going on with the preparser not preparsing the input as expected...

comment:3 Changed 3 years ago by ThomasKluyver

OK, investigated this for IPython. The issue is that it evaluates the expression gp(s) within the main user namespace, not within the function's internal namespace (where s lives). Off the top of my head, I can't think of a neat way for the time command to get the namespace from inside the function.

Changed 3 years ago by mderickx

comment:4 Changed 3 years ago by mderickx

  • Status changed from new to needs_review

I fixed this by making the preparse_ipython preparse the time just as it already happens in the notebook.

comment:5 Changed 3 years ago by mraum

  • Authors set to Maarten Derickx
  • Reviewers set to Martin Raum
  • Status changed from needs_review to positive_review

That's ok, but let us hope that is gets fixed in IPython soon.

comment:6 Changed 3 years ago by ThomasKluyver

We're looking at a way to make %time evaluate things in the correct scope, but in the current ipython trunk, auto-magics (like "time" without the %) are only used in single-line inputs. So you'd have to do:

def f(x):
    %time gp(x)

comment:7 Changed 3 years ago by ThomasKluyver

I've written some simple code to make this work in IPython, if you want to test my branch here: https://github.com/ipython/ipython/pull/295

comment:8 Changed 3 years ago by jdemeyer

  • Merged in set to sage-4.7.alpha5
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.