Title: _ctypes: callback return value conversion silently swallows TypeError and conversion errors · Issue #9955 · RustPython/RustPython · GitHub
Open Graph Title: _ctypes: callback return value conversion silently swallows TypeError and conversion errors · Issue #9955 · RustPython/RustPython
X Title: _ctypes: callback return value conversion silently swallows TypeError and conversion errors · Issue #9955 · RustPython/RustPython
Description: Feature When a ctypes callback function returns a value that cannot be converted to its declared restype (or when conversion raises an exception such as TypeError or ValueError), RustPython silently discards the error. The callback retur...
Open Graph Description: Feature When a ctypes callback function returns a value that cannot be converted to its declared restype (or when conversion raises an exception such as TypeError or ValueError), RustPython silentl...
X Description: Feature When a ctypes callback function returns a value that cannot be converted to its declared restype (or when conversion raises an exception such as TypeError or ValueError), RustPython silentl...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9955
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_ctypes: callback return value conversion silently swallows TypeError and conversion errors","articleBody":"## Feature\n\nWhen a ctypes callback function returns a value that cannot be converted to its declared `restype` (or when conversion raises an exception such as `TypeError` or `ValueError`), RustPython silently discards the error. The callback returns an uninitialized or default value to the caller without printing any unraisable exception.\n\nIn CPython, failed callback return conversions trigger `PyErr_WriteUnraisable(callable)` and display an unraisable exception warning, alerting the developer to the callback bug.\n\n### Reproduction\n\n```python\nimport ctypes\n\n@ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)\ndef callback(x):\n return \"not an integer\" # str cannot be converted to c_int\n\n# Invoke callback\nres = callback(42)\nprint(\"Returned:\", res)\n```\n\n### Observed Output\n\n**CPython 3.12 / 3.13**:\n```text\nException ignored on converting result of ctypes callback function \u003cfunction callback at 0x...>:\nTraceback (most recent call last):\n File \"\u003cstdin>\", line 1, in callback\nTypeError: 'str' object cannot be interpreted as an integer\nReturned: -540084243\n```\n\n**RustPython (main)**:\n```text\nReturned: 1\n```\n*(The conversion error is silently swallowed; no unraisable exception or warning is emitted).*\n\n### Cause\n\nIn `crates/vm/src/stdlib/_ctypes/function.rs` inside `python_to_ffi`:\n```rust\nSome(\"b\" | \"h\" | \"i\" | \"l\" | \"q\") => {\n if let Ok(i) = obj.try_int(vm) {\n unsafe {\n rustpython_host_env::ctypes::write_callback_result(...);\n }\n }\n}\n```\nFor numeric types (`b`, `h`, `i`, `l`, `q`, `B`, `c`, `H`, `I`, `L`, `Q`, `f`, `d`) and pointer types (`P`, `z`, `Z`), `obj.try_int(vm)` or `obj.try_float(vm)` uses `if let Ok(...) = ...`, silently dropping the error on failure.\n\nBy comparison, the `\"X\"` (COM BSTR) handler in the same function correctly runs:\n```rust\nErr(exc) => vm.run_unraisable(\n exc,\n Some(\"Exception ignored while converting result of ctypes callback function\".to_owned()),\n obj,\n),\n```\n\n## Python Documentation or reference to CPython source code\n\n- CPython `Modules/_ctypes/callbacks.c` in `_CallPythonObject`:\n `PyErr_WriteUnraisable(callable);` when converting the return value fails.\n- https://docs.python.org/3/library/ctypes.html#callback-functions\n","author":{"url":"https://github.com/alok-108","@type":"Person","name":"alok-108"},"datePublished":"2026-11-19T06:48:13Z","url":"https://github.com/RustPython/RustPython/issues/9955"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:db4b5556-87cd-439a-5306-a8345a84db53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C422:3F8AB1:3739968:462B396:6B01D40C |
| html-safe-nonce | c6393fd8f9e8fd32d934eb0b553517f4540abd5c501077a84e8239c6c7b2ce84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDIyOjNGOEFCMTozNzM5OTY4OjQ2MkIzOTY6NkIwMUQ0MEMiLCJ2aXNpdG9yX2lkIjoiODE1OTY2MzQ0MjkzNzEwNzQ2OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b5295409f6dccd4f39dbb77eaaf4272546e1f63736224a7ac22930c779f717be |
| hovercard-subject-tag | issue:6484087621 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9955/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c2abd29f83f8a0e6ef9b08d361711f69e2a60b8e043c20ebdab047ed570dcae9/RustPython/RustPython/issues/9955 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c2abd29f83f8a0e6ef9b08d361711f69e2a60b8e043c20ebdab047ed570dcae9/RustPython/RustPython/issues/9955 |
| og:image:alt | Feature When a ctypes callback function returns a value that cannot be converted to its declared restype (or when conversion raises an exception such as TypeError or ValueError), RustPython silentl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alok-108 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width