diff -r f45bbd14ad23 sage/misc/interpreter.py
a
|
b
|
def do_prefilter_paste(line, continuatio |
185 | 185 | if F.endswith('.py'): |
186 | 186 | _ip.magic('run -i "%s"'%F) |
187 | 187 | elif F.endswith('.sage'): |
188 | | _ip.magic('run -i "%s"'%process_file(F)) |
| 188 | _ip.magic('run -i "%s"'%preparse_file_named(F)) |
189 | 189 | elif F.endswith('.spyx') or F.endswith('.pyx'): |
190 | 190 | X = load_cython(F) |
191 | 191 | __IPYTHON__.push(X) |
… |
… |
def do_prefilter_paste(line, continuatio |
276 | 276 | raise ImportError, "Error loading '%s'"%name |
277 | 277 | elif name.endswith('.sage'): |
278 | 278 | try: |
279 | | line = '%run -i "' + process_file(name) + '"' |
| 279 | line = '%run -i "' + preparse_file_named(name) + '"' |
280 | 280 | except IOError, s: |
281 | 281 | print s |
282 | 282 | raise ImportError, "Error loading '%s'"%name |
… |
… |
def do_prefilter_paste(line, continuatio |
330 | 330 | raise ImportError, "File '%s' not found."%name |
331 | 331 | elif name.endswith('.sage'): |
332 | 332 | try: |
333 | | line = '%run -i "' + process_file(name) + '"' |
| 333 | line = '%run -i "' + preparse_file_named(name) + '"' |
334 | 334 | attached[name] = os.path.getmtime(name) |
335 | 335 | except IOError, OSError: |
336 | 336 | raise ImportError, "File '%s' not found."%name |
… |
… |
def load_cython(name): |
359 | 359 | import sys |
360 | 360 | sys.path.append(dir) |
361 | 361 | return 'from %s import *'%mod |
362 | | |
363 | | def process_file(name): |
| 362 | |
| 363 | def preparse_file_named_to_stream(name, out): |
| 364 | r""" |
| 365 | Preparse file named \code{name} (presumably a .sage file), outputting to |
| 366 | stream \code{out}. |
| 367 | """ |
| 368 | name = os.path.abspath(name) |
| 369 | dir, _ = os.path.split(name) |
364 | 370 | cur = os.path.abspath(os.curdir) |
365 | | dir, name = os.path.split(os.path.abspath(name)) |
366 | | name2 = "%s/%s.py"%(dir,name[:name.find('.')]) |
367 | 371 | os.chdir(dir) |
368 | 372 | contents = open(name).read() |
369 | 373 | parsed = preparse_file(contents, attached, do_time=True) |
370 | 374 | os.chdir(cur) |
371 | | W = open(name2,'w') |
372 | | W.write('#'*70+'\n') |
373 | | W.write('# This file was *autogenerated* from the file %s.\n'%name) |
374 | | W.write('#'*70+'\n') |
375 | | W.write(parsed) |
376 | | W.close() |
377 | | return name2 |
378 | | |
| 375 | out.write('#'*70+'\n') |
| 376 | out.write('# This file was *autogenerated* from the file %s.\n' % name) |
| 377 | out.write('#'*70+'\n') |
| 378 | out.write(parsed) |
| 379 | |
| 380 | def preparse_file_named(name): |
| 381 | r""" |
| 382 | Preparse file named \code{name} (presumably a .sage file), outputting to a |
| 383 | temporary file. Returns name of temporary file. |
| 384 | """ |
| 385 | import sage.misc.misc |
| 386 | name = os.path.abspath(name) |
| 387 | tmpfilename = os.path.abspath(sage.misc.misc.tmp_filename(name) + ".py") |
| 388 | out = open(tmpfilename,'w') |
| 389 | preparse_file_named_to_stream(name, out) |
| 390 | out.close() |
| 391 | return tmpfilename |
379 | 392 | |
380 | 393 | def sage_prefilter(self, block, continuation): |
381 | 394 | """ |
diff -r f45bbd14ad23 sage/misc/misc.py
a
|
b
|
__tmp_n = 0 |
1366 | 1366 | __tmp_n = 0 |
1367 | 1367 | |
1368 | 1368 | def tmp_filename(name='tmp'): |
1369 | | name = str(name) |
| 1369 | name = list(str(name)) |
| 1370 | for i in range(len(name)): |
| 1371 | # protect against paths with slashes, colons, etc |
| 1372 | if not (name[i].isalpha() or name[i].isdigit()): |
| 1373 | name[i] = '_' |
| 1374 | name = ''.join(name) |
| 1375 | |
1370 | 1376 | global __tmp_n |
1371 | 1377 | while True: |
1372 | 1378 | tmp = "%s/%s_%s"%(SAGE_TMP, name, __tmp_n) |