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

TST : unhandle exceptions raised because of Non closed temporary file streams #2394

Closed
pubpub-zz opened this issue Jan 6, 2024 · 1 comment · Fixed by #2395 or #2396
Closed

TST : unhandle exceptions raised because of Non closed temporary file streams #2394

pubpub-zz opened this issue Jan 6, 2024 · 1 comment · Fixed by #2395 or #2396

Comments

@pubpub-zz
Copy link
Collaborator

Environment

side effect of #2379

$ python -m platform
Windows-10-10.0.22621-SP0
python 3.10

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.17.4, crypt_provider=('cryptography', '41.0.1'), PIL=10.1.0

Code + PDF

pytest -k test_writer_operations_by_traditional_usage

Traceback

This is the complete traceback I see:

E               pytest.PytestUnraisableExceptionWarning: Exception ignored in: <_io.FileIO [closed]>
E
E               Traceback (most recent call last):
E                 File "E:\PyPDF4.git\PyPDF2\tests\test_writer.py", line 227, in test_writer_operations_by_traditional_usage
E                   write_data_here = convert(NamedTemporaryFile(suffix=".pdf", delete=needs_cleanup).name)
E               ResourceWarning: unclosed file <_io.BufferedRandom name=15>

C:\Python310\lib\site-packages\_pytest\unraisableexception.py:78: PytestUnraisableExceptionWarning```


## Analysis

the temporary file created is not closed : it should be 
@stefan6419846
Copy link
Collaborator

Thanks for the report. Could you please test with

    if callable(convert):
        with NamedTemporaryFile(suffix=".pdf", delete=False) as temporary:
            write_data_here = convert(temporary.name)
    else:
        write_data_here = convert

instead? If this works, I will submit a corresponding PR to rework/fix this.

pubpub-zz added a commit to pubpub-zz/pypdf that referenced this issue Jan 6, 2024
Issue due to unclosure of temporary file
closes py-pdf#2394
MartinThoma pushed a commit that referenced this issue Jan 6, 2024
Issue due to unclosure of temporary file

Closes #2394
MartinThoma pushed a commit that referenced this issue Jan 6, 2024
Improvement for #2379 to use a common method and appropriately close the temporary files.

Fixes #2394
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants