# HG changeset patch
# User Mike Hansen <mhansen@gmail.com>
# Date 1232360477 28800
# Node ID bc35da7a04ebbed6ad516372ab159b4e9f4f8cb1
# Parent 68bc7d11954e0b49a97750c6b2bfd9b623efcc0b
Fixed #4975: Sage chokes on utf-8 encoded files
diff -r 68bc7d11954e -r bc35da7a04eb sage-preparse
|
a
|
b
|
|
| 17 | 17 | files = sys.argv[2:] |
| 18 | 18 | |
| 19 | 19 | # There must be at least 1 file or we display an error/usage message |
| 20 | | # and exit. |
| | 20 | # and exit |
| 21 | 21 | if len(files) == 0: |
| 22 | 22 | print "Usage: %s <file1.sage> <file2.sage>..."%sys.argv[0] |
| 23 | 23 | print "Creates files file1.py, file2.py ... that are the Sage" |
| … |
… |
|
| 78 | 78 | #if os.path.exists(fname) and os.path.getmtime(fname) >= os.path.getmtime(f): |
| 79 | 79 | # return |
| 80 | 80 | |
| 81 | | # Finally open the file and preparse it. |
| | 81 | # Finally open the file |
| 82 | 82 | F = open(f).read() |
| | 83 | |
| | 84 | #Check to see if a coding is specified in the .sage file. |
| | 85 | #If it is, then we want to copy it over to the new file |
| | 86 | #and not include it in the preprocessing |
| | 87 | if F.startswith('# -*- coding:'): |
| | 88 | end = F.find('\n') |
| | 89 | coding = F[:end+1] if end != -1 else F |
| | 90 | F = F[len(coding):] |
| | 91 | else: |
| | 92 | coding = '' |
| | 93 | |
| | 94 | #Preparse it |
| 83 | 95 | from sage.misc.preparser import preparse_file |
| 84 | 96 | G = preparse_file(F, magic=False, do_time=True, ignore_prompts=False) |
| 85 | 97 | |
| 86 | 98 | # Check for load/attach commands. |
| 87 | 99 | G = do_load_and_attach(G, f, files_before) |
| 88 | 100 | |
| | 101 | |
| 89 | 102 | # Put the Sage library include along with a autogen message in the file. |
| 90 | 103 | # It is ** critical ** that we put this after the mdoule docstring, since |
| 91 | 104 | # otherwise the module docstring will disappear. |
| 92 | | insert = '%s%s.\nfrom sage.all_cmdline import * # import sage library\n'%(AUTOGEN_MSG, f) |
| | 105 | insert = '%s%s%s.\nfrom sage.all_cmdline import * # import sage library\n'%(coding, AUTOGEN_MSG, f) |
| 93 | 106 | i = find_position_right_after_module_docstring(G) |
| 94 | 107 | G = G[:i] + insert + G[i:] |
| 95 | 108 | |