Skip to content

Fix one-argument math.gcd() and math.lcm() calling abs() before __index__ - #1356

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/math-gcd-lcm-single-index
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/math-gcd-lcm-single-index

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Part of #1291 (item 11).

With one argument, GcdNode.gcdOne and LcmNode.gcdOne take abs() of the argument and convert the result, so an object that defines only __index__ raises TypeError. CPython converts with __index__ first (math_gcd, math_lcm).

AS-IS

>>> class Idx:
...     def __index__(self): return -6
...
>>> math.gcd(Idx())
TypeError: bad operand type for abs(): 'Idx'
>>> math.lcm(Idx())
TypeError: bad operand type for abs(): 'Idx'

TO-BE

>>> math.gcd(Idx())
6
>>> math.lcm(Idx())
6

(matching CPython)

Changes

  • Swap the two calls in both nodes.
  • Add a test suite to tests/test_math.py.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest on test_math.py (the new test fails on the unpatched build and passes with the fix) and the tagged test_math (79 passed). mx python-checkcopyrights, mx eclipseformat and mx checkstyle are clean.

With a single argument both functions took abs() of the argument and
converted the result, so an object that defines only __index__ raised
TypeError.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 25, 2026

This branch has not been deployed

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

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant