Ticket #6305: 12063.patch

File 12063.patch, 2.9 KB (added by rkirov, 4 years ago)
  • sage/misc/preparser.py

    # HG changeset patch
    # User rado@rado-tablet
    # Date 1245120218 18000
    # Node ID 37f4bf1ac22bb1c77b5d1b48f8e1529ac1e80253
    # Parent  891da50821bd920d08245dfb718a0f99bda908cc
    
    fix for preparser issue with recurssive loading of .sage files
    
    diff -r 891da50821bd -r 37f4bf1ac22b sage/misc/preparser.py
    a b  
    906906    return line 
    907907     
    908908 
     909def literals_function(contents): 
     910    contents, literals, state = strip_string_literals(contents) 
     911    contents, nums = extract_numeric_literals(contents) 
     912    contents = contents % literals 
     913    if nums: 
     914        # Stick the assignments at the top, trying not to shift the lines down.  
     915        ix = contents.find('\n') 
     916        if ix == -1: ix = len(contents) 
     917        if not re.match(r"^ *(#.*)?$", contents[:ix]): 
     918            contents = "\n"+contents 
     919        assignments = ["%s = %s" % x for x in nums.items()] 
     920        # the preparser recurses on semicolons, so we only attempt to preserve line numbers if there are a few 
     921        if len(assignments) < 500: 
     922            contents = "; ".join(assignments) + contents 
     923        else: 
     924            contents = "\n".join(assignments) + "\n\n" + contents 
     925    return contents 
     926 
     927 
    909928###################################################### 
    910929## Apply the preparser to an entire file 
    911930###################################################### 
     
    940959        numeric_literals = False 
    941960     
    942961    if numeric_literals: 
    943         contents, literals, state = strip_string_literals(contents) 
    944         contents, nums = extract_numeric_literals(contents) 
    945         contents = contents % literals 
    946         if nums: 
    947             # Stick the assignments at the top, trying not to shift the lines down.  
    948             ix = contents.find('\n') 
    949             if ix == -1: ix = len(contents) 
    950             if not re.match(r"^ *(#.*)?$", contents[:ix]): 
    951                 contents = "\n"+contents 
    952             assignments = ["%s = %s" % x for x in nums.items()] 
    953             # the preparser recurses on semicolons, so we only attempt to preserve line numbers if there are a few 
    954             if len(assignments) < 500: 
    955                 contents = "; ".join(assignments) + contents 
    956             else: 
    957                 contents = "\n".join(assignments) + "\n\n" + contents 
     962        contents=literals_function(contents) 
    958963 
    959964    F = [] 
    960965    A = contents.splitlines() 
     
    9991004                    i += 1 
    10001005                    continue 
    10011006                else: 
    1002                     A = A[:i] + G.readlines() + A[i+1:] 
     1007                    if numeric_literals: 
     1008                        new_content = literals_function(G.read()) 
     1009                    else: 
     1010                        new_content = G.read() 
     1011                    A = A[:i] + new_content.splitlines() + A[i+1:] 
    10031012                    continue 
    10041013            elif name_load[-5:] == '.spyx': 
    10051014                import interpreter