ctypes._CData.from_buffer does not inherit _b_base_
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Continuing work from #135669, I have noticed another inconsistency.
This method returns a ctypes instance that shares the buffer of the source object.
The _b_base_ read-only member is the root ctypes object that owns the memory block.
So, I assumed that, when passing a ctypes object as source to from_buffer, the _b_base_ from the resulting ctypes object would be inherited from source, however that is not the case.
To better visualize the issue, lets consider this code:
import ctypes
bindata = bytearray([
0, 0, # [0].offset
2, 0, # [0].variant
8, 0, # [1].offset
1, 0, # [1].variant
# dynamic_part
1, 2, 3, 4,
5, 6,
0, 0,
9, 8, 7, 6,
])
class A(ctypes.Structure):
_fields_ = [
("foo", ctypes.c_int),
]
class B(ctypes.Structure):
_fields_ = [
("bar", ctypes.c_byte * 6),
]
class Message(ctypes.Structure):
_fields_ = [
("offset", ctypes.c_uint16),
("variant", ctypes.c_uint16),
]
variants = {
1: A,
2: B,
}
def get(self):
root = self._b_base_._b_base_
_type = self.variants[self.variant]
try:
return _type.from_buffer(root.dynamic_part, self.offset)
except ValueError as e:
e.add_note(f"Invalid data offset={self.offset} variant={self.variant}")
raise
class FinalData(ctypes.Structure):
_fields_ = [
("messages", Message * 2),
("dynamic_part", ctypes.c_ubyte * 12),
]
x = FinalData.from_buffer(bindata)
print(x)
print(x.dynamic_part._b_base_)
a = x.messages[1].get()
b = x.messages[0].get()
print(a)
print(a._b_base_)
print(b)
print(b._b_base_)
I expected the a._b_base_ and b._b_base_ would be <__main__.FinalData object at 0x7fbee293d3d0>, like x.dynamic_part._b_base_, instead it is None.
Linked PRs
- gh-152706
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng việc xem xét PR được liên kết gh-152706 và chạy trình tái hiện ctypes được cung cấp. Sau đó kiểm tra phần triển khai phía sau _CData.from_buffer và các bài kiểm thử hiện có; được xem là hoàn tất khi chuỗi quyền sở hữu b_base của đối tượng kết quả khớp với hành vi quyền sở hữu buffer được ghi nhận trong tài liệu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100