python / python/cpython

`struct.pack` with format `s` errors on `bytes`-like data

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

@serhiy-storchaka đang làm issue này rồi.

Từ ngày 14/4/2026.

extension-modules type-bug type-feature
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ả

Bug report

Bug description:

When using struct.pack with format s, it works with bytes and bytearray. However other bytes-like data (such as memoryview) is not handled raising an error. Here is some sample code show this issue

import struct

b = b"abad"

struct. pack("4s", b)               # Ok
struct. pack("4s", bytearray(b))    # Ok
struct. pack("4s", memoryview(b))   # Error

This code appears to contain the corresponding logic

https://github.com/python/cpython/blob/9a89f1bf1e7bb819fe7240be779c99a84f47ea46/Modules/_struct.c#L1936-L1941

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-148565
  • gh-149063
  • gh-149072
  • gh-152029

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.

Đánh giá

Issue này chưa được đánh giá.

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.