Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time of magma command fails inside function #10933

Closed
sagetrac-mariah mannequin opened this issue Mar 14, 2011 · 11 comments
Closed

time of magma command fails inside function #10933

sagetrac-mariah mannequin opened this issue Mar 14, 2011 · 11 comments

Comments

@sagetrac-mariah
Copy link
Mannequin

sagetrac-mariah mannequin commented Mar 14, 2011

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: 

Component: interfaces

Author: Maarten Derickx

Reviewer: Martin Raum

Merged: sage-4.7.alpha5

Issue created by migration from https://trac.sagemath.org/ticket/10933

@sagetrac-mariah sagetrac-mariah mannequin added this to the sage-4.7 milestone Mar 14, 2011
@williamstein
Copy link
Contributor

comment:1

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

def foo(str):
     time gp(str)

foo('1')

@williamstein
Copy link
Contributor

comment:2

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...

@takluyver
Copy link

comment:3

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.

@koffie
Copy link

koffie commented Mar 22, 2011

Attachment: trac_10933.patch.gz

@koffie
Copy link

koffie commented Mar 22, 2011

comment:4

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

@sagetrac-mraum
Copy link
Mannequin

sagetrac-mraum mannequin commented Mar 22, 2011

comment:5

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

@sagetrac-mraum
Copy link
Mannequin

sagetrac-mraum mannequin commented Mar 22, 2011

Author: Maarten Derickx

@sagetrac-mraum
Copy link
Mannequin

sagetrac-mraum mannequin commented Mar 22, 2011

Reviewer: Martin Raum

@takluyver
Copy link

comment:6

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)

@takluyver
Copy link

comment:7

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

@jdemeyer
Copy link

Merged: sage-4.7.alpha5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants