python / python/cpython

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

Aperta
#148,529 7 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@serhiy-storchaka ci sta già lavorando.

Dal 14/4/2026.

extension-modules type-bug type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.