Repository navigation
Clarify ctypes _b_needsfree_ ownership in tagVARIANT and add tests. - #972
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #972 +/- ##
==========================================
+ Coverage 89.53% 89.55% +0.01%
==========================================
Files 143 143
Lines 14122 14147 +25
==========================================
+ Hits 12644 12669 +25
Misses 1478 1478 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Correct the outdated comment in `tagVARIANT.__del__`. Previously, a comment stated that `_b_needsfree_` is never set because the buffer is internal to the object, suggesting `_VariantClear` was not invoked. However, in `ctypes` (`Modules/_ctypes/_ctypes.c`), even when an object fits within `sizeof(obj->b_value)` (16 bytes) and uses the internal inline buffer, `obj->b_needsfree` is explicitly initialized to `1`. As documented in CPython: > The b_needsfree flag does not mean that we actually did > call PyMem_Malloc to allocate the memory block; instead it > means we are the *owner* of the memory and are responsible > for freeing resources associated with the memory. This is > also the reason that b_needsfree is exposed to Python. In `ctypes`, `b_needsfree` is set to `0` only when the object does not own its buffer (e.g. referencing a parent object's buffer as a struct member, an array item `arr[0]`, or pointer contents `p.contents`). Standalone `VARIANT` instances own their buffer, so `_b_needsfree_` is set to `1` and `__del__` invokes `_VariantClear(self)` as intended.
Verify that `tagVARIANT.__del__` invokes `_VariantClear` for standalone `VARIANT` instances. Add `VariantTestCase.test_del_to_clear` to assert that: - Standalone `VARIANT` instances have `_b_needsfree_ == 1` because they own their buffer. - `_VariantClear` is called when the instance is deleted, clearing its type (`VT_EMPTY`) and value.
junkmd
force-pushed
the
variant_dunder_del_comment
branch
from
October 18, 2026 14:22
e9e9785 to
fefa623
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
While investigating intermittent CI test failures in #971 for potential resource leaks, I checked
tagVARIANT.__del__and found that the existing comment ("_b_needsfree_is never set because the buffer is internal to the object") does not reflect modernctypesbehavior.In CPython's
ctypes:VARIANT) haveobj->b_needsfree = 1because they own the memory and are responsible for freeing its resources.obj->b_needsfree = 0as they lack ownership.This PR updates the outdated comment in
tagVARIANT.__del__and adds a test verifying that standaloneVARIANTinstances have_b_needsfree_ == 1and properly invoke_VariantClearwhen deleted.