Skip to content

Commit f916d99

Browse files
Copilotedgarcosta
andcommitted
Add doctest for Path in load() and prevent URL fetch for Path objects
Co-authored-by: edgarcosta <[email protected]>
1 parent 682459a commit f916d99

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/sage/misc/persist.pyx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ def load(*filename, compress=True, verbose=True, **kwargs):
160160
sage: load(t) # needs numpy
161161
sage: hello # needs numpy
162162
<fortran ...>
163+
164+
Path objects are supported::
165+
166+
sage: from pathlib import Path
167+
sage: import tempfile
168+
sage: with tempfile.TemporaryDirectory() as d:
169+
....: p = Path(d) / "test_path"
170+
....: save(1, p)
171+
....: load(p)
172+
1
163173
"""
164174
import sage.repl.load
165175
if len(filename) != 1:
@@ -172,15 +182,17 @@ def load(*filename, compress=True, verbose=True, **kwargs):
172182
return
173183

174184
filename = filename[0]
175-
filename = os.fspath(filename) # Convert path-like objects to strings
185+
is_path_like = hasattr(filename, '__fspath__')
186+
filename = os.fspath(filename)
176187

177188
if sage.repl.load.is_loadable_filename(filename):
178189
sage.repl.load.load(filename, globals())
179190
return
180191

181192
# Check if filename starts with "http://" or "https://"
193+
# (but skip this check if the original input was a Path object)
182194
lower = filename.lower()
183-
if lower.startswith("http://") or lower.startswith("https://"):
195+
if not is_path_like and (lower.startswith("http://") or lower.startswith("https://")):
184196
from sage.misc.remote_file import get_remote_file
185197
filename = get_remote_file(filename, verbose=verbose)
186198
tmpfile_flag = True

0 commit comments

Comments
 (0)