Skip to content

Add __index__ fallback to mpz constructor (fixes #237)#238

Merged
skirpichev merged 8 commits intodiofant:masterfrom
WebVishnu:issue-237-index-fallback
Aug 15, 2025
Merged

Add __index__ fallback to mpz constructor (fixes #237)#238
skirpichev merged 8 commits intodiofant:masterfrom
WebVishnu:issue-237-index-fallback

Conversation

@WebVishnu
Copy link
Copy Markdown
Contributor

Add index fallback to mpz constructor (fixes #237)

Problem

The mpz constructor only supports objects with __int__() method, failing with objects that only implement __index__().

Solution

Added fallback mechanism that:

  1. Tries __int__() first (maintains backward compatibility)
  2. Falls back to __index__() if __int__() not available

Changes

  • Modified new_impl function in main.c to add __index__() fallback

Copy link
Copy Markdown
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

This also needs tests.

Comment thread main.c Outdated
Comment thread main.c Outdated
Comment thread main.c Outdated
Comment thread main.c Outdated
Comment thread main.c Outdated
Comment thread main.c Outdated
Comment thread main.c Outdated
@WebVishnu
Copy link
Copy Markdown
Contributor Author

@skirpichev should I create a new file of test cases for this update?

Comment thread main.c
Comment thread main.c Outdated
@skirpichev
Copy link
Copy Markdown
Member

should I create a new file of test cases for this update?

No, you could add tests like for a case with __int__() dunder. See test_mpz_interface().

Copy link
Copy Markdown
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

LGTM

@skirpichev skirpichev merged commit 88ba04a into diofant:master Aug 15, 2025
19 checks passed
@skirpichev
Copy link
Copy Markdown
Member

Thanks.

@skirpichev skirpichev added this to the 0.4 milestone Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fallback to __index__() dunder in the mpz constructor, if __int__() is not defined for a number

2 participants