-
Notifications
You must be signed in to change notification settings - Fork 0
Fix save/load functions to accept pathlib.Path objects #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
@copilot, follow the advice from sagemath#41037 (comment) to use context manager ("with ..") will both (a) automatically clean up the directory when you're done, and (b) give you the name to begin with. For example, this should work: |
Updated the doctest to use a context manager as suggested. The new format is more concise and automatically handles cleanup. Changes in commit 2a8e6f1. |
src/sage/misc/persist.pyx
Outdated
| """ | ||
| Append the .sobj extension to a filename if it doesn't already have it. | ||
| """ | ||
| s = os.fspath(s) # Convert path-like objects to strings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This line is useless, remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the os.fspath() call from _normalize_filename and moved it to _base_save() instead. This keeps the conversion at entry points only, making _normalize_filename a simpler internal helper that works with strings. Changes in commit 90ca892.
src/sage/misc/persist.pyx
Outdated
| return | ||
|
|
||
| filename = filename[0] | ||
| filename = os.fspath(filename) # Convert path-like objects to strings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot remove comment, add doctest, and make sure if filename is originally a Path then the branch below with fetching URL is never executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the comment, added a doctest for Path objects in load(), and ensured Path objects skip the URL fetching branch by checking for __fspath__ before conversion. Changes in commit 6b2b9e3.
f916d99 to
73d5d53
Compare
73d5d53 to
8885ac3
Compare
Fix save() and load() functions to accept pathlib.Path objects
The issue is that save() cannot take a Path object as it expects a string. The error occurs because Path objects don't support string operations like slicing (s[-5:]) or methods like .endswith() and .lower().
Solution:
Added
os.fspath()calls to convert path-like objects (including Path objects) to strings at entry points before any string operations are performed. This is the standard Python way to handle path-like objects and is compatible with both strings and Path objects.Changes Made:
save(): Addedos.fspath(filename)at the beginning to handle the.endswith('.sobj')callload(): Addedos.fspath(filename)after extracting single filename to handle the.lower()call, with logic to skip URL fetching for Path objects_base_save(): Addedos.fspath(filename)at the beginning to handle Path objects from SageObject.save()save()andload()demonstrating Path object support using context managerThe conversion is now done at all entry points (save, load, _base_save) rather than in the internal helper function (_normalize_filename), making the code cleaner and more maintainable. Additionally, Path objects are explicitly prevented from triggering URL fetching logic.
Testing:
The fix is minimal, follows Python best practices, and maintains backward compatibility with existing string-based code.
Original prompt
Fixes #1
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.