GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ctypes: array slice assignment replaces the value's `__len__` exception with a ValueError · Issue #161840 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
ctypes: array slice assignment replaces the value's __len__ exception with a ValueError#161840
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@grgalex
grgalex
opened on Nov 12, 2026
Issue body actions

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-161840: Propagate __len__() exceptions in ctypes array slice assignment #161859

Activity

  1. grgalex
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 12, 2026
  2. ZeroIntensity
    added
    topic-ctypes
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.