#11542: Fix preparse_file to prevent constants from being assigned to
------------------------------+---------------------------------------------
Reporter: nbruin | Owner: was
Type: defect | Status: new
Priority: major | Milestone: sage-4.7.2
Component: user interface | Keywords:
Work_issues: | Upstream: N/A
Reviewer: | Author:
Merged: | Dependencies:
------------------------------+---------------------------------------------
Comment(by nbruin):
Since the code produced by the normal {{{preparse}}} does not suffer from
the problem (it transforms the ungrammatical constructs into other
ungrammatical ones), one could use its output to test for errors:
{{{
sage: from ast import parse
sage: def fixed_preparse_file(s):
... t=preparse(s)
... try:
... parse(t)
... return sage.misc.preparser.preparse_file(s)
... except:
... return t
}}}
some timings on an ungrammatical construct
{{{
sage: timeit('fixed_preparse_file("[1^2 for j in range(10) for 1 in
[1..2*1]]")')
625 loops, best of 3: 309 µs per loop
sage: timeit('sage.misc.preparser.preparse_file("[1^2 for j in range(10)
for 1 in [1..2*1]]")')
625 loops, best of 3: 477 µs per loop
sage: timeit('preparse("[1^2 for j in range(10) for 1 in [1..2*1]]")')
625 loops, best of 3: 231 µs per loop
}}}
On a grammatical one
{{{
sage: timeit('fixed_preparse_file("[1^2 for j in range(10) if 1 in
[1..2*1]]")')
625 loops, best of 3: 840 µs per loop
sage: timeit('sage.misc.preparser.preparse_file("[1^2 for j in range(10)
if 1 in [1..2*1]]")')
625 loops, best of 3: 476 µs per loop
}}}
And, for reference, the output generated
{{{
sage: fixed_preparse_file("[1^2 for j in range(10) for 1 in [1..2*1]]")
'[Integer(1)**Integer(2) for j in range(Integer(10)) for Integer(1) in
(ellipsis_range(Integer(1),Ellipsis,Integer(2)*Integer(1)))]'
sage: fixed_preparse_file("[1^2 for j in range(10) if 1 in [1..2*1]]")
'_sage_const_2 = Integer(2); _sage_const_1 = Integer(1); _sage_const_10 =
Integer(10)\n[_sage_const_1 **_sage_const_2 for j in range(_sage_const_10
) if _sage_const_1 in (ellipsis_range(_sage_const_1
,Ellipsis,_sage_const_2 *_sage_const_1 ))]'
}}}
This code simply tries to parse the result of preparse and if that leads
to an error, it returns that result (so that the error can be raised in
the normal way). If it does parse, it returns the result of preparse_file
instead. As you can see from the timings, the preparsing (and the double
preparsing doubly so) is more expensive than the parsing.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11542#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.