Skip to content

Commit 12680de

Browse files
committed
Merge remote-tracking branch 'origin/master' into tim/GR-10719/sklearn
2 parents 8625374 + 122ad01 commit 12680de

File tree

68 files changed

+1020
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1020
-589
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
import com.oracle.truffle.api.nodes.NodeUtil;
106106
import com.oracle.truffle.api.source.Source;
107107

108-
@SuppressWarnings({"unchecked"})
109108
public class TestParserTranslator {
110109
PythonContext context;
111110

@@ -132,7 +131,7 @@ <T> T getChild(Node result, int num, Class<? extends T> klass) {
132131
continue;
133132
}
134133
assertTrue("Expected an instance of " + klass + ", got " + n.getClass(), klass.isInstance(n));
135-
return (T) n;
134+
return klass.cast(n);
136135
}
137136
assertFalse("Expected an instance of " + klass + ", got null", true);
138137
return null;
@@ -158,7 +157,12 @@ Object literalAs(String src, Class<? extends PNode> klass) {
158157
<T> T literalAs(String src, Class<? extends PNode> klass, Class<? extends T> rklass) {
159158
Object literal = literalAs(src, klass);
160159
assertTrue("Expected an instance of " + rklass + ", got " + literal.getClass(), rklass.isInstance(literal));
161-
return (T) literal;
160+
return rklass.cast(literal);
161+
}
162+
163+
<T> T assertInstanceOf(PNode value, Class<? extends T> klass) {
164+
assertTrue("Expected an instance of " + klass + ", got " + value.getClass(), klass.isInstance(value));
165+
return klass.cast(value);
162166
}
163167

164168
@Test
@@ -247,18 +251,19 @@ public void parseGlobalVariable() {
247251
public void parsePropertyAccess() {
248252
parseAs("foobar13_ddsA.property", GetAttributeNode.class);
249253
GetAttributeNode anotherProperty = parseAs("foobar13_ddsA.property.anotherProperty", GetAttributeNode.class);
250-
GetAttributeNode property = getFirstChild(anotherProperty, GetAttributeNode.class);
251-
getFirstChild(property, ReadGlobalOrBuiltinNode.class);
254+
GetAttributeNode property = assertInstanceOf(anotherProperty.getObject(), GetAttributeNode.class);
255+
assertInstanceOf(property.getObject(), ReadGlobalOrBuiltinNode.class);
252256
}
253257

254258
@Test
255259
public void parseSubscript() {
256-
getChild(parseAs("foobar[1]", GetItemNode.class), 1, ReadGlobalOrBuiltinNode.class);
260+
GetItemNode node = parseAs("foobar[1]", GetItemNode.class);
261+
assertInstanceOf(node.getLeftNode(), ReadGlobalOrBuiltinNode.class);
257262
parseAs("foobar[:]", GetItemNode.class);
258263
parseAs("foobar[::]", GetItemNode.class);
259264
parseAs("foobar[1:2:3]", GetItemNode.class);
260265
GetItemNode parseAs = parseAs("foobar[1,2]", GetItemNode.class);
261-
assert parseAs.getSlice() instanceof TupleLiteralNode;
266+
assertInstanceOf(parseAs.getSlice(), TupleLiteralNode.class);
262267
}
263268

264269
@Test

graalpython/com.oracle.graal.python.test/src/python_unittests.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
import os
4141
import re
4242
import subprocess
43-
import sys
4443
from collections import defaultdict
4544
from json import dumps
4645
from multiprocessing import Pool
4746
from pprint import pformat
48-
from time import gmtime, strftime
4947

5048
import argparse
49+
import sys
50+
from time import gmtime, strftime
5151

5252
# global CLI flags
5353
flags = None
@@ -64,8 +64,10 @@
6464
PTRN_ERROR = re.compile(r'^(?P<error>[A-Z][a-z][a-zA-Z]+):(?P<message>.*)$')
6565
PTRN_UNITTEST = re.compile(r'^#### running: graalpython/lib-python/3/test/(?P<unittest>.*)$')
6666
PTRN_NUM_TESTS = re.compile(r'^Ran (?P<num_tests>\d+) test.*$')
67-
PTRN_NUM_ERRORS = re.compile(
67+
PTRN_FAILED = re.compile(
6868
r'^FAILED \((failures=(?P<failures>\d+))?(, )?(errors=(?P<errors>\d+))?(, )?(skipped=(?P<skipped>\d+))?\)$')
69+
PTRN_OK = re.compile(
70+
r'^OK \((failures=(?P<failures>\d+))?(, )?(errors=(?P<errors>\d+))?(, )?(skipped=(?P<skipped>\d+))?\)$')
6971
PTRN_JAVA_EXCEPTION = re.compile(r'^(?P<exception>com\.oracle\.[^:]*):(?P<message>.*)')
7072
PTRN_MODULE_NOT_FOUND = re.compile(r'.*ModuleNotFound: \'(?P<module>.*)\'\..*', re.DOTALL)
7173

@@ -169,8 +171,8 @@ def __init__(self):
169171
self.num_skipped = -1
170172

171173
def all_ok(self):
172-
self.num_errors = 0
173174
self.num_fails = 0
175+
self.num_errors = 0
174176
self.num_skipped = 0
175177

176178
@property
@@ -212,12 +214,23 @@ def process_output(output_lines):
212214
stats[unittests[-1]].all_ok()
213215
continue
214216

217+
match = re.match(PTRN_OK, line)
218+
if match:
219+
fails = match.group('failures')
220+
errs = match.group('errors')
221+
skipped = match.group('skipped')
222+
223+
stats[unittests[-1]].num_fails = int(fails) if fails else 0
224+
stats[unittests[-1]].num_errors = int(errs) if errs else 0
225+
stats[unittests[-1]].num_skipped = int(skipped) if skipped else 0
226+
continue
227+
215228
match = re.match(PTRN_NUM_TESTS, line)
216229
if match:
217230
stats[unittests[-1]].num_tests = int(match.group('num_tests'))
218231
continue
219232

220-
match = re.match(PTRN_NUM_ERRORS, line)
233+
match = re.match(PTRN_FAILED, line)
221234
if match:
222235
fails = match.group('failures')
223236
errs = match.group('errors')

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,57 @@ def register():
850850
register()
851851

852852
assert MyClass.a_counter == 24
853+
854+
855+
def test_generator_func_with_nested_nonlocals():
856+
def b_func():
857+
exec_gen = False
858+
859+
def _inner_func():
860+
def doit():
861+
nonlocal exec_gen
862+
exec_gen = True
863+
return [1]
864+
865+
assert set(doit.__code__.co_cellvars) == set()
866+
assert set(doit.__code__.co_freevars) == {'exec_gen'}
867+
for A in doit():
868+
for C in Y:
869+
yield A
870+
871+
assert set(_inner_func.__code__.co_cellvars) == set()
872+
assert set(_inner_func.__code__.co_freevars) == {'Y', 'exec_gen'}
873+
874+
gen = _inner_func()
875+
assert not exec_gen
876+
877+
Y = [1, 2]
878+
879+
list(gen)
880+
assert exec_gen
881+
return gen
882+
883+
assert set(b_func.__code__.co_cellvars) == {'Y', 'exec_gen'}
884+
assert set(b_func.__code__.co_freevars) == set()
885+
b_func()
886+
887+
888+
def test_generator_scope():
889+
my_obj = [1, 2, 3, 4]
890+
my_obj = (i for i in my_obj for j in y)
891+
y = [1, 2]
892+
893+
assert set(my_obj.gi_code.co_cellvars) == set()
894+
assert set(my_obj.gi_code.co_freevars) == {'y'}
895+
896+
897+
def test_func_scope():
898+
my_obj = [1, 2, 3, 4]
899+
900+
def my_obj():
901+
return [i for i in my_obj for j in y]
902+
903+
y = [1, 2]
904+
905+
assert set(my_obj.__code__.co_cellvars) == set()
906+
assert set(my_obj.__code__.co_freevars) == {'my_obj', 'y'}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
import static com.oracle.graal.python.nodes.BuiltinNames.TYPE;
4646
import static com.oracle.graal.python.nodes.BuiltinNames.ZIP;
4747
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
48-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
49-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
50-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REVERSED__;
5148
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
5249
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
5350
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -122,6 +119,7 @@
122119
import com.oracle.graal.python.builtins.objects.type.PythonClass;
123120
import com.oracle.graal.python.nodes.PGuards;
124121
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
122+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
125123
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
126124
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
127125
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
@@ -396,15 +394,17 @@ public PythonObject reversed(PythonClass cls, String value) {
396394

397395
@Specialization(guards = {"!isString(sequence)", "!isPRange(sequence)"})
398396
public Object reversed(PythonClass cls, Object sequence,
399-
@Cached("create()") LookupInheritedAttributeNode reversedNode,
397+
@Cached("create()") GetClassNode getClassNode,
398+
@Cached("create(__REVERSED__)") LookupAttributeInMRONode reversedNode,
400399
@Cached("create()") CallUnaryMethodNode callReversedNode,
401400
@Cached("create(__LEN__)") LookupAndCallUnaryNode lenNode,
402-
@Cached("create()") LookupInheritedAttributeNode getItemNode,
401+
@Cached("create(__GETITEM__)") LookupAttributeInMRONode getItemNode,
403402
@Cached("createBinaryProfile()") ConditionProfile noReversedProfile,
404403
@Cached("createBinaryProfile()") ConditionProfile noGetItemProfile) {
405-
Object reversed = reversedNode.execute(sequence, __REVERSED__);
404+
PythonClass sequenceKlass = getClassNode.execute(sequence);
405+
Object reversed = reversedNode.execute(sequenceKlass);
406406
if (noReversedProfile.profile(reversed == PNone.NO_VALUE)) {
407-
Object getItem = getItemNode.execute(sequence, __GETITEM__);
407+
Object getItem = getItemNode.execute(sequenceKlass);
408408
if (noGetItemProfile.profile(getItem == PNone.NO_VALUE)) {
409409
throw raise(TypeError, "'%p' object is not reversible", sequence);
410410
} else {
@@ -1122,12 +1122,12 @@ public Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] k
11221122
@TruffleBoundary
11231123
public Object type(PythonClass cls, String name, PTuple bases, PDict namespace, PKeyword[] kwds,
11241124
@Cached("create()") GetClassNode getMetaclassNode,
1125-
@Cached("create()") LookupInheritedAttributeNode getNewFuncNode,
1125+
@Cached("create(__NEW__)") LookupInheritedAttributeNode getNewFuncNode,
11261126
@Cached("create()") CallDispatchNode callNewFuncNode,
11271127
@Cached("create()") CreateArgumentsNode createArgs) {
11281128
PythonClass metaclass = calculate_metaclass(cls, bases, getMetaclassNode);
11291129
if (metaclass != cls) {
1130-
Object newFunc = getNewFuncNode.execute(metaclass, __NEW__);
1130+
Object newFunc = getNewFuncNode.execute(metaclass);
11311131
if (newFunc instanceof PBuiltinFunction && (((PBuiltinFunction) newFunc).getFunctionRootNode() == getRootNode())) {
11321132
// the new metaclass has the same __new__ function as we are in
11331133
} else {

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
5555
import static com.oracle.graal.python.nodes.BuiltinNames.__BREAKPOINT__;
5656
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
57-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
58-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__;
5957
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
6058
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
6159
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
@@ -97,7 +95,6 @@
9795
import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode;
9896
import com.oracle.graal.python.nodes.PGuards;
9997
import com.oracle.graal.python.nodes.SpecialMethodNames;
100-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
10198
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
10299
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
103100
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
@@ -262,7 +259,7 @@ public boolean callable(PythonCallable callable) {
262259

263260
@Specialization
264261
public boolean callable(Object object,
265-
@Cached("create()") LookupInheritedAttributeNode getAttributeNode) {
262+
@Cached("create(__CALL__)") LookupInheritedAttributeNode getAttributeNode) {
266263
/**
267264
* Added temporarily to skip translation/execution errors in unit testing
268265
*/
@@ -271,7 +268,7 @@ public boolean callable(Object object,
271268
return true;
272269
}
273270

274-
Object callAttr = getAttributeNode.execute(object, __CALL__);
271+
Object callAttr = getAttributeNode.execute(object);
275272
if (callAttr != NO_VALUE) {
276273
return true;
277274
}
@@ -338,10 +335,10 @@ protected boolean isPException(Object object) {
338335

339336
@Specialization(guards = "!isPException(object)")
340337
public Object hash(Object object,
341-
@Cached("create()") LookupInheritedAttributeNode lookupDirNode,
338+
@Cached("create(__DIR__)") LookupInheritedAttributeNode lookupDirNode,
342339
@Cached("create(__HASH__)") LookupAndCallUnaryNode dispatchHash,
343340
@Cached("createIfTrueNode()") CastToBooleanNode trueNode) {
344-
if (trueNode.executeWith(lookupDirNode.execute(object, __DIR__))) {
341+
if (trueNode.executeWith(lookupDirNode.execute(object))) {
345342
return dispatchHash.executeObject(object);
346343
}
347344
return object.hashCode();
@@ -568,18 +565,18 @@ public abstract static class GetAttrNode extends PythonTernaryBuiltinNode {
568565
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"name.equals(cachedName)", "isNoValue(defaultValue)"})
569566
public Object getAttrDefault(Object primary, String name, PNone defaultValue,
570567
@Cached("name") String cachedName,
571-
@Cached("create()") GetAttributeNode getter) {
572-
return getter.execute(primary, cachedName);
568+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter) {
569+
return getter.executeObject(primary, cachedName);
573570
}
574571

575572
@SuppressWarnings("unused")
576573
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"name.equals(cachedName)", "!isNoValue(defaultValue)"})
577574
public Object getAttr(Object primary, String name, Object defaultValue,
578575
@Cached("name") String cachedName,
579-
@Cached("create()") GetAttributeNode getter,
576+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
580577
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
581578
try {
582-
return getter.execute(primary, cachedName);
579+
return getter.executeObject(primary, cachedName);
583580
} catch (PException e) {
584581
e.expect(AttributeError, getCore(), errorProfile);
585582
return defaultValue;
@@ -588,16 +585,16 @@ public Object getAttr(Object primary, String name, Object defaultValue,
588585

589586
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "isNoValue(defaultValue)")
590587
public Object getAttrFromObject(Object primary, String name, @SuppressWarnings("unused") PNone defaultValue,
591-
@Cached("create()") GetAttributeNode getter) {
592-
return getter.execute(primary, name);
588+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter) {
589+
return getter.executeObject(primary, name);
593590
}
594591

595592
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "!isNoValue(defaultValue)")
596593
public Object getAttrFromObject(Object primary, String name, Object defaultValue,
597-
@Cached("create()") GetAttributeNode getter,
594+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
598595
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
599596
try {
600-
return getter.execute(primary, name);
597+
return getter.executeObject(primary, name);
601598
} catch (PException e) {
602599
e.expect(AttributeError, getCore(), errorProfile);
603600
return defaultValue;
@@ -611,13 +608,13 @@ public Object getAttr(Object object, PString name, Object defaultValue) {
611608

612609
@Specialization(guards = "!isString(name)")
613610
public Object getAttrGeneric(Object primary, Object name, Object defaultValue,
614-
@Cached("create()") GetAttributeNode getter,
611+
@Cached("create(__GETATTRIBUTE__)") LookupAndCallBinaryNode getter,
615612
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
616613
if (PGuards.isNoValue(defaultValue)) {
617-
return getter.execute(primary, name);
614+
return getter.executeObject(primary, name);
618615
} else {
619616
try {
620-
return getter.execute(primary, name);
617+
return getter.executeObject(primary, name);
621618
} catch (PException e) {
622619
e.expect(AttributeError, getCore(), errorProfile);
623620
return defaultValue;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/ManagedMethodWrappersMR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ abstract static class ExecuteNode extends Node {
7979
private CallDispatchNode getDispatchNode() {
8080
if (dispatch == null) {
8181
CompilerDirectives.transferToInterpreterAndInvalidate();
82-
dispatch = insert(CallDispatchNode.create("<foreign-invoke>"));
82+
dispatch = insert(CallDispatchNode.create());
8383
}
8484
return dispatch;
8585
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyNumberMethodsWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

4141
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
42+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4243
import com.oracle.truffle.api.interop.ForeignAccess;
4344
import com.oracle.truffle.api.interop.TruffleObject;
4445

@@ -47,14 +48,14 @@
4748
*/
4849
public class PyNumberMethodsWrapper extends PythonNativeWrapper {
4950

50-
private final Object delegate;
51+
private final PythonClass delegate;
5152

52-
public PyNumberMethodsWrapper(Object delegate) {
53+
public PyNumberMethodsWrapper(PythonClass delegate) {
5354
this.delegate = delegate;
5455
}
5556

5657
@Override
57-
public Object getDelegate() {
58+
public PythonClass getDelegate() {
5859
return delegate;
5960
}
6061

0 commit comments

Comments
 (0)