Skip to content

Commit 24dcf49

Browse files
committed
moved PyComplex_XXX nodes from PythonCextBuiltins to PythonCextComplexBuiltins
1 parent bfa1b64 commit 24dcf49

File tree

3 files changed

+204
-119
lines changed

3 files changed

+204
-119
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
473473
new RandomBuiltins(),
474474
new PythonCextBuiltins(),
475475
new PythonCextBytesBuiltins(),
476+
new PythonCextComplexBuiltins(),
476477
new PythonCextDictBuiltins(),
477478
new PythonCextFloatBuiltins(),
478479
new PythonCextListBuiltins(),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 2 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS;
6161
import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS;
6262
import static com.oracle.graal.python.nodes.SpecialMethodNames.VALUES;
63-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
6463
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
6564
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IADD__;
6665
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
@@ -95,7 +94,6 @@
9594
import com.oracle.graal.python.builtins.Python3Core;
9695
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
9796
import com.oracle.graal.python.builtins.PythonBuiltins;
98-
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.ComplexNode;
9997
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.MappingproxyNode;
10098
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.StrNode;
10199
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.AbsNode;
@@ -110,6 +108,7 @@
110108
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
111109
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins.InternNode;
112110
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltinsFactory.CreateFunctionNodeGen;
111+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextComplexBuiltins.PYTHON_CEXT_COMPLEX;
113112
import static com.oracle.graal.python.builtins.modules.cext.PythonCextFloatBuiltins.PYTHON_CEXT_FLOAT;
114113
import com.oracle.graal.python.builtins.objects.PNone;
115114
import com.oracle.graal.python.builtins.objects.PNotImplemented;
@@ -218,7 +217,6 @@
218217
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
219218
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
220219
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemScalarNode;
221-
import com.oracle.graal.python.builtins.objects.complex.PComplex;
222220
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins;
223221
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.ItemsNode;
224222
import com.oracle.graal.python.builtins.objects.dict.DictBuiltins.KeysNode;
@@ -428,6 +426,7 @@ public void initialize(Python3Core core) {
428426
public void postInitialize(Python3Core core) {
429427
PythonModule cext = core.lookupBuiltinModule(PYTHON_CEXT);
430428
addModuleDict(cext, PYTHON_CEXT_BYTES, core);
429+
addModuleDict(cext, PYTHON_CEXT_COMPLEX, core);
431430
addModuleDict(cext, PYTHON_CEXT_DICT, core);
432431
addModuleDict(cext, PYTHON_CEXT_FLOAT, core);
433432
addModuleDict(cext, PYTHON_CEXT_LONG, core);
@@ -784,122 +783,6 @@ public Object values(Object obj) {
784783
}
785784
}
786785

787-
///////////// complex /////////////
788-
789-
@Builtin(name = "PyComplex_AsCComplex", minNumOfPositionalArgs = 1)
790-
@GenerateNodeFactory
791-
abstract static class PyComplexAsCComplexNode extends PythonUnaryBuiltinNode {
792-
@Specialization
793-
PTuple asComplex(PComplex c) {
794-
return factory().createTuple(new Object[]{c.getReal(), c.getImag()});
795-
}
796-
797-
@Specialization(guards = "!isPComplex(obj)")
798-
Object asComplex(VirtualFrame frame, Object obj,
799-
@Cached ComplexNode complexNode,
800-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
801-
@Cached GetNativeNullNode getNativeNullNode) {
802-
try {
803-
PComplex c = (PComplex) complexNode.execute(frame, PythonBuiltinClassType.PComplex, obj, PNone.NO_VALUE);
804-
return factory().createTuple(new Object[]{c.getReal(), c.getImag()});
805-
} catch (PException e) {
806-
transformExceptionToNativeNode.execute(e);
807-
return getNativeNullNode.execute();
808-
}
809-
}
810-
}
811-
812-
@Builtin(name = "PyComplex_RealAsDouble", minNumOfPositionalArgs = 1)
813-
@GenerateNodeFactory
814-
abstract static class PyComplexRealAsDoubleNode extends PythonUnaryBuiltinNode {
815-
816-
@Specialization
817-
double asDouble(PComplex d) {
818-
return d.getReal();
819-
}
820-
821-
@Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
822-
public Object asDouble(VirtualFrame frame, Object obj,
823-
@Cached PyObjectGetAttr getAttr,
824-
@Cached CallNode callNode,
825-
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
826-
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
827-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
828-
try {
829-
return callNode.execute(getAttr.execute(frame, obj, "real"));
830-
} catch (PException e) {
831-
transformExceptionToNativeNode.execute(e);
832-
return -1.0;
833-
}
834-
}
835-
836-
@Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
837-
public Object asDoubleFloat(VirtualFrame frame, Object obj,
838-
@Cached PyObjectGetAttr getAttr,
839-
@Cached CallNode callNode,
840-
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
841-
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
842-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
843-
try {
844-
return callNode.execute(getAttr.execute(frame, obj, __FLOAT__));
845-
} catch (PException e) {
846-
transformExceptionToNativeNode.execute(e);
847-
return -1.0;
848-
}
849-
}
850-
851-
protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
852-
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex);
853-
}
854-
}
855-
856-
@Builtin(name = "PyComplex_ImagAsDouble", minNumOfPositionalArgs = 1)
857-
@GenerateNodeFactory
858-
abstract static class PyComplexImagAsDoubleNode extends PythonUnaryBuiltinNode {
859-
860-
@Specialization
861-
double asDouble(PComplex d) {
862-
return d.getImag();
863-
}
864-
865-
@Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
866-
public Object asDouble(VirtualFrame frame, Object obj,
867-
@Cached PyObjectGetAttr getAttr,
868-
@Cached CallNode callNode,
869-
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
870-
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
871-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
872-
try {
873-
return callNode.execute(getAttr.execute(frame, obj, "imag"));
874-
} catch (PException e) {
875-
transformExceptionToNativeNode.execute(e);
876-
return -1;
877-
}
878-
}
879-
880-
@SuppressWarnings("unused")
881-
@Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
882-
public Object asDouble(VirtualFrame frame, Object obj,
883-
@Cached GetClassNode getClassNode,
884-
@Cached IsSubtypeNode isSubtypeNode) {
885-
return 0.0;
886-
}
887-
888-
protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
889-
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex);
890-
}
891-
}
892-
893-
@Builtin(name = "PyComplex_FromDoubles", minNumOfPositionalArgs = 1)
894-
@GenerateNodeFactory
895-
abstract static class PyComplexFromDoublesNode extends PythonBinaryBuiltinNode {
896-
897-
@Specialization
898-
public PComplex asDouble(double r, double i) {
899-
return factory().createComplex(r, i);
900-
}
901-
}
902-
903786
///////////// number /////////////
904787

905788
@Builtin(name = "PyNumber_Check", minNumOfPositionalArgs = 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules.cext;
42+
43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
44+
import java.util.List;
45+
import com.oracle.graal.python.builtins.Builtin;
46+
import com.oracle.graal.python.builtins.CoreFunctions;
47+
import com.oracle.graal.python.builtins.Python3Core;
48+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
49+
import com.oracle.graal.python.builtins.PythonBuiltins;
50+
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.ComplexNode;
51+
import com.oracle.graal.python.builtins.objects.PNone;
52+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode;
53+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
54+
import com.oracle.graal.python.builtins.objects.complex.PComplex;
55+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
56+
import com.oracle.graal.python.lib.PyObjectGetAttr;
57+
import com.oracle.graal.python.nodes.call.CallNode;
58+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
59+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
60+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
61+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
62+
import com.oracle.graal.python.nodes.object.GetClassNode;
63+
import com.oracle.graal.python.runtime.exception.PException;
64+
import com.oracle.truffle.api.dsl.Cached;
65+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
66+
import com.oracle.truffle.api.dsl.NodeFactory;
67+
import com.oracle.truffle.api.dsl.Specialization;
68+
import com.oracle.truffle.api.frame.VirtualFrame;
69+
70+
@CoreFunctions(defineModule = PythonCextComplexBuiltins.PYTHON_CEXT_COMPLEX)
71+
@GenerateNodeFactory
72+
public class PythonCextComplexBuiltins extends PythonBuiltins {
73+
74+
public static final String PYTHON_CEXT_COMPLEX = "python_cext_complex";
75+
76+
@Override
77+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
78+
return PythonCextComplexBuiltinsFactory.getFactories();
79+
}
80+
81+
@Override
82+
public void initialize(Python3Core core) {
83+
super.initialize(core);
84+
}
85+
86+
///////////// complex /////////////
87+
88+
@Builtin(name = "PyComplex_AsCComplex", minNumOfPositionalArgs = 1)
89+
@GenerateNodeFactory
90+
abstract static class PyComplexAsCComplexNode extends PythonUnaryBuiltinNode {
91+
@Specialization
92+
PTuple asComplex(PComplex c) {
93+
return factory().createTuple(new Object[]{c.getReal(), c.getImag()});
94+
}
95+
96+
@Specialization(guards = "!isPComplex(obj)")
97+
Object asComplex(VirtualFrame frame, Object obj,
98+
@Cached ComplexNode complexNode,
99+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
100+
@Cached GetNativeNullNode getNativeNullNode) {
101+
try {
102+
PComplex c = (PComplex) complexNode.execute(frame, PythonBuiltinClassType.PComplex, obj, PNone.NO_VALUE);
103+
return factory().createTuple(new Object[]{c.getReal(), c.getImag()});
104+
} catch (PException e) {
105+
transformExceptionToNativeNode.execute(e);
106+
return getNativeNullNode.execute();
107+
}
108+
}
109+
}
110+
111+
@Builtin(name = "PyComplex_RealAsDouble", minNumOfPositionalArgs = 1)
112+
@GenerateNodeFactory
113+
abstract static class PyComplexRealAsDoubleNode extends PythonUnaryBuiltinNode {
114+
115+
@Specialization
116+
double asDouble(PComplex d) {
117+
return d.getReal();
118+
}
119+
120+
@Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
121+
public Object asDouble(VirtualFrame frame, Object obj,
122+
@Cached PyObjectGetAttr getAttr,
123+
@Cached CallNode callNode,
124+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
125+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
126+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
127+
try {
128+
return callNode.execute(getAttr.execute(frame, obj, "real"));
129+
} catch (PException e) {
130+
transformExceptionToNativeNode.execute(e);
131+
return -1.0;
132+
}
133+
}
134+
135+
@Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
136+
public Object asDoubleFloat(VirtualFrame frame, Object obj,
137+
@Cached PyObjectGetAttr getAttr,
138+
@Cached CallNode callNode,
139+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
140+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
141+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
142+
try {
143+
return callNode.execute(getAttr.execute(frame, obj, __FLOAT__));
144+
} catch (PException e) {
145+
transformExceptionToNativeNode.execute(e);
146+
return -1.0;
147+
}
148+
}
149+
150+
protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
151+
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex);
152+
}
153+
}
154+
155+
@Builtin(name = "PyComplex_ImagAsDouble", minNumOfPositionalArgs = 1)
156+
@GenerateNodeFactory
157+
abstract static class PyComplexImagAsDoubleNode extends PythonUnaryBuiltinNode {
158+
159+
@Specialization
160+
double asDouble(PComplex d) {
161+
return d.getImag();
162+
}
163+
164+
@Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
165+
public Object asDouble(VirtualFrame frame, Object obj,
166+
@Cached PyObjectGetAttr getAttr,
167+
@Cached CallNode callNode,
168+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
169+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
170+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
171+
try {
172+
return callNode.execute(getAttr.execute(frame, obj, "imag"));
173+
} catch (PException e) {
174+
transformExceptionToNativeNode.execute(e);
175+
return -1;
176+
}
177+
}
178+
179+
@SuppressWarnings("unused")
180+
@Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"})
181+
public Object asDouble(VirtualFrame frame, Object obj,
182+
@Cached GetClassNode getClassNode,
183+
@Cached IsSubtypeNode isSubtypeNode) {
184+
return 0.0;
185+
}
186+
187+
protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
188+
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex);
189+
}
190+
}
191+
192+
@Builtin(name = "PyComplex_FromDoubles", minNumOfPositionalArgs = 1)
193+
@GenerateNodeFactory
194+
abstract static class PyComplexFromDoublesNode extends PythonBinaryBuiltinNode {
195+
196+
@Specialization
197+
public PComplex asDouble(double r, double i) {
198+
return factory().createComplex(r, i);
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)