Skip to content

Commit b00e24f

Browse files
committed
Rewrap OverflowError in PackValueNode
1 parent 83775d3 commit b00e24f

File tree

1 file changed

+67
-23
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview

1 file changed

+67
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/MemoryViewNodes.java

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
4444
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.NotImplementedError;
45+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
4546
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4647
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
4748

@@ -64,6 +65,7 @@
6465
import com.oracle.graal.python.nodes.PGuards;
6566
import com.oracle.graal.python.nodes.PRaiseNode;
6667
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
68+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6769
import com.oracle.graal.python.nodes.util.CastToJavaUnsignedLongNode;
6870
import com.oracle.graal.python.runtime.PythonContext;
6971
import com.oracle.graal.python.runtime.exception.PException;
@@ -248,6 +250,7 @@ private static long unpackInt64(byte[] bytes) {
248250
@ImportStatic({PMemoryView.BufferFormat.class, PGuards.class})
249251
abstract static class PackValueNode extends Node {
250252
@Child private PRaiseNode raiseNode;
253+
@Child private IsBuiltinClassProfile isOverflowErrorProfile;
251254

252255
// Output goes to bytes, lenght not checked
253256
public abstract void execute(VirtualFrame frame, PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes);
@@ -256,49 +259,49 @@ abstract static class PackValueNode extends Node {
256259
void packUnsignedByteInt(@SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, int value, byte[] bytes) {
257260
assert bytes.length == 1;
258261
if (value < 0 || value > 0xFF) {
259-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
262+
throw valueError(formatStr);
260263
}
261264
bytes[0] = (byte) value;
262265
}
263266

264267
@Specialization(guards = "format == UNSIGNED_BYTE", replaces = "packUnsignedByteInt", limit = "2")
265268
void packUnsignedByteGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
266269
@CachedLibrary("object") PythonObjectLibrary lib) {
267-
long value = lib.asJavaLong(object, frame);
270+
long value = asJavaLong(frame, formatStr, object, lib);
268271
assert bytes.length == 1;
269272
if (value < 0 || value > 0xFF) {
270-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
273+
throw valueError(formatStr);
271274
}
272275
bytes[0] = (byte) value;
273276
}
274277

275278
@Specialization(guards = "format == SIGNED_BYTE", replaces = "packUnsignedByteInt", limit = "2")
276279
void packSignedByteGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
277280
@CachedLibrary("object") PythonObjectLibrary lib) {
278-
long value = lib.asJavaLong(object, frame);
281+
long value = asJavaLong(frame, formatStr, object, lib);
279282
assert bytes.length == 1;
280283
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
281-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
284+
throw valueError(formatStr);
282285
}
283286
bytes[0] = (byte) value;
284287
}
285288

286289
@Specialization(guards = "format == SIGNED_SHORT", limit = "2")
287290
void packSignedShortGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
288291
@CachedLibrary("object") PythonObjectLibrary lib) {
289-
long value = lib.asJavaLong(object, frame);
292+
long value = asJavaLong(frame, formatStr, object, lib);
290293
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
291-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
294+
throw valueError(formatStr);
292295
}
293296
packInt16((int) value, bytes);
294297
}
295298

296299
@Specialization(guards = "format == UNSIGNED_SHORT", limit = "2")
297300
void packUnsignedShortGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
298301
@CachedLibrary("object") PythonObjectLibrary lib) {
299-
long value = lib.asJavaLong(object, frame);
302+
long value = asJavaLong(frame, formatStr, object, lib);
300303
if (value < 0 || value > (Short.MAX_VALUE << 1) + 1) {
301-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
304+
throw valueError(formatStr);
302305
}
303306
packInt16((int) value, bytes);
304307
}
@@ -311,50 +314,62 @@ static void packSignedIntInt(@SuppressWarnings("unused") PMemoryView.BufferForma
311314
@Specialization(guards = "format == SIGNED_INT", replaces = "packSignedIntInt", limit = "2")
312315
void packSignedIntGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
313316
@CachedLibrary("object") PythonObjectLibrary lib) {
314-
long value = lib.asJavaLong(object, frame);
317+
long value = asJavaLong(frame, formatStr, object, lib);
315318
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
316-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
319+
throw valueError(formatStr);
317320
}
318321
packInt32((int) value, bytes);
319322
}
320323

321324
@Specialization(guards = "format == UNSIGNED_INT", replaces = "packSignedIntInt", limit = "2")
322325
void packUnsignedIntGeneric(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
323326
@CachedLibrary("object") PythonObjectLibrary lib) {
324-
long value = lib.asJavaLong(object, frame);
327+
long value = asJavaLong(frame, formatStr, object, lib);
325328
if (value < 0 || value > ((long) (Integer.MAX_VALUE) << 1L) + 1L) {
326-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
329+
throw valueError(formatStr);
327330
}
328331
packInt32((int) value, bytes);
329332
}
330333

331334
@Specialization(guards = "format == SIGNED_LONG", limit = "2")
332-
static void packSignedLong(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, @SuppressWarnings("unused") String formatStr, Object object, byte[] bytes,
335+
void packSignedLong(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
333336
@CachedLibrary("object") PythonObjectLibrary lib) {
334337
assert bytes.length == 8;
335-
packInt64(lib.asJavaLong(object, frame), bytes);
338+
packInt64(asJavaLong(frame, formatStr, object, lib), bytes);
336339
}
337340

338341
@Specialization(guards = "format == UNSIGNED_LONG", limit = "2")
339-
static void packUnsignedLong(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, @SuppressWarnings("unused") String formatStr, Object object, byte[] bytes,
342+
void packUnsignedLong(VirtualFrame frame, @SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
340343
@Cached CastToJavaUnsignedLongNode cast,
341344
@CachedLibrary("object") PythonObjectLibrary lib) {
342345
assert bytes.length == 8;
343-
packInt64(cast.execute(lib.asIndexWithFrame(object, frame)), bytes);
346+
try {
347+
packInt64(cast.execute(lib.asIndexWithFrame(object, frame)), bytes);
348+
} catch (PException e) {
349+
throw processException(e, formatStr);
350+
}
344351
}
345352

346353
@Specialization(guards = "format == FLOAT", limit = "2")
347-
static void packFloat(@SuppressWarnings("unused") PMemoryView.BufferFormat format, @SuppressWarnings("unused") String formatStr, Object object, byte[] bytes,
354+
void packFloat(@SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
348355
@CachedLibrary("object") PythonObjectLibrary lib) {
349356
assert bytes.length == 4;
350-
packInt32(Float.floatToRawIntBits((float) lib.asJavaDouble(object)), bytes);
357+
try {
358+
packInt32(Float.floatToRawIntBits((float) lib.asJavaDouble(object)), bytes);
359+
} catch (PException e) {
360+
throw processException(e, formatStr);
361+
}
351362
}
352363

353364
@Specialization(guards = "format == DOUBLE", limit = "2")
354-
static void packDouble(@SuppressWarnings("unused") PMemoryView.BufferFormat format, @SuppressWarnings("unused") String formatStr, Object object, byte[] bytes,
365+
void packDouble(@SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, Object object, byte[] bytes,
355366
@CachedLibrary("object") PythonObjectLibrary lib) {
356367
assert bytes.length == 8;
357-
packInt64(Double.doubleToRawLongBits(lib.asJavaDouble(object)), bytes);
368+
try {
369+
packInt64(Double.doubleToRawLongBits(lib.asJavaDouble(object)), bytes);
370+
} catch (PException e) {
371+
throw processException(e, formatStr);
372+
}
358373
}
359374

360375
@Specialization(guards = "format == BOOLEAN", limit = "2")
@@ -370,7 +385,7 @@ void packChar(@SuppressWarnings("unused") PMemoryView.BufferFormat format, Strin
370385
try {
371386
byte[] value = lib.getBufferBytes(object);
372387
if (value.length != 1) {
373-
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
388+
throw valueError(formatStr);
374389
}
375390
bytes[0] = value[0];
376391
} catch (UnsupportedMessageException e) {
@@ -380,14 +395,35 @@ void packChar(@SuppressWarnings("unused") PMemoryView.BufferFormat format, Strin
380395

381396
@Specialization(guards = {"format == CHAR", "!isBytes(object)"})
382397
void packChar(@SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, @SuppressWarnings("unused") Object object, @SuppressWarnings("unused") byte[] bytes) {
383-
throw raise(TypeError, ErrorMessages.MEMORYVIEW_INVALID_TYPE_FOR_FORMAT_S, formatStr);
398+
throw typeError(formatStr);
384399
}
385400

386401
@Specialization(guards = "format == OTHER")
387402
void notImplemented(@SuppressWarnings("unused") PMemoryView.BufferFormat format, String formatStr, @SuppressWarnings("unused") Object object, @SuppressWarnings("unused") byte[] bytes) {
388403
throw raise(NotImplementedError, ErrorMessages.MEMORYVIEW_FORMAT_S_NOT_SUPPORTED, formatStr);
389404
}
390405

406+
private PException valueError(String formatStr) {
407+
throw raise(ValueError, ErrorMessages.MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S, formatStr);
408+
}
409+
410+
private PException typeError(String formatStr) {
411+
throw raise(TypeError, ErrorMessages.MEMORYVIEW_INVALID_TYPE_FOR_FORMAT_S, formatStr);
412+
}
413+
414+
private PException processException(PException e, String formatStr) {
415+
e.expect(OverflowError, getIsOverflowErrorProfile());
416+
throw valueError(formatStr);
417+
}
418+
419+
private long asJavaLong(VirtualFrame frame, String formatStr, Object object, PythonObjectLibrary lib) {
420+
try {
421+
return lib.asJavaLong(object, frame);
422+
} catch (PException e) {
423+
throw processException(e, formatStr);
424+
}
425+
}
426+
391427
private static void packInt16(int value, byte[] bytes) {
392428
assert bytes.length == 2;
393429
bytes[0] = (byte) value;
@@ -421,6 +457,14 @@ private PException raise(PythonBuiltinClassType type, String message, Object...
421457
}
422458
throw raiseNode.raise(type, message, args);
423459
}
460+
461+
private IsBuiltinClassProfile getIsOverflowErrorProfile() {
462+
if (isOverflowErrorProfile == null) {
463+
CompilerDirectives.transferToInterpreterAndInvalidate();
464+
isOverflowErrorProfile = insert(IsBuiltinClassProfile.create());
465+
}
466+
return isOverflowErrorProfile;
467+
}
424468
}
425469

426470
@GenerateUncached

0 commit comments

Comments
 (0)