ctypes: array slice assignment replaces the value's `__len__` exception with a ValueError

Aperta
#161,840 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
67/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start by locating the ctypes array slice assignment implementation and its tests; the issue identifies PySequence_Length and includes a reproducer using a BadLen class. Trace how a length error is handled before the slice-size comparison, then add a regression test confirming that the KeyError from __len__ propagates. Done means the reproducer raises KeyError("len raised") instead of the size-mismatch ValueError.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

extension-modules topic-ctypes type-bug
Bug report

Summary

Assigning a sequence to a slice of a ctypes array calls PySequence_Length on the value. When the value's __len__ raises, PySequence_Length returns -1 with the exception set, and the code reads the -1 as a length. The comparison -1 != slicelen is always true, so it raises its own ValueError("Can only assign sequence of same size") over the pending exception, which replaces it.

Expected: the exception from __len__ should propagate.

Reproducer

import ctypes

class BadLen:
    def __len__(self):
        raise KeyError("len raised")

arr = (ctypes.c_int * 3)()
arr[0:1] = BadLen()
ValueError: Can only assign sequence of same size
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-161859
Lingua principale
Python
Stelle
78.2k
Fork
36.6k
Merge medio
19h 57m
PR unite (30g)
630

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

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.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.