Skip to content

Commit 7a34b87

Browse files
committed
[GR-40247] Smaller fixes in preparation of C API impl.
PullRequest: graalpython/2395
2 parents 748e1cd + d2e330c commit 7a34b87

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def compile_module(self, name):
876876
("foo.bar.baz", 7, 0),
877877
),
878878
resultspec="O",
879-
argspec='OOi',
879+
argspec='OOn',
880880
arguments=["PyObject* string", "PyObject* sep", "Py_ssize_t maxsplit"],
881881
cmpfunc=unhandled_error_compare
882882
)

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_PY_BUFFER_TYPEID;
4444
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_PY_TRUFFLE_LONG_ARRAY_TO_NATIVE;
4545

46+
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
4647
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4748
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
4849
import com.oracle.graal.python.builtins.objects.object.PythonObject;
@@ -170,7 +171,7 @@ static Object getBufManaged(PMemoryView object, @SuppressWarnings("unused") Stri
170171
@Cached PySequenceArrayWrapper.ToNativeStorageNode toNativeStorageNode) {
171172
// TODO GR-21120: Add support for PArray
172173
PSequence owner = (PSequence) object.getOwner();
173-
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(owner));
174+
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(owner), owner instanceof PBytesLike);
174175
if (nativeStorage == null) {
175176
throw CompilerDirectives.shouldNotReachHere("cannot allocate native storage");
176177
}
@@ -236,14 +237,24 @@ static Object getFormat(PMemoryView object, @SuppressWarnings("unused") String k
236237

237238
@Specialization(guards = {"eq(J_SHAPE, key)"})
238239
static Object getShape(PMemoryView object, @SuppressWarnings("unused") String key,
240+
@Shared("toSulong") @Cached CExtNodes.ToSulongNode toSulongNode,
239241
@Shared("toArray") @Cached IntArrayToNativePySSizeArray intArrayToNativePySSizeArray) {
240-
return intArrayToNativePySSizeArray.execute(object.getBufferShape());
242+
int[] shape = object.getBufferShape();
243+
if (shape == null) {
244+
return toSulongNode.execute(PythonContext.get(toSulongNode).getNativeNull());
245+
}
246+
return intArrayToNativePySSizeArray.execute(shape);
241247
}
242248

243249
@Specialization(guards = {"eq(J_STRIDES, key)"})
244250
static Object getStrides(PMemoryView object, @SuppressWarnings("unused") String key,
251+
@Shared("toSulong") @Cached CExtNodes.ToSulongNode toSulongNode,
245252
@Shared("toArray") @Cached IntArrayToNativePySSizeArray intArrayToNativePySSizeArray) {
246-
return intArrayToNativePySSizeArray.execute(object.getBufferStrides());
253+
int[] strides = object.getBufferStrides();
254+
if (strides == null) {
255+
return toSulongNode.execute(PythonContext.get(toSulongNode).getNativeNull());
256+
}
257+
return intArrayToNativePySSizeArray.execute(strides);
247258
}
248259

249260
@Specialization(guards = {"eq(J_SUBOFFSETS, key)"})

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.graal.python.util.PythonUtils;
7676
import com.oracle.truffle.api.CompilerAsserts;
7777
import com.oracle.truffle.api.CompilerDirectives;
78+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7879
import com.oracle.truffle.api.dsl.Cached;
7980
import com.oracle.truffle.api.dsl.Cached.Exclusive;
8081
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -397,7 +398,7 @@ static Object doPSequence(PySequenceArrayWrapper object,
397398
@CachedLibrary(limit = "3") PythonNativeWrapperLibrary lib,
398399
@Exclusive @Cached ToNativeStorageNode toNativeStorageNode) {
399400
PSequence sequence = (PSequence) lib.getDelegate(object);
400-
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(sequence));
401+
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(sequence), sequence instanceof PBytesLike);
401402
if (nativeStorage == null) {
402403
CompilerDirectives.transferToInterpreter();
403404
throw new IllegalStateException("could not allocate native storage");
@@ -423,22 +424,38 @@ protected static boolean isPSequence(Object obj) {
423424
@GenerateUncached
424425
abstract static class ToNativeStorageNode extends Node {
425426

426-
public abstract NativeSequenceStorage execute(SequenceStorage object);
427+
public abstract NativeSequenceStorage execute(SequenceStorage object, boolean isBytesLike);
427428

428-
@Specialization(guards = "!isNative(s)")
429-
static NativeSequenceStorage doManaged(SequenceStorage s,
429+
public static boolean isEmptySequenceStorage(SequenceStorage s) {
430+
return s instanceof EmptySequenceStorage;
431+
}
432+
433+
@Specialization(guards = {"!isNative(s)", "!isEmptySequenceStorage(s)"})
434+
static NativeSequenceStorage doManaged(SequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike,
435+
@Cached ConditionProfile isObjectArrayProfile,
430436
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode,
431437
@Cached SequenceStorageNodes.GetInternalArrayNode getInternalArrayNode) {
432-
return storageToNativeNode.execute(getInternalArrayNode.execute(s));
438+
Object array = getInternalArrayNode.execute(s);
439+
if (isBytesLike) {
440+
assert array instanceof byte[];
441+
} else if (!isObjectArrayProfile.profile(array instanceof Object[])) {
442+
array = generalize(s);
443+
}
444+
return storageToNativeNode.execute(array);
445+
}
446+
447+
@TruffleBoundary
448+
private static Object generalize(SequenceStorage s) {
449+
return s.getInternalArray();
433450
}
434451

435452
@Specialization
436-
static NativeSequenceStorage doNative(NativeSequenceStorage s) {
453+
static NativeSequenceStorage doNative(NativeSequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike) {
437454
return s;
438455
}
439456

440457
@Specialization
441-
static NativeSequenceStorage doEmptyStorage(@SuppressWarnings("unused") EmptySequenceStorage s,
458+
static NativeSequenceStorage doEmptyStorage(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike,
442459
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode) {
443460
// TODO(fa): not sure if that completely reflects semantics
444461
return storageToNativeNode.execute(PythonUtils.EMPTY_BYTE_ARRAY);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
diff -r -u a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py
2+
--- a/_distutils_hack/__init__.py 2022-06-17 21:46:23.000000000 +0200
3+
+++ b/_distutils_hack/__init__.py 2022-07-29 10:37:40.000000000 +0200
4+
@@ -44,7 +44,9 @@
5+
"""
6+
Allow selection of distutils by environment variable.
7+
"""
8+
- which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
9+
+ # which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
10+
+ # GraalVM change: default to stdlib
11+
+ which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
12+
return which == 'local'
13+
14+
15+
@@ -200,7 +202,8 @@
16+
17+
18+
def add_shim():
19+
- DISTUTILS_FINDER in sys.meta_path or insert_shim()
20+
+ if enabled():
21+
+ DISTUTILS_FINDER in sys.meta_path or insert_shim()
22+
23+
24+
class shim:
25+
@@ -212,7 +215,8 @@
26+
27+
28+
def insert_shim():
29+
- sys.meta_path.insert(0, DISTUTILS_FINDER)
30+
+ if enabled():
31+
+ sys.meta_path.insert(0, DISTUTILS_FINDER)
32+
33+
34+
def remove_shim():
35+
diff -r -u a/setuptools/_vendor/importlib_metadata/_compat.py b/setuptools/_vendor/importlib_metadata/_compat.py
36+
--- a/setuptools/_vendor/importlib_metadata/_compat.py 2022-06-17 21:46:23.000000000 +0200
37+
+++ b/setuptools/_vendor/importlib_metadata/_compat.py 2022-07-29 10:56:09.000000000 +0200
38+
@@ -36,7 +36,7 @@
39+
def matches(finder):
40+
return getattr(
41+
finder, '__module__', None
42+
- ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
43+
+ ) in ('_frozen_importlib_external', 'importlib._bootstrap_external') and hasattr(finder, 'find_distributions')
44+
45+
for finder in filter(matches, sys.meta_path): # pragma: nocover
46+
del finder.find_distributions

0 commit comments

Comments
 (0)