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

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

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
67/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
Python
スター
78.2k
フォーク
36.6k
平均マージ
19時間 57分
マージ済み PR(30日)
630

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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