Skip to content

Commit b84f656

Browse files
committed
Correcting __int__ node and real and other similar nodes extends this one.
1 parent a3ff553 commit b84f656

File tree

2 files changed

+59
-39
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

+59
-39
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,40 @@ def test_int_bit_length():
101101
class MyInt(int):
102102
pass
103103

104+
def test_int():
105+
def builtinTest(number):
106+
a = int(number)
107+
b = a.__int__()
108+
assert a == b
109+
assert a is b
110+
assert type(a) == int
111+
assert type(b) == int
112+
113+
builtinTest(-9)
114+
builtinTest(0)
115+
builtinTest(9)
116+
builtinTest(6227020800)
117+
builtinTest(9999992432902008176640000999999)
118+
119+
assert True.__int__() == 1
120+
assert False.__int__() == 0
121+
122+
123+
def test_int_subclass():
124+
def subclassTest(number):
125+
a = MyInt(number)
126+
b = a.__int__()
127+
assert a == b
128+
assert a is not b
129+
assert type(a) == MyInt
130+
assert type(b) == int
131+
132+
subclassTest(-9)
133+
subclassTest(0)
134+
subclassTest(9)
135+
subclassTest(6227020800)
136+
subclassTest(9999992432902008176640000999999)
137+
104138
def test_real_imag():
105139
def builtinTest(number):
106140
a = int(number)

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

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,42 +1854,8 @@ int bitLength(PInt argument) {
18541854

18551855
@GenerateNodeFactory
18561856
@Builtin(name = "real", fixedNumOfArguments = 1, isGetter = true, doc = "the real part of a complex number")
1857-
static abstract class RealNode extends PythonBuiltinNode {
1857+
static abstract class RealNode extends IntNode {
18581858

1859-
@Child private GetClassNode getClassNode;
1860-
1861-
protected PythonClass getClass(Object value) {
1862-
if (getClassNode == null) {
1863-
CompilerDirectives.transferToInterpreterAndInvalidate();
1864-
getClassNode = insert(GetClassNode.create());
1865-
}
1866-
return getClassNode.execute(value);
1867-
}
1868-
1869-
@Specialization
1870-
int get(boolean self) {
1871-
return self ? 1 : 0;
1872-
}
1873-
1874-
@Specialization
1875-
int get(int self) {
1876-
return self;
1877-
}
1878-
1879-
@Specialization
1880-
long get(long self) {
1881-
return self;
1882-
}
1883-
1884-
@Specialization(guards = "cannotBeOverridden(getClass(self))")
1885-
PInt getPInt(PInt self) {
1886-
return self;
1887-
}
1888-
1889-
@Specialization(guards = "!cannotBeOverridden(getClass(self))")
1890-
PInt getPIntOverriden(PInt self) {
1891-
return factory().createInt(self.getValue());
1892-
}
18931859
}
18941860

18951861
@GenerateNodeFactory
@@ -1903,13 +1869,13 @@ int get(@SuppressWarnings("unused") Object self) {
19031869

19041870
@GenerateNodeFactory
19051871
@Builtin(name = "numerator", fixedNumOfArguments = 1, isGetter = true, doc = "the numerator of a rational number in lowest terms")
1906-
static abstract class NumeratorNode extends RealNode {
1872+
static abstract class NumeratorNode extends IntNode {
19071873

19081874
}
19091875

19101876
@GenerateNodeFactory
19111877
@Builtin(name = "conjugate", fixedNumOfArguments = 1, doc = "Returns self, the complex conjugate of any int.")
1912-
static abstract class ConjugateNode extends RealNode {
1878+
static abstract class ConjugateNode extends IntNode {
19131879

19141880
}
19151881

@@ -1925,6 +1891,21 @@ int get(@SuppressWarnings("unused") Object self) {
19251891
@Builtin(name = SpecialMethodNames.__INT__, fixedNumOfArguments = 1)
19261892
@GenerateNodeFactory
19271893
abstract static class IntNode extends PythonBuiltinNode {
1894+
@Child private GetClassNode getClassNode;
1895+
1896+
protected PythonClass getClass(Object value) {
1897+
if (getClassNode == null) {
1898+
CompilerDirectives.transferToInterpreterAndInvalidate();
1899+
getClassNode = insert(GetClassNode.create());
1900+
}
1901+
return getClassNode.execute(value);
1902+
}
1903+
1904+
@Specialization
1905+
int doB(boolean self) {
1906+
return self ? 1 : 0;
1907+
}
1908+
19281909
@Specialization
19291910
int doI(int self) {
19301911
return self;
@@ -1935,8 +1916,13 @@ long doL(long self) {
19351916
return self;
19361917
}
19371918

1938-
@Specialization
1939-
PInt doPi(PInt self) {
1919+
@Specialization(guards = "cannotBeOverridden(getClass(self))")
1920+
PInt doPInt(PInt self) {
1921+
return self;
1922+
}
1923+
1924+
@Specialization(guards = "!cannotBeOverridden(getClass(self))")
1925+
PInt doPIntOverriden(PInt self) {
19401926
return factory().createInt(self.getValue());
19411927
}
19421928
}

0 commit comments

Comments
 (0)