Ticket #10747: trac_10747-preparse_calculus.2.patch

File trac_10747-preparse_calculus.2.patch, 1.7 KB (added by burcin, 2 years ago)
  • sage/misc/preparser.py

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1308382047 -7200
    # Node ID 8e64eb3b46729151051c9c5f08cc596ca002d3c8
    # Parent  42b1e66d1465076c5a0a6baabcf6fc7b05434f92
    trac #10747: make preparse_calculus() ignore argument names with _sage_const
    
    Extracting numeric literals in the notebook from callable symbolic expression
    constructions like f(2)=3 causes the argument to be replaced with
    _sage_const_*. This is a valid identifier, so we create a symbolic variable
    with that name and construct the function. This patch adds a check in
    preparse_calculus to rule out variable names that start with
    numeric_literal_prefix.
    
    diff --git a/sage/misc/preparser.py b/sage/misc/preparser.py
    a b  
    798798    #                                   f         (  vars  )   =      expr 
    799799    for m in re.finditer(r";(\s*)([a-zA-Z_]\w*) *\(([^()]+)\) *= *([^;#=][^;#]*)", code): 
    800800        ident, func, vars, expr = m.groups() 
    801         vars = ','.join(v.strip() for v in vars.split(',')) 
     801        stripped_vars = [v.strip() for v in vars.split(',')] 
     802        # if the variable name starts with numeric_literal_prefix 
     803        # the argument name for the symbolic expression is a numeric literal 
     804        # such as f(2)=5 
     805        if any(n.startswith(numeric_literal_prefix) for n in stripped_vars): 
     806            raise ValueError("Argument names should be valid python identifiers.") 
     807        vars = ','.join(stripped_vars) 
     808 
    802809        new_code.append(code[last_end:m.start()]) 
    803810        new_code.append(';%s__tmp__=var("%s"); %s = symbolic_expression(%s).function(%s)' % (ident, vars, func, expr, vars)) 
    804811        last_end = m.end()