Skip to content

Commit 763bc1c

Browse files
committed
[GR-7142] Make unittest work
PullRequest: graalpython-open/4
2 parents f14680d + adbdad1 commit 763bc1c

29 files changed

+543
-135
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.graal.python.builtins.modules.CodecsModuleBuiltins;
5050
import com.oracle.graal.python.builtins.modules.CollectionsModuleBuiltins;
5151
import com.oracle.graal.python.builtins.modules.ErrnoModuleBuiltins;
52+
import com.oracle.graal.python.builtins.modules.FaulthandlerModuleBuiltins;
5253
import com.oracle.graal.python.builtins.modules.FunctoolsModuleBuiltins;
5354
import com.oracle.graal.python.builtins.modules.GcModuleBuiltins;
5455
import com.oracle.graal.python.builtins.modules.IOModuleBuiltins;
@@ -255,6 +256,7 @@ public final class Python3Core implements PythonCore {
255256
new TracebackBuiltins(),
256257
new GcModuleBuiltins(),
257258
new AtexitModuleBuiltins(),
259+
new FaulthandlerModuleBuiltins(),
258260
new SysModuleBuiltins(),
259261
};
260262

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28-
import com.oracle.graal.python.PythonLanguage;
28+
import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE;
2929
import static com.oracle.graal.python.builtins.objects.PNotImplemented.NOT_IMPLEMENTED;
3030
import static com.oracle.graal.python.nodes.BuiltinNames.ABS;
3131
import static com.oracle.graal.python.nodes.BuiltinNames.CALLABLE;
@@ -52,6 +52,7 @@
5252
import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
5353
import static com.oracle.graal.python.nodes.BuiltinNames.__BREAKPOINT__;
5454
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
55+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
5556
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__;
5657
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
5758
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
@@ -69,6 +70,7 @@
6970
import java.util.List;
7071
import java.util.function.Supplier;
7172

73+
import com.oracle.graal.python.PythonLanguage;
7274
import com.oracle.graal.python.builtins.Builtin;
7375
import com.oracle.graal.python.builtins.CoreFunctions;
7476
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -181,7 +183,7 @@ public int absBoolean(boolean arg) {
181183
public Object absObject(Object object,
182184
@Cached("create(__ABS__)") LookupAndCallUnaryNode callAbsNode) {
183185
Object result = callAbsNode.executeObject(object);
184-
if (result == PNone.NO_VALUE) {
186+
if (result == NO_VALUE) {
185187
throw raise(TypeError, "bad operand type for abs(): %p", object);
186188
}
187189
return result;
@@ -200,7 +202,8 @@ public boolean callable(PythonCallable callable) {
200202
}
201203

202204
@Specialization
203-
public boolean callable(Object object) {
205+
public boolean callable(Object object,
206+
@Cached("create()") LookupInheritedAttributeNode getAttributeNode) {
204207
/**
205208
* Added temporarily to skip translation/execution errors in unit testing
206209
*/
@@ -209,6 +212,11 @@ public boolean callable(Object object) {
209212
return true;
210213
}
211214

215+
Object callAttr = getAttributeNode.execute(object, __CALL__);
216+
if (callAttr != NO_VALUE) {
217+
return true;
218+
}
219+
212220
return object instanceof PythonCallable;
213221
}
214222
}
@@ -637,7 +645,7 @@ private Object getId(PythonObject obj) {
637645
writeId = insert(WriteAttributeToObjectNode.create());
638646
}
639647
Object id = readId.execute(obj, idKey);
640-
if (id == PNone.NO_VALUE) {
648+
if (id == NO_VALUE) {
641649
id = GLOBAL_ID++;
642650
writeId.execute(obj, idKey, id);
643651
}
@@ -1128,7 +1136,7 @@ private double sumDoubleInternal(Object arg1, double start, boolean firstValProv
11281136
public Object sum(Object arg1, Object start,
11291137
@Cached("createBinaryProfile()") ConditionProfile hasStart) {
11301138
Object iterator = iter.executeWith(arg1);
1131-
return iterateGeneric(iterator, hasStart.profile(start != PNone.NO_VALUE) ? start : 0, errorProfile1);
1139+
return iterateGeneric(iterator, hasStart.profile(start != NO_VALUE) ? start : 0, errorProfile1);
11321140
}
11331141

11341142
private Object iterateGeneric(Object iterator, Object start, ConditionProfile errorProfile) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
3+
*
4+
* The Universal Permissive License (UPL), Version 1.0
5+
*
6+
* Subject to the condition set forth below, permission is hereby granted to any
7+
* person obtaining a copy of this software, associated documentation and/or data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.builtins.modules;
40+
41+
import java.util.ArrayList;
42+
import java.util.List;
43+
44+
import com.oracle.graal.python.builtins.CoreFunctions;
45+
import com.oracle.graal.python.builtins.PythonBuiltins;
46+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
47+
import com.oracle.truffle.api.dsl.NodeFactory;
48+
49+
@CoreFunctions(defineModule = "faulthandler")
50+
public class FaulthandlerModuleBuiltins extends PythonBuiltins {
51+
@Override
52+
protected List<? extends NodeFactory<? extends PythonBuiltinNode>> getNodeFactories() {
53+
return new ArrayList<>();
54+
}
55+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
29+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
30+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
31+
32+
import java.math.BigDecimal;
33+
import java.math.BigInteger;
2834
import java.util.List;
2935

3036
import com.oracle.graal.python.builtins.Builtin;
@@ -38,8 +44,6 @@
3844
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
3945
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4046
import com.oracle.graal.python.runtime.exception.PythonErrorType;
41-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
42-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
4347
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4448
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4549
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -51,8 +55,6 @@
5155
import com.oracle.truffle.api.dsl.Specialization;
5256
import com.oracle.truffle.api.dsl.TypeSystemReference;
5357
import com.oracle.truffle.api.profiles.ConditionProfile;
54-
import java.math.BigDecimal;
55-
import java.math.BigInteger;
5658

5759
@CoreFunctions(defineModule = "math")
5860
public class MathModuleBuiltins extends PythonBuiltins {
@@ -678,6 +680,54 @@ public boolean isNan(Object value) {
678680
}
679681
}
680682

683+
@Builtin(name = "isclose", minNumOfArguments = 2, keywordArguments = {"rel_tol", "abs_tol"})
684+
@GenerateNodeFactory
685+
public abstract static class IsCloseNode extends PythonBuiltinNode {
686+
private static double DEFAULT_REL = 1e-09;
687+
private static double DEFAULT_ABS = 0.0;
688+
689+
private boolean isCloseDouble(double a, double b, double rel_tol, double abs_tol) {
690+
double diff;
691+
692+
if (rel_tol < 0.0 || abs_tol < 0.0) {
693+
throw raise(ValueError, "tolerances must be non-negative");
694+
}
695+
696+
if (a == b) {
697+
return true;
698+
}
699+
700+
if (Double.isInfinite(a) || Double.isInfinite(b)) {
701+
return false;
702+
}
703+
704+
diff = Math.abs(b - a);
705+
return (((diff <= Math.abs(rel_tol * b)) ||
706+
(diff <= Math.abs(rel_tol * a))) ||
707+
(diff <= abs_tol));
708+
}
709+
710+
@Specialization
711+
public boolean isClose(double a, double b, @SuppressWarnings("unused") PNone rel_tol, @SuppressWarnings("unused") PNone abs_tol) {
712+
return isCloseDouble(a, b, DEFAULT_REL, DEFAULT_ABS);
713+
}
714+
715+
@Specialization
716+
public boolean isClose(double a, double b, @SuppressWarnings("unused") PNone rel_tol, double abs_tol) {
717+
return isCloseDouble(a, b, DEFAULT_REL, abs_tol);
718+
}
719+
720+
@Specialization
721+
public boolean isClose(double a, double b, double rel_tol, @SuppressWarnings("unused") PNone abs_tol) {
722+
return isCloseDouble(a, b, rel_tol, DEFAULT_ABS);
723+
}
724+
725+
@Specialization
726+
public boolean isClose(double a, double b, double rel_tol, double abs_tol) {
727+
return isCloseDouble(a, b, rel_tol, abs_tol);
728+
}
729+
}
730+
681731
@Builtin(name = "ldexp", fixedNumOfArguments = 2)
682732
@TypeSystemReference(PythonArithmeticTypes.class)
683733
@GenerateNodeFactory

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ String cwd() {
246246

247247
}
248248

249+
@Builtin(name = "getpid", fixedNumOfArguments = 0)
250+
@GenerateNodeFactory
251+
public abstract static class GetPidNode extends PythonBuiltinNode {
252+
@Specialization
253+
int getPid() {
254+
// TODO: this needs to be implemented properly at some point (consider managed execution
255+
// as well)
256+
return getContext().hashCode();
257+
}
258+
}
259+
249260
@Builtin(name = "fstat", fixedNumOfArguments = 1)
250261
@GenerateNodeFactory
251262
public abstract static class FstatNode extends PythonFileNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.complex;
4040

41+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETNEWARGS__;
42+
4143
import java.util.List;
4244

4345
import com.oracle.graal.python.builtins.Builtin;
@@ -454,7 +456,7 @@ PComplex pos(PComplex self) {
454456
}
455457

456458
@GenerateNodeFactory
457-
@Builtin(name = "__getnewargs__", fixedNumOfArguments = 1)
459+
@Builtin(name = __GETNEWARGS__, fixedNumOfArguments = 1)
458460
static abstract class GetNewArgsNode extends PythonBuiltinNode {
459461
@Specialization
460462
PTuple get(PComplex self) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.builtins.PythonBuiltins;
3737
import com.oracle.graal.python.builtins.objects.PNone;
3838
import com.oracle.graal.python.builtins.objects.list.PList;
39+
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
3940
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
4041
import com.oracle.graal.python.nodes.expression.CastToListNode;
4142
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -156,4 +157,21 @@ public Object traceback(PBaseException self) {
156157
return self.getTraceback(factory());
157158
}
158159
}
160+
161+
@Builtin(name = "with_traceback", fixedNumOfArguments = 2)
162+
@GenerateNodeFactory
163+
public abstract static class WithTracebackNode extends PythonBuiltinNode {
164+
165+
@Specialization
166+
public Object withTraceback(PBaseException self, @SuppressWarnings("unused") PNone tb) {
167+
self.clearTraceback();
168+
return PNone.NONE;
169+
}
170+
171+
@Specialization
172+
public Object withTraceback(PBaseException self, PTraceback tb) {
173+
self.setTraceback(tb);
174+
return PNone.NONE;
175+
}
176+
}
159177
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public void setTraceback(PTraceback traceback) {
114114
this.traceback = traceback.getException().traceback;
115115
}
116116

117+
public void clearTraceback() {
118+
this.traceback = new PTraceback[0];
119+
}
120+
117121
/**
118122
* Can be null in case of lazily formatted arguments.
119123
*/

0 commit comments

Comments
 (0)