Skip to content

Commit 43bcb24

Browse files
committed
[GR-17733] custom list subclassing with list.__init__ call
PullRequest: graalpython/621
2 parents 85d3293 + f7c8e93 commit 43bcb24

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 17 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, 2019, 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
@@ -62,3 +62,19 @@ def __iter__(self):
6262

6363
l = list(ListSubclass([1, 2, 3, 4]))
6464
assert l == [10, 20, 30, 40], "was: {!s}".format(l)
65+
66+
67+
def test_list_init_call():
68+
class MyList(list):
69+
def __init__(self, a, b=None):
70+
if b:
71+
list.__init__(self, [b])
72+
else:
73+
list.__init__(self, [a])
74+
75+
l = MyList(10)
76+
assert l == [10]
77+
l = MyList(10, 20)
78+
assert l == [20]
79+
l = MyList(10, b=30)
80+
assert l == [30]

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,18 +1444,19 @@ public boolean bool(VirtualFrame frame, Object cls, Object obj,
14441444
}
14451445

14461446
// list([iterable])
1447-
@Builtin(name = LIST, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, constructsClass = PythonBuiltinClassType.PList)
1447+
@Builtin(name = LIST, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PList)
14481448
@GenerateNodeFactory
1449-
public abstract static class ListNode extends PythonBinaryBuiltinNode {
1449+
public abstract static class ListNode extends PythonVarargsBuiltinNode {
14501450
@Specialization
1451-
protected PList constructList(LazyPythonClass cls, @SuppressWarnings("unused") Object value) {
1451+
protected PList constructList(LazyPythonClass cls, @SuppressWarnings("unused") Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords) {
14521452
return factory().createList(cls);
14531453
}
14541454

14551455
@Fallback
1456-
public PList listObject(@SuppressWarnings("unused") Object cls, Object arg) {
1456+
@SuppressWarnings("unused")
1457+
public PList listObject(Object cls, Object[] arguments, PKeyword[] keywords) {
14571458
CompilerAsserts.neverPartOfCompilation();
1458-
throw new RuntimeException("list does not support iterable object " + arg);
1459+
throw raise(PythonBuiltinClassType.TypeError, "'cls' is not a type object (%p)", cls);
14591460
}
14601461
}
14611462

0 commit comments

Comments
 (0)