@@ -414,16 +414,21 @@ static int run() {
414
414
415
415
@ CApiBuiltin (ret = Int , args = {PyLongObject , UNSIGNED_CHAR_PTR , SIZE_T , Int , Int }, call = Direct )
416
416
abstract static class _PyLong_AsByteArray extends CApi5BuiltinNode {
417
+ private static void checkSign (boolean negative , int isSigned , PRaiseNode raise ) {
418
+ if (negative ) {
419
+ if (isSigned == 0 ) {
420
+ throw raise .raise (OverflowError , ErrorMessages .MESSAGE_CONVERT_NEGATIVE );
421
+ }
422
+ }
423
+ }
424
+
417
425
@ Specialization
418
426
static Object get (int value , Object bytes , long n , int littleEndian , int isSigned ,
419
427
@ Bind ("this" ) Node inliningTarget ,
420
428
@ Shared @ Cached InlinedConditionProfile profile ,
421
429
@ Shared @ Cached PRaiseNode raise ,
422
430
@ Shared @ Cached CStructAccess .WriteByteNode write ) {
423
- if (isSigned == 0 && value < 0 ) {
424
- throw raise .raise (OverflowError , ErrorMessages .MESSAGE_CONVERT_NEGATIVE );
425
- }
426
-
431
+ checkSign (value < 0 , isSigned , raise );
427
432
byte [] array = IntBuiltins .ToBytesNode .fromLong (value , PythonUtils .toIntError (n ), littleEndian == 0 , isSigned != 0 , inliningTarget , profile , raise );
428
433
write .writeByteArray (bytes , array );
429
434
return 0 ;
@@ -435,10 +440,7 @@ static Object get(long value, Object bytes, long n, int littleEndian, int isSign
435
440
@ Shared @ Cached InlinedConditionProfile profile ,
436
441
@ Shared @ Cached PRaiseNode raise ,
437
442
@ Shared @ Cached CStructAccess .WriteByteNode write ) {
438
- if (isSigned == 0 && value < 0 ) {
439
- throw raise .raise (OverflowError , ErrorMessages .MESSAGE_CONVERT_NEGATIVE );
440
- }
441
-
443
+ checkSign (value < 0 , isSigned , raise );
442
444
byte [] array = IntBuiltins .ToBytesNode .fromLong (value , PythonUtils .toIntError (n ), littleEndian == 0 , isSigned != 0 , inliningTarget , profile , raise );
443
445
write .writeByteArray (bytes , array );
444
446
return 0 ;
@@ -450,10 +452,7 @@ static Object get(PInt value, Object bytes, long n, int littleEndian, int isSign
450
452
@ Shared @ Cached InlinedConditionProfile profile ,
451
453
@ Shared @ Cached PRaiseNode raise ,
452
454
@ Shared @ Cached CStructAccess .WriteByteNode write ) {
453
- if (isSigned == 0 && value .isNegative ()) {
454
- throw raise .raise (OverflowError , ErrorMessages .MESSAGE_CONVERT_NEGATIVE );
455
- }
456
-
455
+ checkSign (value .isNegative (), isSigned , raise );
457
456
byte [] array = IntBuiltins .ToBytesNode .fromBigInteger (value , PythonUtils .toIntError (n ), littleEndian == 0 , isSigned != 0 , inliningTarget , profile , raise );
458
457
write .writeByteArray (bytes , array );
459
458
return 0 ;
0 commit comments