-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
TST: fix PermissionError on Windows #5278
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
@@ -24,6 +24,7 @@ def test_font_priority(): | |||
|
|||
def test_json_serialization(): | |||
with tempfile.NamedTemporaryFile() as temp: | |||
temp.close() |
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.
From docs:
If delete is true (the default), the file is deleted as soon as it is closed.
That sounds like the temporary file would be cleaned up here. Then the write in the next line will be to a file that isn't cleaned up. Or does the context manager try to delete the file again even though it's been closed already?
Since json_load
/json_dump
are new, maybe they should be modified to take a file object instead?
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.
+1 on modifying json_load/json_dump.
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 really care how to resolve this, but we should confirm that this doesn't leave a file hanging around on disk, which is what I think this change does.
json_dump(fontManager, temp.name) | ||
copy = json_load(temp.name) | ||
if sys.platform == 'win32': |
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.
This should be in a try
/finally
so it always runs.
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.
sigh... we are making this overly complicated.
try:
temp = tempfile.NamedTemporaryFile(delete=False)
temp.close()
json_dump(fontManager, temp.name)
copy = json_load(temp.name)
finally:
if os.path.exists(temp.name):
os.remove(temp.name)
@cgohlke: Did you close this by accident? |
Included a version of this PR in #5604 |
On Windows, a open NamedTemporaryFile can not be used to open the file a second time.
Fixes one test error: