Skip to content

Commit 311568b

Browse files
committed
[GR-23277] get test_call to pass.
PullRequest: graalpython/1190
2 parents 1349c7f + 5453a81 commit 311568b

File tree

11 files changed

+303
-82
lines changed

11 files changed

+303
-82
lines changed

graalpython/com.oracle.graal.python.cext/src/functionobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,7 +40,7 @@
4040
*/
4141
#include "capi.h"
4242

43-
PyTypeObject PyFunction_Type = PY_TRUFFLE_TYPE("function", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyFunctionObject));
43+
PyTypeObject PyFunction_Type = PY_TRUFFLE_TYPE("function", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_METHOD_DESCRIPTOR, sizeof(PyFunctionObject));
4444

4545
PyObject* PyClassMethod_New(PyObject* method) {
4646
return UPCALL_O(PY_BUILTIN, polyglot_from_string("classmethod", SRC_CS), native_to_java(method));

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40+
import sys
41+
from unittest import skipIf
42+
43+
4044
def assert_raises(err, fn, *args, **kwargs):
4145
raised = False
4246
try:
@@ -45,6 +49,7 @@ def assert_raises(err, fn, *args, **kwargs):
4549
raised = True
4650
assert raised
4751

52+
4853
def test_base():
4954
A = type('A', (), {})
5055
assert A.__base__ == object
@@ -181,3 +186,69 @@ class C:
181186
def test_isinstance_non_type():
182187
import typing
183188
assert isinstance(1, typing.AbstractSet) is False
189+
190+
191+
@skipIf(sys.implementation.name == 'cpython' and sys.version_info[0:2] < (3, 8), "skipping for cPython versions < 3.8")
192+
def test_flags():
193+
import functools
194+
195+
def testfunction(self):
196+
"""some doc"""
197+
return self
198+
199+
TPFLAGS_METHOD_DESCRIPTOR = 1 << 17
200+
TPFLAGS_LONG_SUBCLASS = 1 << 24
201+
TPFLAGS_LIST_SUBCLASS = 1 << 25
202+
TPFLAGS_TUPLE_SUBCLASS = 1 << 26
203+
TPFLAGS_BYTES_SUBCLASS = 1 << 27
204+
TPFLAGS_UNICODE_SUBCLASS = 1 << 28
205+
TPFLAGS_DICT_SUBCLASS = 1 << 29
206+
TPFLAGS_BASE_EXC_SUBCLASS = 1 << 30
207+
TPFLAGS_TYPE_SUBCLASS = 1 << 31
208+
209+
cached = functools.lru_cache(1)(testfunction)
210+
211+
assert not type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, "masked __flags__ = {}, expected {}".format(type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, 0)
212+
assert type(list.append).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, "masked __flags__ = {}, expected {}".format(type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, TPFLAGS_METHOD_DESCRIPTOR)
213+
assert type(list.__add__).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, "masked __flags__ = {}, expected {}".format(type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, TPFLAGS_METHOD_DESCRIPTOR)
214+
assert type(testfunction).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, "masked __flags__ = {}, expected {}".format(type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, TPFLAGS_METHOD_DESCRIPTOR)
215+
assert type(cached).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, "masked __flags__ = {}, expected {}".format(type(repr).__flags__ & TPFLAGS_METHOD_DESCRIPTOR, TPFLAGS_METHOD_DESCRIPTOR)
216+
217+
class MyInt(int):
218+
pass
219+
220+
class MyList(list):
221+
pass
222+
223+
class MyTuple(tuple):
224+
pass
225+
226+
class MyBytes(bytes):
227+
pass
228+
229+
class MyStr(str):
230+
pass
231+
232+
class MyDict(dict):
233+
pass
234+
235+
for x, flag in [
236+
(1, TPFLAGS_LONG_SUBCLASS),
237+
(MyInt(1), TPFLAGS_LONG_SUBCLASS),
238+
239+
([1,2], TPFLAGS_LIST_SUBCLASS),
240+
(MyList([1,2]), TPFLAGS_LIST_SUBCLASS),
241+
242+
((1,2), TPFLAGS_TUPLE_SUBCLASS),
243+
(MyTuple((1,2)), TPFLAGS_TUPLE_SUBCLASS),
244+
245+
(b"123", TPFLAGS_BYTES_SUBCLASS),
246+
(MyBytes(b"123"), TPFLAGS_BYTES_SUBCLASS),
247+
248+
("123", TPFLAGS_UNICODE_SUBCLASS),
249+
(MyStr("123"), TPFLAGS_UNICODE_SUBCLASS),
250+
251+
({"1":1, "2": 2}, TPFLAGS_DICT_SUBCLASS),
252+
(MyDict({"1":1, "2": 2}), TPFLAGS_DICT_SUBCLASS),
253+
]:
254+
assert type(x).__flags__ & flag, "masked __flags__ = {}, expected {}".format(type(x).__flags__ & flag, flag)

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_call.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@
6161
*graalpython.lib-python.3.test.test_call.FastCallTests.test_fastcall_clearing_dict
6262
*graalpython.lib-python.3.test.test_call.FastCallTests.test_vectorcall
6363
*graalpython.lib-python.3.test.test_call.FastCallTests.test_vectorcall_dict
64+
*graalpython.lib-python.3.test.test_call.FunctionCalls.test_kwargs_order
65+
*graalpython.lib-python.3.test.test_call.TestPEP590.test_method_descriptor_flag
66+
*graalpython.lib-python.3.test.test_call.TestPEP590.test_vectorcall
67+
*graalpython.lib-python.3.test.test_call.TestPEP590.test_vectorcall_flag
68+
*graalpython.lib-python.3.test.test_call.TestPEP590.test_vectorcall_override

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ public void postInitialize(PythonCore core) {
150150
mod.setAttribute("stdlib_home", stdlibHome);
151151
mod.setAttribute("capi_home", capiHome);
152152
mod.setAttribute("platform_id", toolchain.getIdentifier());
153+
mod.setAttribute("flags", core.factory().createTuple(new Object[]{
154+
false, // bytes_warning
155+
!context.getOption(PythonOptions.PythonOptimizeFlag), // debug
156+
context.getOption(PythonOptions.DontWriteBytecodeFlag), // dont_write_bytecode
157+
false, // hash_randomization
158+
context.getOption(PythonOptions.IgnoreEnvironmentFlag), // ignore_environment
159+
context.getOption(PythonOptions.InspectFlag), // inspect
160+
context.getOption(PythonOptions.TerminalIsInteractive), // interactive
161+
context.getOption(PythonOptions.IsolateFlag), // isolated
162+
context.getOption(PythonOptions.NoSiteFlag), // no_site
163+
context.getOption(PythonOptions.NoUserSiteFlag), // no_user_site
164+
context.getOption(PythonOptions.PythonOptimizeFlag), // optimize
165+
context.getOption(PythonOptions.QuietFlag), // quiet
166+
context.getOption(PythonOptions.VerboseFlag), // verbose
167+
false, // dev_mode
168+
0, // utf8_mode
169+
}));
153170
}
154171

155172
@Builtin(name = "cache_module_code", minNumOfPositionalArgs = 3)

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,6 @@ public void postInitialize(PythonCore core) {
221221
Object[] warnoptions = new Object[strWarnoptions.length];
222222
System.arraycopy(strWarnoptions, 0, warnoptions, 0, strWarnoptions.length);
223223
sys.setAttribute("warnoptions", core.factory().createList(warnoptions));
224-
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
225-
false, // bytes_warning
226-
!context.getOption(PythonOptions.PythonOptimizeFlag), // debug
227-
context.getOption(PythonOptions.DontWriteBytecodeFlag), // dont_write_bytecode
228-
false, // hash_randomization
229-
context.getOption(PythonOptions.IgnoreEnvironmentFlag), // ignore_environment
230-
context.getOption(PythonOptions.InspectFlag), // inspect
231-
context.getOption(PythonOptions.TerminalIsInteractive), // interactive
232-
context.getOption(PythonOptions.IsolateFlag), // isolated
233-
context.getOption(PythonOptions.NoSiteFlag), // no_site
234-
context.getOption(PythonOptions.NoUserSiteFlag), // no_user_site
235-
context.getOption(PythonOptions.PythonOptimizeFlag), // optimize
236-
context.getOption(PythonOptions.QuietFlag), // quiet
237-
context.getOption(PythonOptions.VerboseFlag), // verbose
238-
false, // dev_mode
239-
0, // utf8_mode
240-
}));
241224

242225
Env env = context.getEnv();
243226
String option = context.getOption(PythonOptions.PythonPath);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/LocalsStorage.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.common;
4242

43+
import static com.oracle.graal.python.nodes.frame.FrameSlotIDs.isUserFrameSlot;
44+
4345
import java.util.Iterator;
4446
import java.util.List;
4547
import java.util.NoSuchElementException;
@@ -51,7 +53,6 @@
5153
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5254
import com.oracle.graal.python.builtins.objects.str.PString;
5355
import com.oracle.graal.python.nodes.PGuards;
54-
import com.oracle.graal.python.nodes.frame.FrameSlotIDs;
5556
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5657
import com.oracle.truffle.api.CompilerDirectives;
5758
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -117,7 +118,7 @@ private void calculateLength() {
117118
this.len = this.frame.getFrameDescriptor().getSize();
118119
for (FrameSlot slot : this.frame.getFrameDescriptor().getSlots()) {
119120
Object identifier = slot.getIdentifier();
120-
if (!FrameSlotIDs.isUserFrameSlot(identifier) || getValue(frame, slot) == null) {
121+
if (!isUserFrameSlot(identifier) || getValue(frame, slot) == null) {
121122
this.len--;
122123
}
123124
}
@@ -137,7 +138,7 @@ static Object getItemCached(LocalsStorage self, String key, ThreadState state,
137138

138139
@Specialization(replaces = "getItemCached")
139140
static Object string(LocalsStorage self, String key, ThreadState state) {
140-
if (!FrameSlotIDs.isUserFrameSlot(key)) {
141+
if (!isUserFrameSlot(key)) {
141142
return null;
142143
}
143144
FrameSlot slot = findSlot(self, key);
@@ -219,9 +220,14 @@ public Object forEachUntyped(ForEachNode<Object> node, Object arg) {
219220
bailout();
220221
Object result = arg;
221222
for (FrameSlot slot : this.frame.getFrameDescriptor().getSlots()) {
222-
Object value = getValue(slot);
223-
if (value != null) {
224-
result = node.execute(slot.getIdentifier(), result);
223+
Object identifier = slot.getIdentifier();
224+
if (identifier instanceof String) {
225+
if (isUserFrameSlot(identifier)) {
226+
Object value = getValue(slot);
227+
if (value != null) {
228+
result = node.execute(identifier, result);
229+
}
230+
}
225231
}
226232
}
227233
return result;
@@ -372,7 +378,7 @@ protected boolean loadNext() {
372378
FrameSlot nextCandidate = this.slots.get(this.index++);
373379
Object identifier = nextCandidate.getIdentifier();
374380
if (identifier instanceof String) {
375-
if (FrameSlotIDs.isUserFrameSlot(identifier)) {
381+
if (isUserFrameSlot(identifier)) {
376382
Object nextValue = getValue(this.frame, nextCandidate);
377383
if (nextValue != null) {
378384
this.nextFrameSlot = nextCandidate;
@@ -399,7 +405,7 @@ protected boolean loadNext() {
399405
FrameSlot nextCandidate = this.slots.get(this.index--);
400406
Object identifier = nextCandidate.getIdentifier();
401407
if (identifier instanceof String) {
402-
if (FrameSlotIDs.isUserFrameSlot(identifier)) {
408+
if (isUserFrameSlot(identifier)) {
403409
Object nextValue = getValue(this.frame, nextCandidate);
404410
if (nextValue != null) {
405411
this.nextFrameSlot = nextCandidate;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ static long doBuiltinClassType(PythonBuiltinClassType clazz) {
177177
case PBytes:
178178
result = DEFAULT | BASETYPE | BYTES_SUBCLASS;
179179
break;
180+
case PFunction:
180181
case PBuiltinFunction:
181182
result = DEFAULT | HAVE_GC | METHOD_DESCRIPTOR;
182183
break;
183-
case PFunction:
184184
case PMethod:
185185
case PBuiltinMethod:
186186
case GetSetDescriptor:

0 commit comments

Comments
 (0)