Skip to content

Commit

Permalink
Use NamedTemporaryFile for improved file handling in REPL examples
Browse files Browse the repository at this point in the history
And uses a `context` object instead of `globals`.
  • Loading branch information
tobiasdiez authored Mar 8, 2025
1 parent c28eb93 commit de21055
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/sage/repl/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,42 +119,44 @@ def load(filename, globals, attach=False):
Note that ``.py`` files are *not* preparsed::
sage: t = tmp_filename(ext='.py')
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', 1)
sage: z
sage: from tempfile import NamedTemporaryFile
sage: context = { "z": 1}
sage: with NamedTemporaryFile(mode='w', suffix='.py') as file:
....: _ = file.write("print(('hi', 2^3, z)); z = -2^7")
....: _ = file.seek(0)
....: sage.repl.load.load(file.name, context)
('hi', 1, 1)
sage: context["z"]
-7
A ``.sage`` file *is* preparsed::
sage: t = tmp_filename(ext='.sage')
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', 8)
sage: z
sage: context = { "z": 1, "Integer": Integer }
sage: with NamedTemporaryFile(mode='w', suffix='.sage') as file:
....: _ = file.write("print(('hi', 2^3, z)); z = -2^7")
....: _ = file.seek(0)
....: sage.repl.load.load(file.name, context)
('hi', 8, 1)
sage: context["z"]
-128
Cython files are *not* preparsed::
sage: t = tmp_filename(ext='.pyx')
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()) # needs sage.misc.cython
sage: context = { "z": 1 }
sage: with NamedTemporaryFile(mode='w', suffix='.pyx') as file:
....: _ = file.write("print(('hi', 2^3)); z = -2^7")
....: _ = file.seek(0)
....: sage.repl.load.load(file.name, context) # needs sage.misc.cython
Compiling ...
('hi', 1)
sage: z
sage: context["z"]
-7
If the file is not a Cython, Python, or Sage file, a :exc:`ValueError`
is raised::
sage: sage.repl.load.load(tmp_filename(ext='.foo'), globals())
sage: with NamedTemporaryFile(mode='w', suffix='.foo') as file:
....: sage.repl.load.load(file.name, {})
Traceback (most recent call last):
...
ValueError: unknown file extension '.foo' for load or attach (supported extensions: .py, .pyx, .sage, .spyx, .f, .f90, .m)
Expand All @@ -172,12 +174,12 @@ def load(filename, globals, attach=False):
We attach a file (note that :func:`~sage.repl.attach.attach`
is equivalent, but available at the global scope by default)::
sage: t = tmp_filename(ext='.py')
sage: with open(t, 'w') as f:
....: _ = f.write("print('hello world')")
sage: sage.repl.load.load(t, globals(), attach=True)
sage: with NamedTemporaryFile(mode='w', suffix='.py') as file:
....: _ = file.write("print('hello world')")
....: _ = file.seek(0)
....: sage.repl.load.load(file.name, {}, attach=True)
hello world
sage: t in attached_files()
sage: file.name in attached_files()
True
You cannot attach remote URLs (yet)::
Expand Down

0 comments on commit de21055

Please sign in to comment.