# 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
|
|
| 906 | 906 | return line |
| 907 | 907 | |
| 908 | 908 | |
| | 909 | def 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 | |
| 909 | 928 | ###################################################### |
| 910 | 929 | ## Apply the preparser to an entire file |
| 911 | 930 | ###################################################### |
| … |
… |
|
| 940 | 959 | numeric_literals = False |
| 941 | 960 | |
| 942 | 961 | 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) |
| 958 | 963 | |
| 959 | 964 | F = [] |
| 960 | 965 | A = contents.splitlines() |
| … |
… |
|
| 999 | 1004 | i += 1 |
| 1000 | 1005 | continue |
| 1001 | 1006 | 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:] |
| 1003 | 1012 | continue |
| 1004 | 1013 | elif name_load[-5:] == '.spyx': |
| 1005 | 1014 | import interpreter |