python / python/cpython

Documentation: clarify that struct.pack_into() does not guarantee exact memory-access semantics

Đang mở
#156,866 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs extension-modules
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ả

Documentation

This is a documentation clarification request, not a request to change
struct.pack_into() behavior.

The current documentation for struct.pack_into() says that it writes the
packed bytes into a writable buffer:

https://docs.python.org/3/library/struct.html#struct.pack_into

For ordinary memory this is straightforward, but it may be misleading when
the writable buffer is backed by memory-mapped I/O (MMIO), where the number,
width, and ordering of actual memory accesses can be semantically significant.

For example:

struct.pack_into("<I", mmio_buffer, offset, value)

may look like a natural way to perform one 32-bit MMIO write.

However, the Python-level operation should not be assumed to correspond to
one native store or one device transaction.

In one tested CPython environment, a single struct.pack_into("<I", ...)
operation was observed to produce three native stores, with values equivalent
to:

0
0
value

The PCIe device observed three corresponding Memory Write requests.

This is intentionally not a claim that struct.pack_into() always performs
three stores. The observed access shape is implementation-, compiler-,
runtime-, platform-, and mapping-dependent. It should not be treated as a
Python or CPython API guarantee.

I think a short documentation note would make this abstraction boundary clear.
For example:

pack_into() specifies the packed bytes written to the buffer, but does not
guarantee the number, width, or ordering of the underlying native memory
accesses used to perform the write. In particular, writable buffers backed
by memory-mapped I/O (MMIO) should not be treated as exact-width register
access interfaces. Code that depends on exact MMIO access semantics should
use an interface designed for that purpose and verify its behavior for the
target implementation and platform.

I am not suggesting that MMIO should become a supported special case of
struct, nor that the current implementation is incorrect. The requested
change is only to document that a writable-buffer operation does not imply an
exact-MMIO-access contract.

Tested environment:

  • CPython 3.10.12 (/usr/bin/python3)
  • Ubuntu 22.04.5 LTS
  • Linux 6.8
  • x86-64, Intel Xeon E5-1620 v3
  • glibc 2.35
  • GCC 11.4

The investigation separated the Python-level operation, native load/store
instructions, PCIe requests, and FPGA-side observations.

Supporting evidence and reproduction records are public here:

https://github.com/bit-otter-jp/AS02MC04-XCKU3P

Relevant directories:

  • 02_as02mc04_pcie/WORK6/PART1_ANNEX3

    • investigation of Python MMIO access behavior
    • native instruction / PCIe request observations
  • 02_as02mc04_pcie/WORK6/PART1_ANNEX4

    • native CPython C-extension reference implementation
    • explicit read32() / write32() primitives
    • final-binary and hardware qualification
Linked PRs
  • gh-157052

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với tài liệu về struct.pack_into() trên trang tài liệu Python được liên kết và xem phần mô tả về bộ đệm có thể ghi ở xung quanh. Thêm một ghi chú ngắn gọn làm rõ rằng các byte đã đóng gói không đảm bảo số lượng, độ rộng hoặc thứ tự của các lần truy cập bộ nhớ native, đặc biệt đối với các bộ đệm được MMIO hỗ trợ. Hoàn tất khi giới hạn này được nêu rõ mà không thay đổi hành vi của struct.pack_into().

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
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.