Skip to content

Commit fbfa011

Browse files
committed
[GR-38686] Add sq_length slot wrapper
PullRequest: graalpython/2255
2 parents db9eaf6 + e0a47b7 commit fbfa011

File tree

9 files changed

+75
-18
lines changed

9 files changed

+75
-18
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
from collections import namedtuple
10-
import hashlib
1110
import marshal
1211
import ntpath
1312
import os
@@ -16,6 +15,9 @@
1615
import textwrap
1716
import shutil
1817

18+
from _sha256 import sha256
19+
20+
1921
FROZEN_ONLY = os.path.join(os.path.dirname(__file__), "flag.py")
2022

2123
# These are modules that get frozen.
@@ -369,7 +371,7 @@ def _iter_sources(modules):
369371
def _get_checksum(filename):
370372
with open(filename, "rb") as infile:
371373
contents = infile.read()
372-
m = hashlib.sha256()
374+
m = sha256()
373375
m.update(contents)
374376
return m.hexdigest()
375377

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ Object doNativeObjectIndirect(VirtualFrame frame, PythonManagedClass self, Objec
15931593
throw getReportAbstractClassNode().execute(frame, self);
15941594
}
15951595
Object nativeBaseClass = findFirstNativeBaseClass(getMroNode.execute(self));
1596-
return callNativeGenericNewNode(nativeBaseClass, varargs, kwargs);
1596+
return callNativeGenericNewNode(self, nativeBaseClass, varargs, kwargs);
15971597
}
15981598

15991599
@Specialization(guards = "isNativeClass(self)")
@@ -1603,7 +1603,7 @@ Object doNativeObjectDirect(VirtualFrame frame, Object self, Object[] varargs, P
16031603
if ((getTypeFlagsNode.execute(self) & TypeFlags.IS_ABSTRACT) != 0) {
16041604
throw getReportAbstractClassNode().execute(frame, self);
16051605
}
1606-
return callNativeGenericNewNode(self, varargs, kwargs);
1606+
return callNativeGenericNewNode(self, self, varargs, kwargs);
16071607
}
16081608

16091609
@SuppressWarnings("unused")
@@ -1622,7 +1622,7 @@ private static Object findFirstNativeBaseClass(PythonAbstractClass[] methodResol
16221622
throw new IllegalStateException("class needs native allocation but has not native base class");
16231623
}
16241624

1625-
private Object callNativeGenericNewNode(Object self, Object[] varargs, PKeyword[] kwargs) {
1625+
private Object callNativeGenericNewNode(Object type, Object nativeBase, Object[] varargs, PKeyword[] kwargs) {
16261626
if (callCapiFunction == null) {
16271627
CompilerDirectives.transferToInterpreterAndInvalidate();
16281628
callCapiFunction = insert(PCallCapiFunction.create());
@@ -1643,7 +1643,7 @@ private Object callNativeGenericNewNode(Object self, Object[] varargs, PKeyword[
16431643
PTuple targs = factory().createTuple(varargs);
16441644
PDict dkwargs = factory().createDict(kwarr);
16451645
return asPythonObjectNode.execute(
1646-
callCapiFunction.call(FUN_PY_OBJECT_NEW, toSulongNodes[0].execute(self), toSulongNodes[1].execute(self), toSulongNodes[2].execute(targs),
1646+
callCapiFunction.call(FUN_PY_OBJECT_NEW, toSulongNodes[0].execute(type), toSulongNodes[1].execute(nativeBase), toSulongNodes[2].execute(targs),
16471647
toSulongNodes[3].execute(dkwargs)));
16481648
}
16491649

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/builtins/objects/function/PBuiltinFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -27,6 +27,7 @@
2727

2828
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
2929

30+
import java.lang.invoke.VarHandle;
3031
import java.util.Arrays;
3132

3233
import com.oracle.graal.python.PythonLanguage;
@@ -50,7 +51,6 @@
5051
import com.oracle.truffle.api.interop.InteropLibrary;
5152
import com.oracle.truffle.api.library.ExportLibrary;
5253
import com.oracle.truffle.api.library.ExportMessage;
53-
import com.oracle.truffle.api.memory.MemoryFence;
5454
import com.oracle.truffle.api.nodes.RootNode;
5555
import com.oracle.truffle.api.object.Shape;
5656

@@ -235,7 +235,7 @@ public void setDescriptor(BuiltinMethodDescriptor value) {
235235
assert value.getName().equals(getName()) && getBuiltinNodeFactory() == value.getFactory() : getName() + " vs " + value;
236236
// Only make sure that info is fully initialized, otherwise it is fine if it is set multiple
237237
// times from different threads, all of them should set the same value
238-
MemoryFence.storeStore();
238+
VarHandle.storeStoreFence();
239239
BuiltinMethodDescriptor local = descriptor;
240240
assert local == null || local == value : value;
241241
this.descriptor = value;

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 {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.InputStream;
3838
import java.io.OutputStream;
3939
import java.io.PrintWriter;
40+
import java.lang.invoke.VarHandle;
4041
import java.lang.ref.WeakReference;
4142
import java.nio.file.LinkOption;
4243
import java.security.NoSuchAlgorithmException;
@@ -63,7 +64,6 @@
6364
import java.util.concurrent.locks.ReentrantLock;
6465
import java.util.logging.Level;
6566

66-
import com.oracle.graal.python.builtins.objects.function.PFunction;
6767
import org.graalvm.nativeimage.ImageInfo;
6868
import org.graalvm.options.OptionKey;
6969

@@ -91,6 +91,7 @@
9191
import com.oracle.graal.python.builtins.objects.dict.PDict;
9292
import com.oracle.graal.python.builtins.objects.frame.PFrame;
9393
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
94+
import com.oracle.graal.python.builtins.objects.function.PFunction;
9495
import com.oracle.graal.python.builtins.objects.function.PKeyword;
9596
import com.oracle.graal.python.builtins.objects.list.PList;
9697
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -138,7 +139,6 @@
138139
import com.oracle.truffle.api.interop.ExceptionType;
139140
import com.oracle.truffle.api.interop.InteropLibrary;
140141
import com.oracle.truffle.api.interop.UnsupportedMessageException;
141-
import com.oracle.truffle.api.memory.MemoryFence;
142142
import com.oracle.truffle.api.nodes.LanguageInfo;
143143
import com.oracle.truffle.api.nodes.Node;
144144
import com.oracle.truffle.api.source.Source;
@@ -1827,7 +1827,7 @@ public void setSingletonNativeWrapper(PythonAbstractObject obj, PythonNativeWrap
18271827
assert PythonLanguage.getSingletonNativeWrapperIdx(obj) != -1 : "invalid special singleton object";
18281828
assert singletonNativePtrs[PythonLanguage.getSingletonNativeWrapperIdx(obj)] == null;
18291829
// Other threads must see the nativeWrapper fully initialized once it becomes non-null
1830-
MemoryFence.storeStore();
1830+
VarHandle.storeStoreFence();
18311831
singletonNativePtrs[PythonLanguage.getSingletonNativeWrapperIdx(obj)] = nativePtr;
18321832
}
18331833

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/PException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.truffle.api.dsl.Cached;
5858
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5959
import com.oracle.truffle.api.exception.AbstractTruffleException;
60+
import com.oracle.truffle.api.frame.FrameInstance;
6061
import com.oracle.truffle.api.frame.VirtualFrame;
6162
import com.oracle.truffle.api.interop.ExceptionType;
6263
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -238,7 +239,7 @@ public void expect(PythonBuiltinClassType error, IsBuiltinClassProfile profile)
238239
@TruffleBoundary
239240
public Iterable<TruffleStackTraceElement> getTruffleStackTrace() {
240241
if (tracebackCutoffTarget == null) {
241-
tracebackCutoffTarget = Truffle.getRuntime().getCurrentFrame().getCallTarget();
242+
tracebackCutoffTarget = Truffle.getRuntime().iterateFrames(FrameInstance::getCallTarget, 0);
242243
}
243244
// Cause may contain wrapped Java exception
244245
if (getCause() != null) {

0 commit comments

Comments
 (0)