-
-
Notifications
You must be signed in to change notification settings - Fork 631
Use TemporaryFile for test code compilation #39646
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
Conversation
Documentation preview for this PR (built with commit 30a213f; changes) is ready! 🎉 |
cython_import(pyx.name, verbose=-1) | ||
with TemporaryFile(suffix='.pyx') as pyx: | ||
pyx.write(self.test_code) | ||
cython_import(pyx.name, verbose=-1) |
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.
I don't think this will work on POSIX systems. The docs say,
The returned object is a true file object on POSIX platforms. On other platforms, it is a file-like object whose file attribute is the underlying true file object.
What it does on my machine:
>>> import tempfile
>>> with tempfile.TemporaryFile() as f:
... print(f.name)
...
3
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.
Right, it should have used NamedTemporaryFile
. Strange thing is that on CI the Cython features bliss
and sage.misc.cython
were nevertheless correctly recognized.
sage: z | ||
sage: from tempfile import NamedTemporaryFile | ||
sage: context = { "z": 1} | ||
sage: with NamedTemporaryFile(mode='w', suffix='.py') as file: |
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.
You are the only one able to test it, but I would err on the side of caution and clean up the file explicitly here (i.e. set delete=False
, and unlink it when done). The docs say that reopening an open file by name is tricky on Windows:
Opening the temporary file again by its name while it is still open works as follows... On Windows, make sure that at least one of the following conditions are fulfilled:
- delete is false
- additional open shares delete access (e.g. by calling os.open() with the flag O_TEMPORARY)
- delete is true but delete_on_close is false. Note, that in this case the additional opens that do not share delete access (e.g. created via builtin open()) must be closed before exiting the context manager, else the os.unlink() call on context manager exit will fail with a PermissionError.
So ideally what we want to do is set delete_on_close=False
... but the delete_on_close
parameter was only added in python-3.12.
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.
Thanks, that's a good point.
So ideally what we want to do is set delete_on_close=False... but the delete_on_close parameter was only added in python-3.12.
We can drop support for 3.11 this autumn and I would prefer to wait until then with improving windows support.
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.
dropping 3.11 would be nice as well to enable the use of nanobind (the successor of pybind11)
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.
Sure, but that would have to wait a few more months if we want to adhere to spec 0.
could you rebase this? |
76ebe71
to
30a213f
Compare
Sure, done! |
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.
lgtm
Thanks! |
Use
tempfile.TemporaryFile
instead of Sage's own temporary file handler for the compilation of the test cython file.This fixes errors during test collection with pytest:
https://github.com/sagemath/sage/actions/runs/13718450041/job/38368348037?pr=39641#step:13:2369
📝 Checklist
⌛ Dependencies