Ticket #3484: trac3484-sage_eval-review-response.patch

File trac3484-sage_eval-review-response.patch, 2.0 kB (added by cwitty, 5 months ago)
  • a/sage/misc/sage_eval.py

    old new  
    110110        in Python, and has the wrong precedence. 
    111111 
    112112    Here you can see eval simply will not work but \code{sage_eval} will. 
     113 
     114    TESTS: 
     115 
     116    We get a nice minimal error message for syntax errors, that still 
     117    points to the location of the error (in the input string): 
     118 
     119        sage: sage_eval('RR(22/7]') 
     120        Traceback (most recent call last): 
     121        ... 
     122         File "<string>", line 1 
     123            RR(Integer(22)/Integer(7)] 
     124                                     ^ 
     125        SyntaxError: unexpected EOF while parsing 
     126 
     127        sage: sage_eval('None', cmds='$x = $y[3] # Does Perl syntax work?') 
     128        Traceback (most recent call last): 
     129        ... 
     130         File "<string>", line 1 
     131            $x = $y[Integer(3)] # Does Perl syntax work? 
     132            ^ 
     133        SyntaxError: invalid syntax         
    113134    """ 
    114135    if isinstance(source, (list, tuple)): 
    115136        cmds = source[0] 
     
    131152    else: 
    132153        if preparse: 
    133154            source = preparser.preparse(source) 
    134     try: 
    135         if len(cmds): 
    136             exec cmd_seq in sage.all.__dict__, locals 
    137             return locals['_sage_eval_returnval_'] 
    138         else: 
    139             return eval(source, sage.all.__dict__, locals) 
    140     except SyntaxError, msg: 
    141         raise SyntaxError, "%s\nError using SAGE to evaluate '%s'"%(msg, cmd_seq if len(cmds) else source) 
     155 
     156    if len(cmds): 
     157        exec cmd_seq in sage.all.__dict__, locals 
     158        return locals['_sage_eval_returnval_'] 
     159    else: 
     160        return eval(source, sage.all.__dict__, locals) 
    142161     
    143162         
    144163