Skip to content

Commit 06170ef

Browse files
committed
[GR-18235] [GR-18238] Call __index__ in appropriate conversion functions
PullRequest: graalpython/882
2 parents 93001ef + 64ba570 commit 06170ef

File tree

12 files changed

+319
-374
lines changed

12 files changed

+319
-374
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_abstract.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -132,6 +132,12 @@ def __int__(self):
132132
return 0xCAFE
133133

134134

135+
class DummyIndexable():
136+
137+
def __index__(self):
138+
return 0xCAFE
139+
140+
135141
class DummyIntSubclass(int):
136142

137143
def __int__(self):
@@ -201,6 +207,7 @@ def _default_unarop_args():
201207
(0xffffffffffffffffffffffffffffffff,),
202208
(DummyIntable(),),
203209
(DummyIntSubclass(),),
210+
(DummyIndexable(), ),
204211
(NoNumber(),),
205212
(DummyFloatable(),),
206213
(DummyFloatSubclass(),),

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -103,6 +103,15 @@ def __int__(self):
103103
return 0xBABE
104104

105105

106+
class DummyIndexable:
107+
108+
def __int__(self):
109+
return 0xDEAD
110+
111+
def __index__(self):
112+
return 0xBEEF
113+
114+
106115
class TestPyLong(CPyExtTestCase):
107116

108117
def compile_module(self, name):
@@ -119,6 +128,7 @@ def compile_module(self, name):
119128
(DummyIntable(), 0xCAFE),
120129
(DummyIntSubclass(), 0xBABE),
121130
(DummyNonInt(), -1),
131+
(DummyIndexable(), 0xBEEF if sys.version_info >= (3, 8, 0) else 0xDEAD),
122132
),
123133
code='''int wrap_PyLong_AsLong(PyObject* obj, long expected) {
124134
long res = PyLong_AsLong(obj);

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_modsupport.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -54,6 +54,14 @@ def _reference_typecheck(args, expected_type):
5454
return args[0][0]
5555

5656

57+
class Indexable:
58+
def __int__(self):
59+
return 456
60+
61+
def __index__(self):
62+
return 123
63+
64+
5765
class TestModsupport(CPyExtTestCase):
5866
def compile_module(self, name):
5967
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
@@ -206,6 +214,7 @@ def compile_module(self, name):
206214
((0xFFFFFFFFFFFFFFFF, ), 0xFFFFFFFF),
207215
# will be a big one
208216
((0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, ), 0xFFFFFFFF),
217+
((Indexable(), ), 123 if sys.version_info > (3, 8, 0) else 456),
209218
),
210219
code='''
211220
static unsigned int wrap_PyArg_ParseTuple(PyObject* argTuple, PyObject* expected) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39-
39+
import sys
4040
import unittest
4141
from math import atan2
4242

@@ -143,6 +143,14 @@ class CP2(complex):
143143
def __complex__(self):
144144
return 42j
145145

146+
class CP3:
147+
def __float__(self):
148+
return 6.0
149+
150+
class CP4:
151+
def __index__(self):
152+
return 123
153+
146154
assert CP1(1+4j) == complex(1, 4)
147155

148156
c = CP2(1+4j)
@@ -240,6 +248,9 @@ def __complex__(self):
240248
assert c == complex(-7, 49)
241249
assert type(c) == CP2
242250

251+
assert complex(CP3()) == complex(6.0)
252+
if sys.version_info >= (3, 8, 0):
253+
assert complex(CP4()) == complex(123)
243254

244255
class ComplexTest(unittest.TestCase):
245256

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -99,7 +99,12 @@ def test_create(self):
9999
class Obj:
100100
def __float__(self):
101101
return 1.123
102+
class Indexable:
103+
def __index__(self):
104+
return 4
102105
assert [float(x) for x in [Obj(), b"0.123"]] == [1.123, 0.123]
106+
if sys.version_info >= (3, 8, 0):
107+
assert float(Indexable()) == 4.0
103108

104109
assert float(99) == 99
105110
assert float(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999) == 1e+132

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -95,6 +95,10 @@ class SubInt(int):
9595
def __int__(self):
9696
return 0xBADF00D
9797

98+
class Indexable:
99+
def __index__(self):
100+
return 4
101+
98102
class NoInt():
99103
pass
100104

@@ -108,6 +112,9 @@ class NoInt():
108112
assert False, "converting non-integer to integer must not be possible"
109113
except BaseException as e:
110114
assert type(e) == TypeError, "expected type error, was: %r" % type(e)
115+
if sys.version_info >= (3, 8, 0):
116+
assert int(Indexable()) == 4
117+
111118

112119
def test_int_bit_length():
113120
assert (int(0)).bit_length() == 0

0 commit comments

Comments
 (0)