Skip to content

Commit e0a47b7

Browse files
committed
Add sq_length wrapper
1 parent fb7f017 commit e0a47b7

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public enum NativeMember {
113113
SQ_ITEM("sq_item"),
114114
SQ_REPEAT("sq_repeat"),
115115
SQ_CONCAT("sq_concat"),
116+
SQ_LENGTH("sq_length"),
116117

117118
// PyDictObject
118119
MA_USED("ma_used", PRIMITIVE),

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -49,13 +49,19 @@
4949
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PAsPointerNode;
5050
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.ToPyObjectNode;
5151
import com.oracle.graal.python.builtins.objects.function.PKeyword;
52+
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
53+
import com.oracle.graal.python.lib.PyNumberIndexNode;
54+
import com.oracle.graal.python.lib.PyObjectSizeNode;
55+
import com.oracle.graal.python.nodes.PRaiseNode;
5256
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
5357
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
5458
import com.oracle.graal.python.nodes.argument.positional.PositionalArgumentsNode;
5559
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
5660
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
61+
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
5762
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
5863
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
64+
import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
5965
import com.oracle.graal.python.runtime.GilNode;
6066
import com.oracle.graal.python.runtime.PythonContext;
6167
import com.oracle.graal.python.runtime.exception.PException;
@@ -337,6 +343,45 @@ protected Object execute(Object[] arguments,
337343
}
338344
}
339345

346+
@ExportLibrary(InteropLibrary.class)
347+
static class LenfuncWrapper extends PyProcsWrapper {
348+
349+
public LenfuncWrapper(Object delegate) {
350+
super(delegate);
351+
}
352+
353+
@ExportMessage
354+
protected Object execute(Object[] arguments,
355+
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
356+
@Cached CallUnaryMethodNode executeNode,
357+
@Cached ToJavaNode toJavaNode,
358+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
359+
@Cached ToSulongNode nullToSulongNode,
360+
@Cached PyNumberIndexNode indexNode,
361+
@Cached CastToJavaIntLossyNode castLossy,
362+
@Cached PyNumberAsSizeNode asSizeNode,
363+
@Cached PRaiseNode raiseNode,
364+
@Exclusive @Cached GilNode gil) throws ArityException {
365+
boolean mustRelease = gil.acquire();
366+
try {
367+
if (arguments.length != 1) {
368+
CompilerDirectives.transferToInterpreterAndInvalidate();
369+
throw ArityException.create(1, 1, arguments.length);
370+
}
371+
try {
372+
Object result = executeNode.executeObject(null, lib.getDelegate(this), toJavaNode.execute(arguments[0]));
373+
int len = PyObjectSizeNode.convertAndCheckLen(null, result, indexNode, castLossy, asSizeNode, raiseNode);
374+
return (long) len;
375+
} catch (PException e) {
376+
transformExceptionToNativeNode.execute(null, e);
377+
return nullToSulongNode.execute(PythonContext.get(nullToSulongNode).getNativeNull());
378+
}
379+
} finally {
380+
gil.release(mustRelease);
381+
}
382+
}
383+
}
384+
340385
public static GetAttrWrapper createGetAttrWrapper(Object getAttrMethod) {
341386
return new GetAttrWrapper(getAttrMethod);
342387
}
@@ -359,4 +404,8 @@ public static TernaryFunctionWrapper createTernaryFunctionWrapper(Object setTern
359404
public static SsizeargfuncWrapper createSsizeargfuncWrapper(Object ssizeArgMethod, boolean newRef) {
360405
return new SsizeargfuncWrapper(ssizeArgMethod, newRef);
361406
}
407+
408+
public static LenfuncWrapper createLenfuncWrapper(Object lenfuncMethod) {
409+
return new LenfuncWrapper(lenfuncMethod);
410+
}
362411
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -42,9 +42,11 @@
4242

4343
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.SQ_CONCAT;
4444
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.SQ_ITEM;
45+
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.SQ_LENGTH;
4546
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeMember.SQ_REPEAT;
4647
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
4748
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
49+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
4850
import static com.oracle.graal.python.nodes.SpecialMethodNames.__MUL__;
4951

5052
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode;
@@ -90,7 +92,7 @@ protected boolean hasMembers() {
9092

9193
@ExportMessage
9294
protected boolean isMemberReadable(String member) {
93-
return SQ_REPEAT.getMemberName().equals(member) || SQ_ITEM.getMemberName().equals(member) || SQ_CONCAT.getMemberName().equals(member);
95+
return SQ_REPEAT.getMemberName().equals(member) || SQ_ITEM.getMemberName().equals(member) || SQ_CONCAT.getMemberName().equals(member) || SQ_LENGTH.getMemberName().equals(member);
9496
}
9597

9698
@ExportMessage
@@ -116,6 +118,8 @@ protected Object readMember(String member,
116118
return PyProcsWrapper.createSsizeargfuncWrapper(lookup.execute(getPythonClass(lib), __GETITEM__), true);
117119
} else if (SQ_CONCAT.getMemberName().equals(member)) {
118120
result = toSulongNode.execute(lookup.execute(getPythonClass(lib), __ADD__));
121+
} else if (SQ_LENGTH.getMemberName().equals(member)) {
122+
result = PyProcsWrapper.createLenfuncWrapper(lookup.execute(getPythonClass(lib), __LEN__));
119123
} else {
120124
// TODO extend list
121125
throw UnknownIdentifierException.create(member);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectSizeNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, 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
@@ -171,7 +171,7 @@ static int checkLen(PRaiseNode raiseNode, int len) {
171171
return len;
172172
}
173173

174-
static int convertAndCheckLen(VirtualFrame frame, Object result, PyNumberIndexNode indexNode, CastToJavaIntLossyNode castLossy, PyNumberAsSizeNode asSizeNode, PRaiseNode raiseNode) {
174+
public static int convertAndCheckLen(VirtualFrame frame, Object result, PyNumberIndexNode indexNode, CastToJavaIntLossyNode castLossy, PyNumberAsSizeNode asSizeNode, PRaiseNode raiseNode) {
175175
int len;
176176
Object index = indexNode.execute(frame, result);
177177
try {

0 commit comments

Comments
 (0)