Skip to content

Commit 39bb7d4

Browse files
committed
[GR-10281] Int doesn't have implemented getters for numerator and denominator.
1 parent 93b91bb commit 39bb7d4

File tree

2 files changed

+79
-4
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints

2 files changed

+79
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def test_int_bit_length():
9898
assert (int(9999992432902008176640000999999)).bit_length() == 103
9999
assert (int(-9999992432902008176640000999999)).bit_length() == 103
100100

101+
class MyInt(int):
102+
pass
103+
101104
def test_real_imag():
102105
def builtinTest(number):
103106
a = int(number)
@@ -121,10 +124,7 @@ def builtinTest(number):
121124
assert True.imag == 0
122125
assert False.imag == 0
123126

124-
def test_real_imag_subclass():
125-
class MyInt(int):
126-
pass
127-
127+
def test_real_imag_subclass():
128128
def subclassTest(number):
129129
a = MyInt(number)
130130
b = a.real
@@ -141,3 +141,47 @@ def subclassTest(number):
141141
subclassTest(9)
142142
subclassTest(6227020800)
143143
subclassTest(9999992432902008176640000999999)
144+
145+
def test_numerator_denominator():
146+
def builtinTest(number):
147+
a = int(number)
148+
b = a.numerator
149+
c = a.denominator
150+
assert a == b
151+
assert a is b
152+
assert c == 1
153+
assert type(a) == int
154+
assert type(b) == int
155+
assert type(c) == int
156+
157+
builtinTest(-9)
158+
builtinTest(0)
159+
builtinTest(9)
160+
builtinTest(6227020800)
161+
builtinTest(9999992432902008176640000999999)
162+
163+
assert True.numerator == 1
164+
assert False.numerator == 0
165+
assert True.denominator == 1
166+
assert False.denominator == 1
167+
168+
def test_mumerator_denominator_subclass():
169+
class MyInt(int):
170+
pass
171+
172+
def subclassTest(number):
173+
a = MyInt(number)
174+
b = a.numerator
175+
c = a.denominator
176+
assert a == b
177+
assert a is not b
178+
assert c == 1
179+
assert type(a) == MyInt
180+
assert type(b) == int
181+
assert type(c) == int
182+
183+
subclassTest(-9)
184+
subclassTest(0)
185+
subclassTest(9)
186+
subclassTest(6227020800)
187+
subclassTest(9999992432902008176640000999999)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,37 @@ int get(@SuppressWarnings("unused") PInt self) {
19051905
}
19061906
}
19071907

1908+
@GenerateNodeFactory
1909+
@Builtin(name = "numerator", fixedNumOfArguments = 1, isGetter = true, doc = "the numerator of a rational number in lowest terms")
1910+
static abstract class NumeratorNode extends RealNode {
1911+
1912+
}
1913+
1914+
@GenerateNodeFactory
1915+
@Builtin(name = "denominator", fixedNumOfArguments = 1, isGetter = true, doc = "the denominator of a rational number in lowest terms")
1916+
static abstract class DenominatorNode extends PythonBuiltinNode {
1917+
@Specialization
1918+
int get(@SuppressWarnings("unused") boolean self) {
1919+
return 1;
1920+
}
1921+
1922+
@Specialization
1923+
int get(@SuppressWarnings("unused") int self) {
1924+
return 1;
1925+
}
1926+
1927+
@Specialization
1928+
int get(@SuppressWarnings("unused") long self) {
1929+
return 1;
1930+
}
1931+
1932+
@Specialization
1933+
int get(@SuppressWarnings("unused") PInt self) {
1934+
return 1;
1935+
}
1936+
}
1937+
1938+
19081939
@Builtin(name = SpecialMethodNames.__INT__, fixedNumOfArguments = 1)
19091940
@GenerateNodeFactory
19101941
abstract static class IntNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)