ctypes._CData._b_base_ does not point to root ctypes object
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Hi, I've been writing some binary interface "decoder" in python using ctypes, and one thing confuses me a bit (and also ruins some of my expectations)
The docs say:
The
_b_base_read-only member is the root ctypes object that owns the memory block.
So with this code:
import ctypes
bindata = bytearray(b"AAAABBBBCCCC")
SomeArray = ctypes.c_int * 3
class SomeData(ctypes.Structure):
_fields_ = [
("asdf", SomeArray),
]
class FinalData(ctypes.Structure):
_fields_ = [
("foo", SomeData),
]
x = FinalData.from_buffer(bindata)
print(x)
print(hex(ctypes.addressof(x)))
print(x._b_base_)
print()
foo = x.foo
print(foo)
print(hex(ctypes.addressof(foo)))
print(foo._b_base_)
print()
asdf = foo.asdf
print(asdf)
print(hex(ctypes.addressof(asdf)))
print(asdf._b_base_)
print()
I expect foo._b_base_ and asdf._b_base_ to point to the same FinalData object, meanwhile this is the output:
<__main__.FinalData object at 0x7f3bae3fa5d0>
0x7f3bae6985d0
None
<__main__.SomeData object at 0x7f3bae6597b0>
0x7f3bae6985d0
<__main__.FinalData object at 0x7f3bae3fa5d0>
<__main__.c_int_Array_3 object at 0x7f3bae69e450>
0x7f3bae6985d0
<__main__.SomeData object at 0x7f3bae6597b0>
So instead of root ctypes object, this returns the parent ctypes object. Is this an oversight/bug?
Python version: Python 3.14.0b2
OS: NixOS unstable
Linked PRs
- gh-152623
- gh-152706
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read the ctypes._CData.b_base documentation and run the provided reproducer first. Trace the parent chain for FinalData, SomeData, and SomeArray, then use the linked PRs as context; done means the documented root-owner behavior and regression coverage match the intended semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100