42
42
43
43
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .IndexError ;
44
44
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
45
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .OverflowError ;
45
46
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
46
47
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
47
48
64
65
import com .oracle .graal .python .nodes .PGuards ;
65
66
import com .oracle .graal .python .nodes .PRaiseNode ;
66
67
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
68
+ import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
67
69
import com .oracle .graal .python .nodes .util .CastToJavaUnsignedLongNode ;
68
70
import com .oracle .graal .python .runtime .PythonContext ;
69
71
import com .oracle .graal .python .runtime .exception .PException ;
@@ -248,6 +250,7 @@ private static long unpackInt64(byte[] bytes) {
248
250
@ ImportStatic ({PMemoryView .BufferFormat .class , PGuards .class })
249
251
abstract static class PackValueNode extends Node {
250
252
@ Child private PRaiseNode raiseNode ;
253
+ @ Child private IsBuiltinClassProfile isOverflowErrorProfile ;
251
254
252
255
// Output goes to bytes, lenght not checked
253
256
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 {
256
259
void packUnsignedByteInt (@ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , int value , byte [] bytes ) {
257
260
assert bytes .length == 1 ;
258
261
if (value < 0 || value > 0xFF ) {
259
- throw raise ( ValueError , ErrorMessages . MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S , formatStr );
262
+ throw valueError ( formatStr );
260
263
}
261
264
bytes [0 ] = (byte ) value ;
262
265
}
263
266
264
267
@ Specialization (guards = "format == UNSIGNED_BYTE" , replaces = "packUnsignedByteInt" , limit = "2" )
265
268
void packUnsignedByteGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
266
269
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
267
- long value = lib . asJavaLong (object , frame );
270
+ long value = asJavaLong (frame , formatStr , object , lib );
268
271
assert bytes .length == 1 ;
269
272
if (value < 0 || value > 0xFF ) {
270
- throw raise ( ValueError , ErrorMessages . MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S , formatStr );
273
+ throw valueError ( formatStr );
271
274
}
272
275
bytes [0 ] = (byte ) value ;
273
276
}
274
277
275
278
@ Specialization (guards = "format == SIGNED_BYTE" , replaces = "packUnsignedByteInt" , limit = "2" )
276
279
void packSignedByteGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
277
280
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
278
- long value = lib . asJavaLong (object , frame );
281
+ long value = asJavaLong (frame , formatStr , object , lib );
279
282
assert bytes .length == 1 ;
280
283
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 );
282
285
}
283
286
bytes [0 ] = (byte ) value ;
284
287
}
285
288
286
289
@ Specialization (guards = "format == SIGNED_SHORT" , limit = "2" )
287
290
void packSignedShortGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
288
291
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
289
- long value = lib . asJavaLong (object , frame );
292
+ long value = asJavaLong (frame , formatStr , object , lib );
290
293
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 );
292
295
}
293
296
packInt16 ((int ) value , bytes );
294
297
}
295
298
296
299
@ Specialization (guards = "format == UNSIGNED_SHORT" , limit = "2" )
297
300
void packUnsignedShortGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
298
301
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
299
- long value = lib . asJavaLong (object , frame );
302
+ long value = asJavaLong (frame , formatStr , object , lib );
300
303
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 );
302
305
}
303
306
packInt16 ((int ) value , bytes );
304
307
}
@@ -311,50 +314,62 @@ static void packSignedIntInt(@SuppressWarnings("unused") PMemoryView.BufferForma
311
314
@ Specialization (guards = "format == SIGNED_INT" , replaces = "packSignedIntInt" , limit = "2" )
312
315
void packSignedIntGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
313
316
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
314
- long value = lib . asJavaLong (object , frame );
317
+ long value = asJavaLong (frame , formatStr , object , lib );
315
318
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 );
317
320
}
318
321
packInt32 ((int ) value , bytes );
319
322
}
320
323
321
324
@ Specialization (guards = "format == UNSIGNED_INT" , replaces = "packSignedIntInt" , limit = "2" )
322
325
void packUnsignedIntGeneric (VirtualFrame frame , @ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , Object object , byte [] bytes ,
323
326
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
324
- long value = lib . asJavaLong (object , frame );
327
+ long value = asJavaLong (frame , formatStr , object , lib );
325
328
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 );
327
330
}
328
331
packInt32 ((int ) value , bytes );
329
332
}
330
333
331
334
@ 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 ,
333
336
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
334
337
assert bytes .length == 8 ;
335
- packInt64 (lib . asJavaLong (object , frame ), bytes );
338
+ packInt64 (asJavaLong (frame , formatStr , object , lib ), bytes );
336
339
}
337
340
338
341
@ 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 ,
340
343
@ Cached CastToJavaUnsignedLongNode cast ,
341
344
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
342
345
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
+ }
344
351
}
345
352
346
353
@ 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 ,
348
355
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
349
356
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
+ }
351
362
}
352
363
353
364
@ 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 ,
355
366
@ CachedLibrary ("object" ) PythonObjectLibrary lib ) {
356
367
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
+ }
358
373
}
359
374
360
375
@ Specialization (guards = "format == BOOLEAN" , limit = "2" )
@@ -370,7 +385,7 @@ void packChar(@SuppressWarnings("unused") PMemoryView.BufferFormat format, Strin
370
385
try {
371
386
byte [] value = lib .getBufferBytes (object );
372
387
if (value .length != 1 ) {
373
- throw raise ( ValueError , ErrorMessages . MEMORYVIEW_INVALID_VALUE_FOR_FORMAT_S , formatStr );
388
+ throw valueError ( formatStr );
374
389
}
375
390
bytes [0 ] = value [0 ];
376
391
} catch (UnsupportedMessageException e ) {
@@ -380,14 +395,35 @@ void packChar(@SuppressWarnings("unused") PMemoryView.BufferFormat format, Strin
380
395
381
396
@ Specialization (guards = {"format == CHAR" , "!isBytes(object)" })
382
397
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 );
384
399
}
385
400
386
401
@ Specialization (guards = "format == OTHER" )
387
402
void notImplemented (@ SuppressWarnings ("unused" ) PMemoryView .BufferFormat format , String formatStr , @ SuppressWarnings ("unused" ) Object object , @ SuppressWarnings ("unused" ) byte [] bytes ) {
388
403
throw raise (NotImplementedError , ErrorMessages .MEMORYVIEW_FORMAT_S_NOT_SUPPORTED , formatStr );
389
404
}
390
405
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
+
391
427
private static void packInt16 (int value , byte [] bytes ) {
392
428
assert bytes .length == 2 ;
393
429
bytes [0 ] = (byte ) value ;
@@ -421,6 +457,14 @@ private PException raise(PythonBuiltinClassType type, String message, Object...
421
457
}
422
458
throw raiseNode .raise (type , message , args );
423
459
}
460
+
461
+ private IsBuiltinClassProfile getIsOverflowErrorProfile () {
462
+ if (isOverflowErrorProfile == null ) {
463
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
464
+ isOverflowErrorProfile = insert (IsBuiltinClassProfile .create ());
465
+ }
466
+ return isOverflowErrorProfile ;
467
+ }
424
468
}
425
469
426
470
@ GenerateUncached
0 commit comments