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

Automatically close objects via __del__ #142

Merged
merged 10 commits into from
Sep 25, 2022
Merged

Automatically close objects via __del__ #142

merged 10 commits into from
Sep 25, 2022

Conversation

mara004
Copy link
Member

@mara004 mara004 commented Sep 24, 2022

No description provided.

@mara004 mara004 linked an issue Sep 24, 2022 that may be closed by this pull request
@@ -188,8 +188,6 @@ def test_open_new():


def test_open_invalid():
with pytest.raises(TypeError, match=re.escape("__init__() missing 1 required positional argument: 'input_data'")):
pdf = pdfium.PdfDocument()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this test case because it doesn't check anything pypdfium2-specific. This is common Python behaviour.

Thereby, we avoid an "unraisable exception" warning when running tests (__del__ is still called, even if initialisation failed due to a parameter error handled by Python and the actual constructor code was never reached).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, theoretically, we could add some hasattr() check to suppress these warnings, but I don't think that's worth it, given that this only happens in case of serious API misuse.

This avoids an additional variable.

Nice side effect: Most PDFium methods have checks against passing in
nullptr (None). If callers try to use an object after its `.raw`
attribute has been set to None, now nothing should happen (as opposed to
a segmentation fault).

Also, it's good to remove the reference to the ctypes representation on
close() to directly enqueue it for being discarded.
@mara004 mara004 marked this pull request as ready for review September 25, 2022 10:00
@mara004 mara004 merged commit b068b9e into main Sep 25, 2022
@mara004 mara004 deleted the autoclose branch September 25, 2022 10:00
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 this pull request may close these issues.

Why pdf.close must be called explicitly and not via a destructor ?
1 participant