Skip to content

Commit b030df9

Browse files
committed
[GR-33305] Setup indirect call when using PythonBuffer*Library.
PullRequest: graalpython/1992
2 parents b30fef8 + 873e55b commit b030df9

File tree

59 files changed

+964
-383
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+964
-383
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_fileio.txt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testAttributes
22
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testBlksize
3-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClose
4-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedFileno
5-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedIsatty
6-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedRead
7-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedReadable
8-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedReadall
9-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedReadinto
10-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedSeek
11-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedSeekable
12-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedTell
13-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedTruncate
14-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedWritable
15-
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrnoOnClosedWrite
163
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testErrors
174
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testMethods
185
*graalpython.lib-python.3.test.test_fileio.CAutoFileTests.testOpenDirFD
@@ -46,19 +33,6 @@
4633
*graalpython.lib-python.3.test.test_fileio.COtherFileTests.test_open_code
4734
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testAttributes
4835
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testBlksize
49-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClose
50-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedFileno
51-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedIsatty
52-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedRead
53-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedReadable
54-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedReadall
55-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedReadinto
56-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedSeek
57-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedSeekable
58-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedTell
59-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedTruncate
60-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedWritable
61-
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrnoOnClosedWrite
6236
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testErrors
6337
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testMethods
6438
*graalpython.lib-python.3.test.test_fileio.PyAutoFileTests.testOpenDirFD

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,18 @@
6868
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
6969
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
7070
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
71-
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
71+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
7272
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
7373
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
74+
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
7475
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7576
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7677
import com.oracle.truffle.api.dsl.Cached;
7778
import com.oracle.truffle.api.dsl.Fallback;
7879
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7980
import com.oracle.truffle.api.dsl.NodeFactory;
8081
import com.oracle.truffle.api.dsl.Specialization;
82+
import com.oracle.truffle.api.frame.VirtualFrame;
8183
import com.oracle.truffle.api.library.CachedLibrary;
8284
import com.oracle.truffle.api.library.ExportLibrary;
8385
import com.oracle.truffle.api.library.ExportMessage;
@@ -90,11 +92,11 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9092
return BinasciiModuleBuiltinsFactory.getFactories();
9193
}
9294

93-
abstract static class AsciiBufferConverter extends ArgumentCastNode.ArgumentCastNodeWithRaise {
95+
abstract static class AsciiBufferConverter extends ArgumentCastNodeWithRaiseAndIndirectCall {
9496
@Specialization(guards = "acquireLib.hasBuffer(value)", limit = "getCallSiteInlineCacheMaxDepth()")
95-
Object doObject(Object value,
97+
Object doObject(VirtualFrame frame, Object value,
9698
@CachedLibrary("value") PythonBufferAcquireLibrary acquireLib) {
97-
return acquireLib.acquireReadonly(value);
99+
return acquireLib.acquireReadonly(value, frame, getContext(), getLanguage(), this);
98100
}
99101

100102
@ExportLibrary(PythonBufferAccessLibrary.class)
@@ -156,24 +158,25 @@ public static AsciiBufferConverter create() {
156158
@GenerateNodeFactory
157159
abstract static class A2bBase64Node extends PythonUnaryClinicBuiltinNode {
158160
@Specialization(limit = "3")
159-
PBytes doConvert(Object buffer,
161+
PBytes doConvert(VirtualFrame frame, Object buffer,
160162
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
161163
try {
162-
return b64decode(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
164+
ByteSequenceStorage storage = b64decode(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
165+
return factory().createBytes(storage);
163166
} finally {
164-
bufferLib.release(buffer);
167+
bufferLib.release(buffer, frame, this);
165168
}
166169
}
167170

168171
@TruffleBoundary
169-
private PBytes b64decode(byte[] data, int dataLen) {
172+
private ByteSequenceStorage b64decode(byte[] data, int dataLen) {
170173
try {
171174
// Using MIME decoder because that one skips over anything that is not the alphabet,
172175
// just like CPython does
173176
ByteBuffer result = Base64.getMimeDecoder().decode(ByteBuffer.wrap(data, 0, dataLen));
174-
return factory().createBytes(result.array(), result.limit());
177+
return new ByteSequenceStorage(result.array(), result.limit());
175178
} catch (IllegalArgumentException e) {
176-
throw raise(BinasciiError, e);
179+
throw PRaiseNode.raiseUncached(this, BinasciiError, e);
177180
}
178181
}
179182

@@ -188,25 +191,26 @@ protected ArgumentClinicProvider getArgumentClinic() {
188191
@GenerateNodeFactory
189192
abstract static class A2bHexNode extends PythonUnaryClinicBuiltinNode {
190193
@Specialization(limit = "3")
191-
PBytes a2b(Object buffer,
194+
PBytes a2b(VirtualFrame frame, Object buffer,
192195
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
193196
try {
194-
return a2b(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
197+
byte[] bytes = a2b(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
198+
return factory().createBytes(bytes);
195199
} finally {
196-
bufferLib.release(buffer);
200+
bufferLib.release(buffer, frame, this);
197201
}
198202
}
199203

200204
@TruffleBoundary
201-
private PBytes a2b(byte[] bytes, int length) {
205+
private byte[] a2b(byte[] bytes, int length) {
202206
if (length % 2 != 0) {
203-
throw raise(BinasciiError, ErrorMessages.ODD_LENGTH_STRING);
207+
throw PRaiseNode.raiseUncached(this, BinasciiError, ErrorMessages.ODD_LENGTH_STRING);
204208
}
205209
byte[] output = new byte[length / 2];
206210
for (int i = 0; i < length / 2; i++) {
207211
output[i] = (byte) (digitValue((char) bytes[i * 2]) * 16 + digitValue((char) bytes[i * 2 + 1]));
208212
}
209-
return factory().createBytes(output);
213+
return output;
210214
}
211215

212216
private int digitValue(char b) {
@@ -217,7 +221,7 @@ private int digitValue(char b) {
217221
} else if (b >= 'A' && b <= 'F') {
218222
return b - 'A' + 10;
219223
} else {
220-
throw raise(BinasciiError, ErrorMessages.NON_HEX_DIGIT_FOUND);
224+
throw PRaiseNode.raiseUncached(this, BinasciiError, ErrorMessages.NON_HEX_DIGIT_FOUND);
221225
}
222226
}
223227

@@ -249,12 +253,12 @@ private PBytes b2a(byte[] data, int lenght, int newline) {
249253
}
250254

251255
@Specialization(limit = "3")
252-
PBytes b2aBuffer(Object buffer, int newline,
256+
PBytes b2aBuffer(VirtualFrame frame, Object buffer, int newline,
253257
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
254258
try {
255259
return b2a(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer), newline);
256260
} finally {
257-
bufferLib.release(buffer);
261+
bufferLib.release(buffer, frame, this);
258262
}
259263
}
260264

@@ -273,7 +277,7 @@ abstract static class B2aHexNode extends PythonTernaryClinicBuiltinNode {
273277
@CompilationFinal(dimensions = 1) private static final byte[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
274278

275279
@Specialization(limit = "3")
276-
PBytes b2a(Object buffer, Object sep, int bytesPerSep,
280+
PBytes b2a(VirtualFrame frame, Object buffer, Object sep, int bytesPerSep,
277281
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
278282
if (sep != PNone.NO_VALUE || bytesPerSep != 1) {
279283
// TODO implement sep and bytes_per_sep
@@ -282,7 +286,7 @@ PBytes b2a(Object buffer, Object sep, int bytesPerSep,
282286
try {
283287
return b2a(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
284288
} finally {
285-
bufferLib.release(buffer);
289+
bufferLib.release(buffer, frame, this);
286290
}
287291
}
288292

@@ -310,12 +314,12 @@ protected ArgumentClinicProvider getArgumentClinic() {
310314
abstract static class Crc32Node extends PythonBinaryClinicBuiltinNode {
311315
// TODO crc != NO_VALUE
312316
@Specialization(guards = "isNoValue(crc)", limit = "3")
313-
static long b2a(Object buffer, @SuppressWarnings("unused") PNone crc,
317+
long b2a(VirtualFrame frame, Object buffer, @SuppressWarnings("unused") PNone crc,
314318
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
315319
try {
316320
return getCrcValue(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
317321
} finally {
318-
bufferLib.release(buffer);
322+
bufferLib.release(buffer, frame, this);
319323
}
320324
}
321325

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ PBytes doCallBytes(VirtualFrame frame, Object cls, Object source, PNone encoding
325325
if (cls == PythonBuiltinClassType.PBytes) {
326326
return (PBytes) bytes;
327327
} else {
328-
return factory().createBytes(cls, toBytesNode.execute(bytes));
328+
return factory().createBytes(cls, toBytesNode.execute(frame, bytes));
329329
}
330330
} else {
331331
throw raise(TypeError, ErrorMessages.RETURNED_NONBYTES, __BYTES__, bytes);
@@ -1375,15 +1375,17 @@ Object fail(Object cls, Object arg, Object base) {
13751375
Object createIntGeneric(VirtualFrame frame, Object cls, Object obj, @SuppressWarnings("unused") PNone base,
13761376
@CachedLibrary(limit = "3") PythonBufferAcquireLibrary bufferAcquireLib,
13771377
@CachedLibrary(limit = "3") PythonBufferAccessLibrary bufferLib) {
1378-
// This method (together with callInt and callIndex) reflects the logic of PyNumber_Long
1379-
// in CPython. We don't use PythonObjectLibrary here since the original CPython function
1380-
// does not use any of the conversion functions (such as _PyLong_AsInt or
1381-
// PyNumber_Index) either, but it reimplements the logic in a slightly different way
1382-
// (e.g. trying __int__ before __index__ whereas _PyLong_AsInt does it the other way)
1383-
// and also with specific exception messages which are expected by Python unittests.
1384-
// This unfortunately means that this method relies on the internal logic of NO_VALUE
1385-
// return values representing missing magic methods which should be ideally hidden
1386-
// by PythonObjectLibrary.
1378+
/*
1379+
* This method (together with callInt and callIndex) reflects the logic of PyNumber_Long
1380+
* in CPython. We don't use PythonObjectLibrary here since the original CPython function
1381+
* does not use any of the conversion functions (such as _PyLong_AsInt or
1382+
* PyNumber_Index) either, but it reimplements the logic in a slightly different way
1383+
* (e.g. trying __int__ before __index__ whereas _PyLong_AsInt does it the other way)
1384+
* and also with specific exception messages which are expected by Python unittests.
1385+
* This unfortunately means that this method relies on the internal logic of NO_VALUE
1386+
* return values representing missing magic methods which should be ideally hidden by
1387+
* PythonObjectLibrary.
1388+
*/
13871389
Object result = callInt(frame, obj);
13881390
if (result == PNone.NO_VALUE) {
13891391
result = callIndex(frame, obj);
@@ -1392,15 +1394,15 @@ Object createIntGeneric(VirtualFrame frame, Object cls, Object obj, @SuppressWar
13921394
if (truncResult == PNone.NO_VALUE) {
13931395
Object buffer;
13941396
try {
1395-
buffer = bufferAcquireLib.acquireReadonly(obj);
1397+
buffer = bufferAcquireLib.acquireReadonly(obj, frame, this);
13961398
} catch (PException e) {
13971399
throw raise(TypeError, ErrorMessages.ARG_MUST_BE_STRING_OR_BYTELIKE_OR_NUMBER, "int()", obj);
13981400
}
13991401
try {
14001402
String number = PythonUtils.newString(bufferLib.getInternalOrCopiedByteArray(buffer), 0, bufferLib.getBufferLength(buffer));
14011403
return stringToInt(frame, cls, number, 10, obj);
14021404
} finally {
1403-
bufferLib.release(buffer);
1405+
bufferLib.release(buffer, frame, this);
14041406
}
14051407
}
14061408
if (isIntegerType(truncResult)) {
@@ -1892,7 +1894,7 @@ Object doBuffer(VirtualFrame frame, Object strClass, Object obj, Object encoding
18921894
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
18931895
Object buffer;
18941896
try {
1895-
buffer = acquireLib.acquireReadonly(obj);
1897+
buffer = acquireLib.acquireReadonly(obj, frame, this);
18961898
} catch (PException e) {
18971899
throw raise(TypeError, ErrorMessages.NEED_BYTELIKE_OBJ, obj);
18981900
}
@@ -1903,7 +1905,7 @@ Object doBuffer(VirtualFrame frame, Object strClass, Object obj, Object encoding
19031905
Object en = encoding == PNone.NO_VALUE ? "utf-8" : encoding;
19041906
return decodeBytes(frame, strClass, bytesObj, en, errors);
19051907
} finally {
1906-
bufferLib.release(buffer);
1908+
bufferLib.release(buffer, frame, this);
19071909
}
19081910
}
19091911

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,14 @@ PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode
847847
String filename;
848848
// TODO use PyUnicode_FSDecode
849849
if (acquireLib.hasBuffer(wFilename)) {
850-
Object filenameBuffer = acquireLib.acquireReadonly(wFilename);
850+
Object filenameBuffer = acquireLib.acquireReadonly(wFilename, frame, this);
851851
try {
852852
filename = PythonUtils.newString(bufferLib.getCopiedByteArray(filenameBuffer));
853853
if (!(wFilename instanceof PBytes)) {
854854
warnNode.warnFormat(frame, null, DeprecationWarning, 1, ErrorMessages.PATH_SHOULD_BE_STR_BYTES_PATHLIKE_NOT_P, wFilename);
855855
}
856856
} finally {
857-
bufferLib.release(filenameBuffer);
857+
bufferLib.release(filenameBuffer, frame, this);
858858
}
859859
} else {
860860
filename = asPath.execute(frame, wFilename);
@@ -920,7 +920,7 @@ String sourceAsString(VirtualFrame frame, Object source, String filename, Intero
920920
// buffers, since that's fast for us anyway
921921
Object buffer;
922922
try {
923-
buffer = acquireLib.acquireReadonly(source);
923+
buffer = acquireLib.acquireReadonly(source, frame, this);
924924
} catch (PException e) {
925925
throw raise(TypeError, ErrorMessages.ARG_D_MUST_BE_S, "compile()", 1, "string, bytes or AST object");
926926
}
@@ -942,8 +942,7 @@ String sourceAsString(VirtualFrame frame, Object source, String filename, Intero
942942
} catch (PythonFileDetector.InvalidEncodingException e) {
943943
throw raiseInvalidSyntax(filename, "encoding problem: %s", e.getEncodingName());
944944
} finally {
945-
bufferLib.release(buffer);
946-
945+
bufferLib.release(buffer, frame, this);
947946
}
948947
}
949948
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,13 @@ public final Object execute(@SuppressWarnings("unused") VirtualFrame frame, byte
638638
}
639639

640640
@Specialization(limit = "3")
641-
Object decode(Object buffer, String errors,
641+
Object decode(VirtualFrame frame, Object buffer, String errors,
642642
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
643643
try {
644644
int len = bufferLib.getBufferLength(buffer);
645645
return decodeBytes(bufferLib.getInternalOrCopiedByteArray(buffer), len, errors);
646646
} finally {
647-
bufferLib.release(buffer);
647+
bufferLib.release(buffer, frame, this);
648648
}
649649
}
650650

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,14 @@ static Object doBytesByteStorage(PBytesLike object) {
293293
}
294294

295295
@Specialization(guards = "!isBytes(object)", limit = "3")
296-
static Object doBuffer(Object object,
296+
Object doBuffer(VirtualFrame frame, Object object,
297297
@CachedLibrary("object") PythonBufferAcquireLibrary acquireLib,
298298
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
299-
Object buffer = acquireLib.acquireReadonly(object);
299+
Object buffer = acquireLib.acquireReadonly(object, frame, this);
300300
try {
301301
return new InteropByteArray(bufferLib.getCopiedByteArray(object));
302302
} finally {
303-
bufferLib.release(buffer);
303+
bufferLib.release(buffer, frame, this);
304304
}
305305
}
306306
}

0 commit comments

Comments
 (0)