Skip to content
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

Test fail on windows due to NamedTemporaryFile #155

Closed
Cube707 opened this issue Jan 25, 2025 · 1 comment · Fixed by #158
Closed

Test fail on windows due to NamedTemporaryFile #155

Cube707 opened this issue Jan 25, 2025 · 1 comment · Fixed by #158

Comments

@Cube707
Copy link
Contributor

Cube707 commented Jan 25, 2025

Its a known limitation of the tempfile module that on windows-systems the same file can not be opend multiple times.

See docs: https://docs.python.org/3.9/library/tempfile.html#tempfile.NamedTemporaryFile

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

This lead to the following test-code failing with a permission error, as the try to open the file multiple times.

def test_write():
with NamedTemporaryFile(suffix=".xml", encoding="utf-8", mode="rt") as tmpfile:
do_test_write(tmpfile.name, tmpfile.read)
def test_write_file_obj():
with NamedTemporaryFile(suffix=".xml", encoding="utf-8", mode="rt") as tmpfile:
with open(tmpfile.name, "wb") as file_obj:
def read():
file_obj.close()
return tmpfile.read()
do_test_write(file_obj, read)
def test_write_filelike_obj():
# a file-like object providing a write method only
class FileObject:
content = BytesIO()
def write(self, buf):
return self.content.write(buf)
def _read(self):
self.content.seek(0)
return self.content.read().decode("utf-8")
filelike_obj = FileObject()
do_test_write(filelike_obj, filelike_obj._read)

@Cube707
Copy link
Contributor Author

Cube707 commented Jan 25, 2025

CC: @EnricoMi

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 a pull request may close this issue.

1 participant