Skip to content

Commit

Permalink
Trac #24286: py3: minor fixes to sage.repl.load and sage.repl.attach
Browse files Browse the repository at this point in the history
This will be one of many tickets I will be opening for miscellaneous
minor fixes to specific modules.

In some cases I'll keep these more "thematic" in which case they might
span multiple modules, but in other cases it is simpler to submit one
module at a time for minor fixes.

See the commit messages for more details about individual fixes.

URL: https://trac.sagemath.org/24286
Reported by: embray
Ticket author(s): Erik Bray
Reviewer(s): Jeroen Demeyer
  • Loading branch information
Release Manager authored and vbraun committed Feb 14, 2018
2 parents 5acceec + ede1402 commit ee52455
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/sage/repl/attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
....: traceback.print_exc()
Traceback (most recent call last):
...
exec(preparse_file(open(fpath).read()) + "\n", globals)
exec(preparse_file(f.read()) + "\n", globals)
File "<string>", line 3, in <module>
ValueError: third
sage: detach(src)
Expand Down
57 changes: 37 additions & 20 deletions src/sage/repl/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import os
import base64

from sage.cpython.string import str_to_bytes, bytes_to_str, FS_ENCODING

def is_loadable_filename(filename):
"""
Returns whether a file can be loaded into Sage. This checks only
Expand Down Expand Up @@ -96,33 +98,36 @@ def load(filename, globals, attach=False):
Note that ``.py`` files are *not* preparsed::
sage: t = tmp_filename(ext='.py')
sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: with open(t, 'w') as f:
....: _ = f.write("print(('hi', 2^3)); z = -2^7")
sage: z = 1
sage: sage.repl.load.load(t, globals()) # py2
hi 0
sage: z # py2
-1
sage: sage.repl.load.load(t, globals())
('hi', 1)
sage: z
-7
A ``.sage`` file *is* preparsed::
sage: t = tmp_filename(ext='.sage')
sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: with open(t, 'w') as f:
....: _ = f.write("print(('hi', 2^3)); z = -2^7")
sage: z = 1
sage: sage.repl.load.load(t, globals())
hi 2/3
('hi', 8)
sage: z
-2/7
-128
Cython files are *not* preparsed::
sage: t = tmp_filename(ext='.pyx')
sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: with open(t, 'w') as f:
....: _ = f.write("print(('hi', 2^3)); z = -2^7")
sage: z = 1
sage: sage.repl.load.load(t, globals())
Compiling ...
hi 0
('hi', 1)
sage: z
-1
-7
If the file isn't a Cython, Python, or a Sage file, a ValueError
is raised::
Expand All @@ -145,7 +150,8 @@ def load(filename, globals, attach=False):
We attach a file::
sage: t = tmp_filename(ext='.py')
sage: _ = open(t,'w').write("print 'hello world'")
sage: with open(t, 'w') as f:
....: _ = f.write("print('hello world')")
sage: sage.repl.load.load(t, globals(), attach=True)
hello world
sage: t in attached_files()
Expand All @@ -166,10 +172,12 @@ def load(filename, globals, attach=False):
sage: load_attach_path()
['.']
sage: t_dir = tmp_dir()
sage: fullpath = os.path.join(t_dir, 'test.py')
sage: _ = open(fullpath, 'w').write("print 37 * 3")
sage: fname = 'test.py'
sage: fullpath = os.path.join(t_dir, fname)
sage: with open(fullpath, 'w') as f:
....: _ = f.write("print(37 * 3)")
sage: load_attach_path(t_dir)
sage: attach('test.py')
sage: attach(fname)
111
sage: sage.repl.attach.reset(); reset_load_attach_path() # clean up
Expand All @@ -185,7 +193,9 @@ def load(filename, globals, attach=False):
Make sure that load handles filenames with spaces in the name or path::
sage: t = tmp_filename(ext=' b.sage'); _ = open(t,'w').write("print 2")
sage: t = tmp_filename(ext=' b.sage')
sage: with open(t, 'w') as f:
....: _ = f.write("print(2)")
sage: sage.repl.load.load(t, globals())
2
Expand Down Expand Up @@ -244,7 +254,8 @@ def load(filename, globals, attach=False):
# Preparse in memory only for speed.
if attach:
add_attached_file(fpath)
exec(preparse_file(open(fpath).read()) + "\n", globals)
with open(fpath) as f:
exec(preparse_file(f.read()) + "\n", globals)
elif ext == '.spyx' or ext == '.pyx':
if attach:
add_attached_file(fpath)
Expand Down Expand Up @@ -286,8 +297,14 @@ def load_wrap(filename, attach=False):
'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnB5"),globals(),True)'
sage: sage.repl.load.load_wrap('foo.sage')
'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnNhZ2U="),globals(),False)'
sage: sage.repl.load.base64.b64decode("Zm9vLnNhZ2U=")
'foo.sage'
sage: m = sage.repl.load.base64.b64decode("Zm9vLnNhZ2U=")
sage: m == b'foo.sage'
True
"""

# Note: On Python 3 b64encode only accepts bytes, and returns bytes (yet
# b64decode does accept str, but always returns bytes)
b64 = base64.b64encode(str_to_bytes(filename, FS_ENCODING,
"surrogateescape"))
return 'sage.repl.load.load(sage.repl.load.base64.b64decode("{}"),globals(),{})'.format(
base64.b64encode(filename), attach)
bytes_to_str(b64, 'ascii'), attach)

0 comments on commit ee52455

Please sign in to comment.