Title: Support custom converter types and instances in ctypes argtypes by alok-108 · Pull Request #9685 · RustPython/RustPython · GitHub
Open Graph Title: Support custom converter types and instances in ctypes argtypes by alok-108 · Pull Request #9685 · RustPython/RustPython
X Title: Support custom converter types and instances in ctypes argtypes by alok-108 · Pull Request #9685 · RustPython/RustPython
Description: Closes #8341 One of checkbox below must be checked. I did not use AI to write the code of this patch. This PR follows our AI policy Summary In CPython's ctypes, any object or type with a from_param method can be passed in argtypes. Previously, RustPython required every item in argtypes to be a known ctypes type upfront before calling from_param. Furthermore, when checking whether an argument should be passed as an aggregate by value, it checked whether the converter type itself was a subclass of Structure, rather than checking the type of the converted object. This broke calls that use custom converters (such as rich._win32_console._FillConsoleOutputCharacterW, where WindowsCoordinates is a NamedTuple with a from_param classmethod returning a COORD structure). Changes In extract_arg_types_and_converters, allow arbitrary types/instances as long as they have a from_param method, raising TypeError: item %d in _argtypes_ has no from_param method on AttributeError. Propagate non-AttributeError exceptions directly. In PyCFuncPtrType::init, resolve and store class-level converters in StgInfo when the prototype class is created, matching CPython. In set_argtypes, resolve and cache the validated from_param callables in converters, avoiding repeated descriptor lookups at call time. In extract_call_info, use the cached converters when available to avoid redundant lookups during calls, and synchronize lock acquisition order with set_argtypes. In convert_object, invoke the cached from_param converter directly without rejecting custom converter types or instances upfront. When dispatching structures and unions by value, check the converted object's type (converted.class()) rather than the converter type. Keep converted objects alive across foreign function calls in the conv_param fallback. Wrap conversion errors in ArgumentError: argument %d: ... with the original exception chained via __cause__. In PyCThunk::new, reject non-ctypes classes in callback argtypes. Validation cargo test -p rustpython-vm --lib stdlib::_ctypes::function (passed) cargo run -p rustpython -- extra_tests/snippets/stdlib_ctypes.py (passed) cargo fmt --check and cargo clippy -p rustpython-vm (passed)
Open Graph Description: Closes #8341 One of checkbox below must be checked. I did not use AI to write the code of this patch. This PR follows our AI policy Summary In CPython's ctypes, any object or type with a f...
X Description: Closes #8341 One of checkbox below must be checked. I did not use AI to write the code of this patch. This PR follows our AI policy Summary In CPython's ctypes, any object or type with...
Opengraph URL: https://github.com/RustPython/RustPython/pull/9685
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:70be480f-5339-556e-6109-49f2d37d0f65 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AA8A:19E4BB:322D57A:4006D17:6B01D7E4 |
| html-safe-nonce | 8ae42521dfa89243c5bca69ff5530bba3c5501fde346e5f713a2be92f5da92d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQThBOjE5RTRCQjozMjJENTdBOjQwMDZEMTc6NkIwMUQ3RTQiLCJ2aXNpdG9yX2lkIjoiNDQxNjI0NzM1MTY0NDExNjk2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d28886fa18d5ff601381a9737227e8221036ec0d1bd63352b06cce98927705e6 |
| hovercard-subject-tag | pull_request:5147009247 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/RustPython/RustPython/pull/9685/files |
| twitter:image | https://avatars.githubusercontent.com/u/166419621?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/166419621?s=400&v=4 |
| og:image:alt | Closes #8341 One of checkbox below must be checked. I did not use AI to write the code of this patch. This PR follows our AI policy Summary In CPython's ctypes, any object or type with a f... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
Viewport: width=device-width