Skip to content

Commit bd203d1

Browse files
committed
[GR-23266] Remaining fixes for test_types
PullRequest: graalpython/1227
2 parents 4148ca1 + e5280f1 commit bd203d1

File tree

7 files changed

+84
-6
lines changed

7 files changed

+84
-6
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_new_class_with_mro_entry_multiple
1515
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_new_class_with_mro_entry_multiple_2
1616
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_new_class_with_mro_entry_none
17+
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_one_argument_type
1718
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_prepare_class
1819
*graalpython.lib-python.3.test.test_types.ClassCreationTests.test_resolve_bases
20+
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_async_def
1921
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_duck_coro
2022
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_duck_corogen
23+
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_duck_functional_gen
2124
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_duck_gen
25+
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_gen
26+
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_genfunc
2227
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_non_gen_values
28+
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_returning_itercoro
2329
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_wrapper_object
2430
*graalpython.lib-python.3.test.test_types.CoroutineTests.test_wrong_args
2531
*graalpython.lib-python.3.test.test_types.MappingProxyTests.test_chainmap
@@ -43,6 +49,7 @@
4349
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_nested
4450
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_pickle
4551
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_recursive
52+
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_recursive_repr
4653
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_repr
4754
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_subclass
4855
*graalpython.lib-python.3.test.test_types.SimpleNamespaceTests.test_unbound
@@ -57,8 +64,12 @@
5764
*graalpython.lib-python.3.test.test_types.TypesTests.test_format_spec_errors
5865
*graalpython.lib-python.3.test.test_types.TypesTests.test_int__format__
5966
*graalpython.lib-python.3.test.test_types.TypesTests.test_int__format__locale
67+
*graalpython.lib-python.3.test.test_types.TypesTests.test_internal_sizes
68+
*graalpython.lib-python.3.test.test_types.TypesTests.test_method_descriptor_types
69+
*graalpython.lib-python.3.test.test_types.TypesTests.test_method_wrapper_types
6070
*graalpython.lib-python.3.test.test_types.TypesTests.test_normal_integers
6171
*graalpython.lib-python.3.test.test_types.TypesTests.test_numeric_types
72+
*graalpython.lib-python.3.test.test_types.TypesTests.test_slot_wrapper_types
6273
*graalpython.lib-python.3.test.test_types.TypesTests.test_strings
6374
*graalpython.lib-python.3.test.test_types.TypesTests.test_truth_values
6475
*graalpython.lib-python.3.test.test_types.TypesTests.test_type_function

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static com.oracle.graal.python.nodes.BuiltinNames.ZIP;
6262
import static com.oracle.graal.python.nodes.ErrorMessages.ARG_MUST_NOT_BE_ZERO;
6363
import static com.oracle.graal.python.nodes.ErrorMessages.ERROR_CALLING_SET_NAME;
64+
import static com.oracle.graal.python.nodes.ErrorMessages.TAKES_EXACTLY_D_ARGUMENTS_D_GIVEN;
6465
import static com.oracle.graal.python.nodes.PGuards.isInteger;
6566
import static com.oracle.graal.python.nodes.PGuards.isNoValue;
6667
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__BASICSIZE__;
@@ -2135,9 +2136,14 @@ public abstract static class TypeNode extends PythonBuiltinNode {
21352136

21362137
@Specialization(guards = {"isNoValue(bases)", "isNoValue(dict)"})
21372138
@SuppressWarnings("unused")
2138-
static Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] kwds,
2139+
Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] kwds,
2140+
@Cached IsBuiltinClassProfile profile,
21392141
@Cached GetClassNode getClass) {
2140-
return getClass.execute(obj);
2142+
if (profile.profileClass(cls, PythonBuiltinClassType.PythonClass)) {
2143+
return getClass.execute(obj);
2144+
} else {
2145+
throw raise(TypeError, TAKES_EXACTLY_D_ARGUMENTS_D_GIVEN, "type.__new__", 3, 1);
2146+
}
21412147
}
21422148

21432149
@Specialization(guards = "isString(wName)")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@
3535
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FORMAT__;
3636
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
3737
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
38+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
39+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
3840
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
3941
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT_SUBCLASS__;
4042
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LE__;
44+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LT__;
4145
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NE__;
4246
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
4347
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__;
@@ -292,6 +296,19 @@ Object ne(VirtualFrame frame, Object self, Object other) {
292296
}
293297
}
294298

299+
@Builtin(name = __LT__, minNumOfPositionalArgs = 2)
300+
@Builtin(name = __LE__, minNumOfPositionalArgs = 2)
301+
@Builtin(name = __GT__, minNumOfPositionalArgs = 2)
302+
@Builtin(name = __GE__, minNumOfPositionalArgs = 2)
303+
@GenerateNodeFactory
304+
public abstract static class LtLeGtGeNode extends PythonBinaryBuiltinNode {
305+
@Specialization
306+
@SuppressWarnings("unused")
307+
Object notImplemented(Object self, Object other) {
308+
return PNotImplemented.NOT_IMPLEMENTED;
309+
}
310+
}
311+
295312
@Builtin(name = __STR__, minNumOfPositionalArgs = 1)
296313
@GenerateNodeFactory
297314
public abstract static class StrNode extends PythonUnaryBuiltinNode {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,14 @@ Object getItemsizeType(PythonBuiltinClassType cls, @SuppressWarnings("unused") P
10501050
static Object getItemsizeManaged(PythonManagedClass cls, @SuppressWarnings("unused") PNone value,
10511051
@Cached("create()") IsBuiltinClassProfile profile,
10521052
@Cached("create()") ReadAttributeFromObjectNode getName) {
1053+
Object itemsize;
10531054
// recursion anchor; since the metaclass of 'type' is 'type'
10541055
if (profile.profileClass(cls, PythonBuiltinClassType.PythonClass)) {
1055-
return getName.execute(cls, TYPE_ITEMSIZE);
1056+
itemsize = getName.execute(cls, TYPE_ITEMSIZE);
1057+
} else {
1058+
itemsize = getName.execute(cls, __ITEMSIZE__);
10561059
}
1057-
return getName.execute(cls, __ITEMSIZE__);
1060+
return itemsize != PNone.NO_VALUE ? itemsize : 0;
10581061
}
10591062

10601063
@Specialization(guards = "!isNoValue(value)")
@@ -1098,11 +1101,14 @@ Object getBasicsizeType(PythonBuiltinClassType cls, @SuppressWarnings("unused")
10981101
static Object getBasicsizeManaged(PythonManagedClass cls, @SuppressWarnings("unused") PNone value,
10991102
@Cached("create()") IsBuiltinClassProfile profile,
11001103
@Cached("create()") ReadAttributeFromObjectNode getName) {
1104+
Object basicsize;
11011105
// recursion anchor; since the metaclass of 'type' is 'type'
11021106
if (profile.profileClass(cls, PythonBuiltinClassType.PythonClass)) {
1103-
return getName.execute(cls, TYPE_BASICSIZE);
1107+
basicsize = getName.execute(cls, TYPE_BASICSIZE);
1108+
} else {
1109+
basicsize = getName.execute(cls, __BASICSIZE__);
11041110
}
1105-
return getName.execute(cls, __BASICSIZE__);
1111+
return basicsize != PNone.NO_VALUE ? basicsize : 0;
11061112
}
11071113

11081114
@Specialization(guards = "!isNoValue(value)")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public abstract class ErrorMessages {
473473
public static final String TAKES_D_POS_ARG_S_BUT_GIVEN_S = "%s() takes %d positional argument%s but %d %s given%s";
474474
public static final String TAKES_FROM_D_TO_D_POS_ARG_S_BUT_D_POS_ARG_S = "%s() takes from %d to %d positional argument%s but %d positional argument%s (and %d keyword-only argument%s) were given%s";
475475
public static final String TAKES_FROM_D_TO_D_POS_ARG_S_BUT_D_S_GIVEN_S = "%s() takes from %d to %d positional argument%s but %d %s given%s";
476+
public static final String TAKES_EXACTLY_D_ARGUMENTS_D_GIVEN = "%s() takes exactly %d arguments (%d given)";
476477
public static final String TAKES_NO_KEYWORD_ARGS = "%s takes no keyword arguments";
477478
public static final String THROW_THIRD_ARG_MUST_BE_TRACEBACK = "throw() third argument must be a traceback object";
478479
public static final String TIMEOUT_VALUE_MUST_BE_POSITIVE = "timeout value must be positive";

graalpython/lib-graalpython/_descriptor.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,39 @@ def func(self):
7575
return named_tuple
7676

7777

78+
def recursive_repr(fillfn):
79+
def inner(fn):
80+
data = None
81+
82+
def wrapper(self):
83+
nonlocal data
84+
if data is None:
85+
# lazy initialization to avoid bootstrap issues
86+
import threading
87+
data = threading.local()
88+
data.running = set()
89+
key = id(self)
90+
if key in data.running:
91+
return fillfn(self)
92+
data.running.add(key)
93+
try:
94+
result = fn(self)
95+
finally:
96+
data.running.discard(key)
97+
return result
98+
99+
wrapper.__name__ = fn.__name__
100+
wrapper.__qualname__ = fn.__qualname__
101+
return wrapper
102+
return inner
103+
104+
78105
class SimpleNamespace(object):
79106
def __init__(self, **kwargs):
80107
for k, v in kwargs.items():
81108
setattr(self, k, v)
82109

110+
@recursive_repr(lambda self: "%s(...)" % 'namespace' if type(self) is SimpleNamespace else type(self).__name__)
83111
def __repr__(self):
84112
sb = []
85113
for k, v in sorted(self.__dict__.items()):

graalpython/lib-python/3/test/test_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Python test set -- part 6, built-in types
22

33
from test.support import run_with_locale
4+
from test import support
45
import collections.abc
56
import inspect
67
import pickle
@@ -572,6 +573,7 @@ def test_format_spec_errors(self):
572573
for code in 'xXobns':
573574
self.assertRaises(ValueError, format, 0, ',' + code)
574575

576+
@support.impl_detail(graalvm=False)
575577
def test_internal_sizes(self):
576578
self.assertGreater(object.__basicsize__, 0)
577579
self.assertGreater(tuple.__itemsize__, 0)
@@ -588,6 +590,7 @@ def test_method_wrapper_types(self):
588590
self.assertIsInstance(object().__lt__, types.MethodWrapperType)
589591
self.assertIsInstance((42).__lt__, types.MethodWrapperType)
590592

593+
@support.impl_detail(graalvm=False)
591594
def test_method_descriptor_types(self):
592595
self.assertIsInstance(str.join, types.MethodDescriptorType)
593596
self.assertIsInstance(list.append, types.MethodDescriptorType)
@@ -1366,6 +1369,7 @@ def foo():
13661369
foo = types.coroutine(foo)
13671370
self.assertIs(aw, foo())
13681371

1372+
@support.impl_detail("async support", graalvm=False)
13691373
def test_async_def(self):
13701374
# Test that types.coroutine passes 'async def' coroutines
13711375
# without modification
@@ -1417,6 +1421,7 @@ def foo():
14171421
self.assertIs(foo(), coro)
14181422
self.assertIs(foo().__await__(), coro)
14191423

1424+
@support.impl_detail("async support", graalvm=False)
14201425
def test_duck_gen(self):
14211426
class GenLike:
14221427
def send(self): pass
@@ -1522,6 +1527,7 @@ def bar(): return wrapper
15221527
ref = weakref.ref(wrapper)
15231528
self.assertIs(ref(), wrapper)
15241529

1530+
@support.impl_detail("async support", graalvm=False)
15251531
def test_duck_functional_gen(self):
15261532
class Generator:
15271533
"""Emulates the following generator (very clumsy):
@@ -1573,6 +1579,7 @@ async def corofunc():
15731579
else:
15741580
self.fail('StopIteration was expected')
15751581

1582+
@support.impl_detail("async support", graalvm=False)
15761583
def test_gen(self):
15771584
def gen_func():
15781585
yield 1
@@ -1605,6 +1612,7 @@ def foo(): return gen
16051612
foo = types.coroutine(foo)
16061613
self.assertIs(foo().__await__(), gen)
16071614

1615+
@support.impl_detail("async support", graalvm=False)
16081616
def test_returning_itercoro(self):
16091617
@types.coroutine
16101618
def gen():
@@ -1622,6 +1630,7 @@ def foo():
16221630
foo = types.coroutine(foo)
16231631
self.assertIs(foo(), gencoro)
16241632

1633+
@support.impl_detail("async support", graalvm=False)
16251634
def test_genfunc(self):
16261635
def gen(): yield
16271636
self.assertIs(types.coroutine(gen), gen)

0 commit comments

Comments
 (0)