python / python/cpython

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

オープン
#148,529 コメント 7 件 リアクション 0 件 担当者 1 名 GitHub で見る

@serhiy-storchaka がすでに取り組んでいます。

2026年4月14日 から。

extension-modules type-bug type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。