Visitar URL original
gh-125788: Improve ctypes error message for mismatched callback prototypes by aribaskagan · Pull Request #161873 · python/cpython · GitHub
Skip to content

gh-125788: Improve ctypes error message for mismatched callback prototypes - #161873

Open
aribaskagan wants to merge 1 commit into
python:mainfrom
aribaskagan:gh-125788-ctypes-callback-error
Open

aribaskagan wants to merge 1 commit into
python:mainfrom
aribaskagan:gh-125788-ctypes-callback-error

Conversation

@aribaskagan

@aribaskagan aribaskagan commented Nov 12, 2026 •

Copy link
Copy Markdown

When a CFUNCTYPE/WINFUNCTYPE callback is passed where a different callback prototype is expected, ctypes raised a confusing error because both prototype classes share the name CFunctionType:

ctypes.ArgumentError: argument 4: TypeError: expected CFunctionType instance instead of CFunctionType

This change makes CDataType_from_param_impl() report the expected and actual signatures when both sides are callback prototypes with argtypes set:

ctypes.ArgumentError: argument 4: TypeError: expected CFunctionType instance with signature (restype=<class 'ctypes.c_int'>, argtypes=(<class 'ctypes.c_void_p'>, <class 'ctypes.c_void_p'>)) instead of CFunctionType instance with signature (restype=<class 'ctypes.c_long'>, argtypes=(<class 'ctypes.c_void_p'>, <class 'ctypes.c_void_p'>))

Call behavior is unchanged; other mismatches keep the existing wording. Added test_mismatched_callback_prototype based on the qsort reproducer from the issue, plus a NEWS entry. ./python -m test test_ctypes passes on a Linux debug build.

AI assistance was used in preparing this change; I reviewed and tested it.

… prototypes

CFUNCTYPE and WINFUNCTYPE prototypes are distinct classes that share one
type name, so a mismatch was reported as "expected CFunctionType instance
instead of CFunctionType".  Include both signatures in the TypeError.

Co-authored-by: Kağan Arıbaş <aribaskagan@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 14:20
@python-cla-bot

python-cla-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T14:24:58.355484Z f6c92af PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor
cursor Bot force-pushed the gh-125788-ctypes-callback-error branch from f6c92af to fd17c9a Compare November 12, 2026 17:02
@ZeroIntensity ZeroIntensity added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 12, 2026
@bedevere-app

bedevere-app Bot commented Nov 12, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants