Title: `math.ldexp()` rejects an `x` that defines `__float__` or `__index__` · Issue #9508 · RustPython/RustPython · GitHub
Open Graph Title: `math.ldexp()` rejects an `x` that defines `__float__` or `__index__` · Issue #9508 · RustPython/RustPython
X Title: `math.ldexp()` rejects an `x` that defines `__float__` or `__index__` · Issue #9508 · RustPython/RustPython
Description: Feature math.ldexp(x, i) raises TypeError in RustPython when x is neither a float nor an int, but CPython converts x with __float__ or __index__ like every other math function. Reproduction import math class F: def __float__(self): retur...
Open Graph Description: Feature math.ldexp(x, i) raises TypeError in RustPython when x is neither a float nor an int, but CPython converts x with __float__ or __index__ like every other math function. Reproduction import ...
X Description: Feature math.ldexp(x, i) raises TypeError in RustPython when x is neither a float nor an int, but CPython converts x with __float__ or __index__ like every other math function. Reproduction import ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9508
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`math.ldexp()` rejects an `x` that defines `__float__` or `__index__`","articleBody":"## Feature\n\n`math.ldexp(x, i)` raises `TypeError` in RustPython when `x` is neither a `float` nor an `int`, but CPython converts `x` with `__float__` or `__index__` like every other `math` function.\n\n### Reproduction\n\n```python\nimport math\n\nclass F:\n def __float__(self):\n return 1.5\n\nprint(math.ldexp(F(), 1))\n```\n\n**RustPython:**\n\n```\nTypeError: unexpected type F\n```\n\n**CPython:**\n\n```\n3.0\n```\n\nThe same `TypeError` comes from an `x` with only `__index__`, from `fractions.Fraction(1, 2)`, and from an `x` whose `__float__` raises, where CPython propagates that exception instead.\n\n### Root cause\n\n[`LdexpArgs`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/stdlib/src/math.rs#L457-L474) binds `x` as `Either\u003cPyRef\u003cPyFloat>, PyIntRef>`, so only exact `float` and `int` payloads pass and [`Either::try_from_object`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/function/either.rs#L93-L98) turns everything else into `unexpected type`. CPython's [`math_ldexp`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/mathmodule.c.h#L257-L266) converts `x` with `PyFloat_AsDouble`, which calls `__float__` and then `__index__`.\n\n### Fix\n\nRead a `float` instance directly and convert anything else with [`ArgIntoFloat`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/function/number.rs#L88-L94), which raises `OverflowError` for an `int` too large for a float, so the `try_bigint_to_f64` branch goes away. `ArgIntoFloat` on its own would call `__float__` on a `float` subclass, which `PyFloat_AsDouble` does not.\n\n### Environment\n\n- RustPython [713e14c](https://github.com/RustPython/RustPython/tree/713e14cfc78d06372c68a1804240de52a8340548) (0.6.1, Python 3.15.0a, macOS arm64), also reproduced on 0.4.0 (Debian 12)\n- CPython v3.15.2, also v3.14.4 and v3.12.10 (macOS arm64)\n\n## Python Documentation or reference to CPython source code\n\n- CPython `math_ldexp`: [`Modules/clinic/mathmodule.c.h#L257-L266`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/mathmodule.c.h#L257-L266) converts a non-`float` `x` with `PyFloat_AsDouble`:\n\n ```c\n if (PyFloat_CheckExact(args[0])) {\n x = PyFloat_AS_DOUBLE(args[0]);\n }\n else\n {\n x = PyFloat_AsDouble(args[0]);\n if (x == -1.0 && PyErr_Occurred()) {\n goto exit;\n }\n }\n ```\n\n- Documentation: [`math.ldexp`](https://docs.python.org/3.15/library/math.html#math.ldexp)\n\nAI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergence was found by differential testing against CPython and checked against CPython 3.15.2, and every source reference above was checked against the linked revisions.","author":{"url":"https://github.com/jseop-lim","@type":"Person","name":"jseop-lim"},"datePublished":"2026-11-01T03:46:12Z","url":"https://github.com/RustPython/RustPython/issues/9508"}
| 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:8a9845bc-5003-fab2-fb18-211f1ec54495 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A6AE:172B92:32F3B39:43E4F0B:6B01F700 |
| html-safe-nonce | dd2411396840562acbbf74ff2ed5acb413d086b1ad2d3c1deaefae61c47af523 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkFFOjE3MkI5MjozMkYzQjM5OjQzRTRGMEI6NkIwMUY3MDAiLCJ2aXNpdG9yX2lkIjoiOTQ4MjEzMjUwNDI2Nzk1Nzc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1abc0e56323af84ba1f4684bca4214b266233e1bdd08ee4f3fe63a18da2f324c |
| hovercard-subject-tag | issue:6142912156 |
| 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/9508/issue_layout |
| twitter:image | https://opengraph.githubassets.com/75a26659cf729921d636855a730077ca788ec55792c21fa0e3c5db1335896e82/RustPython/RustPython/issues/9508 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/75a26659cf729921d636855a730077ca788ec55792c21fa0e3c5db1335896e82/RustPython/RustPython/issues/9508 |
| og:image:alt | Feature math.ldexp(x, i) raises TypeError in RustPython when x is neither a float nor an int, but CPython converts x with __float__ or __index__ like every other math function. Reproduction import ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jseop-lim |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width