Skip to content

Populate correct meta information in load_file, deprecate taking file objects #310

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

tmadlener
Copy link
Contributor

BEGINRELEASENOTES

  • Make sure that load_file sets the correct __file__ and __spec__ information for files that are loaded with it
  • Restore the output of the filename in case loading doesn't work
  • Deprecate passing a file handle to load_file and always pass a path-like object

ENDRELEASENOTES

Setting the correct __file__ is necessary as that is quite frequently used for some special casing, or also to add things to sys.path relative to the path of the loaded file, e.g. here

Putting out the correct file name was lost in #213

Finally it looks like a simple change in k4run makes it trivial to pass in path-like objects rather than open file handles (which we didn't bother to close before).

@tmadlener tmadlener force-pushed the fix-file-dunder-load-file branch 2 times, most recently from 78682e2 to 509295d Compare April 29, 2025 14:40
@tmadlener
Copy link
Contributor Author

closing and opening again to check if that triggers CI

@tmadlener tmadlener closed this Apr 30, 2025
@tmadlener tmadlener reopened this Apr 30, 2025
@tmadlener tmadlener force-pushed the fix-file-dunder-load-file branch from 421636d to d23279d Compare May 19, 2025 14:40
@tmadlener tmadlener force-pushed the fix-file-dunder-load-file branch 2 times, most recently from 74b8360 to 3037db5 Compare June 6, 2025 11:25
Comment on lines +105 to +108
warnings.warn(
"load_file will remove support for handling TextIOWrapper. Please switch to pasing os.PathLike",
FutureWarning,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is used anywhere. It might be possible to remove this entirely and clean the implementation up a bit more.

@jmcarcell jmcarcell force-pushed the fix-file-dunder-load-file branch from 3037db5 to 0d371f9 Compare June 12, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant