Skip to content

Commit 924de16

Browse files
committed
[GR-10278] int and float types don't have defined real and imag getters.
1 parent b68b8f5 commit 924de16

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,43 @@ def fromhex(cls, value1, value2):
575575
f = F.fromhex('1', '1')
576576
self.assertEqual(17.0, f)
577577
self.assertEqual(F, type(f))
578+
579+
class RealImagTests(unittest.TestCase):
580+
def test_real_imag(self):
581+
def builtinTest(number):
582+
a = float(number)
583+
b = a.real
584+
c = a.imag
585+
assert a == b
586+
assert a is b
587+
assert c == 0
588+
assert type(a) == float
589+
assert type(b) == float
590+
assert type(c) == float
591+
592+
builtinTest(-9.1)
593+
builtinTest(0.0)
594+
builtinTest(9.2)
595+
builtinTest(6227020800.90)
596+
builtinTest(9999992432902008176640000999999.1)
597+
598+
def test_real_imag_subclass(self):
599+
class MyFloat(float):
600+
pass
601+
602+
def subclassTest(number):
603+
a = MyFloat(number)
604+
b = a.real
605+
c = a.imag
606+
assert a == b
607+
assert a is not b
608+
assert c == 0.0
609+
assert type(a) == MyFloat
610+
assert type(b) == float
611+
assert type(c) == float
612+
613+
subclassTest(-9.0)
614+
subclassTest(0.0)
615+
subclassTest(9.1)
616+
subclassTest(6227020800.2)
617+
subclassTest(9999992432902008176640000999999.33)

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,47 @@ def test_int_bit_length():
9797
assert (int(-2432902008176640000)).bit_length() == 62
9898
assert (int(9999992432902008176640000999999)).bit_length() == 103
9999
assert (int(-9999992432902008176640000999999)).bit_length() == 103
100+
101+
def test_real_imag():
102+
def builtinTest(number):
103+
a = int(number)
104+
b = a.real
105+
c = a.imag
106+
assert a == b
107+
assert a is b
108+
assert c == 0
109+
assert type(a) == int
110+
assert type(b) == int
111+
assert type(c) == int
112+
113+
builtinTest(-9)
114+
builtinTest(0)
115+
builtinTest(9)
116+
builtinTest(6227020800)
117+
builtinTest(9999992432902008176640000999999)
118+
119+
assert True.real == 1
120+
assert False.real == 0
121+
assert True.imag == 0
122+
assert False.imag == 0
123+
124+
def test_real_imag_subclass():
125+
class MyInt(int):
126+
pass
127+
128+
def subclassTest(number):
129+
a = MyInt(number)
130+
b = a.real
131+
c = a.imag
132+
assert a == b
133+
assert a is not b
134+
assert c == 0
135+
assert type(a) == MyInt
136+
assert type(b) == int
137+
assert type(c) == int
138+
139+
subclassTest(-9)
140+
subclassTest(0)
141+
subclassTest(9)
142+
subclassTest(6227020800)
143+
subclassTest(9999992432902008176640000999999)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7272
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7373
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
74+
import com.oracle.graal.python.nodes.object.GetClassNode;
7475
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7576
import com.oracle.graal.python.runtime.JavaTypeConversions;
7677
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -875,6 +876,50 @@ abstract static class NegNode extends PythonUnaryBuiltinNode {
875876
}
876877
}
877878

879+
@GenerateNodeFactory
880+
@Builtin(name = "real", fixedNumOfArguments = 1, isGetter = true)
881+
static abstract class RealNode extends PythonBuiltinNode {
882+
@Specialization
883+
float get(float self) {
884+
return self;
885+
}
886+
887+
@Specialization
888+
double get(double self) {
889+
return self;
890+
}
891+
892+
@Specialization
893+
PFloat get(PFloat self,
894+
@Cached("create()") GetClassNode clazzNode) {
895+
PythonClass clazz = clazzNode.execute(self);
896+
if (clazz.isBuiltin()) {
897+
return self;
898+
}
899+
return factory().createFloat(self.getValue());
900+
}
901+
}
902+
903+
@GenerateNodeFactory
904+
@Builtin(name = "imag", fixedNumOfArguments = 1, isGetter = true)
905+
static abstract class ImagNode extends PythonBuiltinNode {
906+
907+
@Specialization
908+
float get(@SuppressWarnings("unused") float self) {
909+
return 0;
910+
}
911+
912+
@Specialization
913+
double get(@SuppressWarnings("unused") double self) {
914+
return 0;
915+
}
916+
917+
@Specialization
918+
double get(@SuppressWarnings("unused") PFloat self) {
919+
return 0;
920+
}
921+
}
922+
878923
@Builtin(name = __GETFORMAT__, fixedNumOfArguments = 2)
879924
@GenerateNodeFactory
880925
abstract static class GetFormatNode extends PythonUnaryBuiltinNode {

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@
4747
import com.oracle.graal.python.builtins.objects.PNone;
4848
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4949
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
50+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5051
import com.oracle.graal.python.nodes.SpecialMethodNames;
5152
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5253
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5354
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
55+
import com.oracle.graal.python.nodes.object.GetClassNode;
5456
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
5557
import com.oracle.graal.python.runtime.ArithmeticUtil;
5658
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5759
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
60+
import com.oracle.truffle.api.dsl.Cached;
5861
import com.oracle.truffle.api.dsl.Fallback;
5962
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6063
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -1849,6 +1852,59 @@ int bitLength(PInt argument) {
18491852
}
18501853
}
18511854

1855+
@GenerateNodeFactory
1856+
@Builtin(name = "real", fixedNumOfArguments = 1, isGetter = true)
1857+
static abstract class RealNode extends PythonBuiltinNode {
1858+
@Specialization
1859+
int get(boolean self) {
1860+
return self ? 1 : 0;
1861+
}
1862+
1863+
@Specialization
1864+
int get(int self) {
1865+
return self;
1866+
}
1867+
1868+
@Specialization
1869+
long get(long self) {
1870+
return self;
1871+
}
1872+
1873+
@Specialization
1874+
PInt get(PInt self,
1875+
@Cached("create()") GetClassNode clazzNode) {
1876+
PythonClass clazz = clazzNode.execute(self);
1877+
if (clazz.isBuiltin()) {
1878+
return self;
1879+
}
1880+
return factory().createInt(self.getValue());
1881+
}
1882+
}
1883+
1884+
@GenerateNodeFactory
1885+
@Builtin(name = "imag", fixedNumOfArguments = 1, isGetter = true)
1886+
static abstract class ImagNode extends PythonBuiltinNode {
1887+
@Specialization
1888+
int get(@SuppressWarnings("unused") boolean self) {
1889+
return 0;
1890+
}
1891+
1892+
@Specialization
1893+
int get(@SuppressWarnings("unused") int self) {
1894+
return 0;
1895+
}
1896+
1897+
@Specialization
1898+
int get(@SuppressWarnings("unused") long self) {
1899+
return 0;
1900+
}
1901+
1902+
@Specialization
1903+
int get(@SuppressWarnings("unused") PInt self) {
1904+
return 0;
1905+
}
1906+
}
1907+
18521908
@Builtin(name = SpecialMethodNames.__INT__, fixedNumOfArguments = 1)
18531909
@GenerateNodeFactory
18541910
abstract static class IntNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)