Hello,

On Tue, Jan 26, 2010 at 6:07 AM, msafiri <reich...@ifd.mavt.ethz.ch> wrote:

> Hello everyone,
> ticket #7207 deals with "from __future__" imports resulting in syntax
> errors in the SAGE notebook. The same is true when one tries to use
> cython from the command line e.g. via cython_create_local_so(). This
> is because the pyx_preparse() function in cython.py adds include
> statements to the beginning of the source code.
> This is probably not the correct platform to discuss bug-fixes, but
> this short piece of code solves the aforementioned problem (at least
> for my needs):
>
> # ------------ in cython.py:
>
> def parse_future_statements(s):
>    """Looks for 'from __future__ import *' statements and moves them
> to the
>    beginning of the string.
>    """
>
>    i = s.find('from __future__')
>    if i != -1:
>        j = s.find('\n', i)
>        s = s[i:j+1] + s[0:i] + s[j+1:]
>
>    return s
>
> # ---------------------------------
>
> The function "pyx_preparse()" in cython.py needs to call the above
> function just before it returns:
> s = parse_future_statements(s)
>
> Best,
> Johannes
>
> PS: Also, I've noticed that the source files to be "cythoned" really
> need to have .spxy extention. When using a .pyx file instead, the
> function "pyx_preparse()" will append the include directives in that
> very file (also, regardless of whether they have already been appended
> in a previous run).
>
> --
> 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<sage-devel%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

My original patch was something like that, but William Stein pointed out
that it is easily tricked:

"""
from __future__ import division"""
[...]

will activate the code as well, which will result in a syntax error. I used
the AST parser to make sure no such thing happens, but I'm certain it is
possible to just use a less naive search.

-- 
Tim Joseph Dumol <tim (at) timdumol (dot) com>
http://timdumol.com

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

Reply via email to