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

ResourceWarning: unclosed file inside offset_rendering context manager #1356

Open
stenci opened this issue Feb 4, 2025 · 2 comments
Open

Comments

@stenci
Copy link

stenci commented Feb 4, 2025

I have noticed a ResourceWarning: unclosed file warning shown when working inside a with self.offset_rendering() as dummy: context manager. (I use the context manager to know whether what I am adding will fit in the page or not.)

I add the fonts in the class constructor, something like this:

class PdfPackingList(FPDF):
    def __init__(self):
        super().__init__()
        [...]
        self.add_font('roboto', style='', fname=r'C:\<path-name>\Roboto-Regular.ttf')

And the full warning is:

<input>:1: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\<path-name>\\Roboto-Regular.ttf'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

I am using fpdf2 version 2.7.7.

Please let me know if this is a known issue or it is not an issue, or if you are interested in further digging or in a small reproducible example.

@stenci stenci added the question label Feb 4, 2025
@Lucas-C
Copy link
Member

Lucas-C commented Feb 5, 2025

Hi @stenci

Thank you for the issue report 👍

Would you mind providing a minimal bug-reproduction code snippet, please?

Because without knowing how precisely you use offset_rendering() and the fonts involved, it would be very difficult for us maintainers to figure the origin of the problem 🙂

@stenci
Copy link
Author

stenci commented Feb 5, 2025

Hi @Lucas-C

I tried to reproduce the problem, and I found out that it only reproduces while running in a unittest TestCase. Moving the same test function outside of the TestCase class doesn't show the warning.

If you are still interested, I have attached two scripts. They use the same PdfPackingList(FPDF) class, but one with and one without the unit test. The one with the unittest shows the warnings at pdf.output('test.pdf').

This is the output I see:

add_page2(T1)
add_table(
['Crate #', 'Size', 123.123, 'PckGrp', 'Panels'],
[20, 30, 20, 20, -1],
[['R1-C2', '55 x 125 x 35', 123.123, 'ABCDE', '12_<(123)>_ 12_<(123)>_ 12_<(123)>_ 123456_<(123)>_ '], ['R1-C2', '55 x 125 x 35', '1550', 'ABCDa a ds a E', '12_<(123)>_ 12_<(123)>_ 12_<(123)>_ 123456_<(123)>_ ']]
)
C:\Users\StefanoMenci\AppData\Local\anaconda3\envs\AnvilCrating\lib\site-packages\fontTools\ttLib\tables\_g_l_y_f.py:688: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\Users\\StefanoMenci\\Downloads\\Roboto-BoldItalic.ttf'>
  self.data = data
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\StefanoMenci\AppData\Local\anaconda3\envs\AnvilCrating\lib\site-packages\fontTools\ttLib\tables\_g_l_y_f.py:688: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\Users\\StefanoMenci\\Downloads\\Roboto-Italic.ttf'>
  self.data = data
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\StefanoMenci\AppData\Local\anaconda3\envs\AnvilCrating\lib\site-packages\fontTools\ttLib\tables\_g_l_y_f.py:688: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\Users\\StefanoMenci\\Downloads\\Roboto-Regular.ttf'>
  self.data = data
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\StefanoMenci\AppData\Local\anaconda3\envs\AnvilCrating\lib\site-packages\fontTools\ttLib\tables\_g_l_y_f.py:688: ResourceWarning: unclosed file <_io.BufferedReader name='C:\\Users\\StefanoMenci\\Downloads\\Roboto-Bold.ttf'>
  self.data = data
ResourceWarning: Enable tracemalloc to get the object allocation traceback

fpdf2_warning_test.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants