120
120
import com .oracle .graal .python .builtins .objects .ints .IntBuiltinsClinicProviders .FormatNodeClinicProviderGen ;
121
121
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
122
122
import com .oracle .graal .python .builtins .objects .type .PythonBuiltinClass ;
123
- import com .oracle .graal .python .lib .PyNumberAsSizeNode ;
124
123
import com .oracle .graal .python .lib .PyNumberFloatNode ;
125
124
import com .oracle .graal .python .lib .PyObjectHashNode ;
126
125
import com .oracle .graal .python .nodes .ErrorMessages ;
127
- import com .oracle .graal .python .nodes .PGuards ;
128
126
import com .oracle .graal .python .nodes .PRaiseNode ;
129
127
import com .oracle .graal .python .nodes .SpecialMethodNames ;
130
128
import com .oracle .graal .python .nodes .call .special .LookupAndCallUnaryNode ;
@@ -2395,17 +2393,14 @@ static PNotImplemented doGeneric(Object a, Object b) {
2395
2393
}
2396
2394
2397
2395
// to_bytes
2398
- @ Builtin (name = "to_bytes" , minNumOfPositionalArgs = 3 , parameterNames = {"self" , "bytecount" , "byteorder" , "signed" })
2396
+ @ Builtin (name = "to_bytes" , minNumOfPositionalArgs = 3 , parameterNames = {"$self" , "length" , "byteorder" }, keywordOnlyNames = {"signed" })
2397
+ @ ArgumentClinic (name = "length" , conversion = ClinicConversion .Index )
2398
+ @ ArgumentClinic (name = "byteorder" , conversion = ClinicConversion .TString )
2399
+ @ ArgumentClinic (name = "signed" , conversion = ClinicConversion .Boolean , defaultValue = "false" )
2399
2400
@ GenerateNodeFactory
2400
2401
@ SuppressWarnings ("unused" )
2401
2402
@ TypeSystemReference (PythonArithmeticTypes .class )
2402
- public abstract static class ToBytesNode extends PythonBuiltinNode {
2403
-
2404
- public abstract PBytes execute (VirtualFrame frame , Object self , Object byteCount , Object StringOrder , Object signed );
2405
-
2406
- // used for obtaining int, which will be the size of created array
2407
- @ Child private ToBytesNode recursiveNode ;
2408
- @ Child private PyNumberAsSizeNode asSizeNode ;
2403
+ public abstract static class ToBytesNode extends PythonClinicBuiltinNode {
2409
2404
2410
2405
@ TruffleBoundary
2411
2406
private boolean isBigEndian (TruffleString order ) {
@@ -2418,20 +2413,11 @@ private boolean isBigEndian(TruffleString order) {
2418
2413
throw raise (PythonErrorType .ValueError , ErrorMessages .BYTEORDER_MUST_BE_LITTLE_OR_BIG );
2419
2414
}
2420
2415
2421
- @ Specialization
2422
- PBytes fromLong (long self , int byteCount , TruffleString byteorder , PNone signed ,
2423
- @ Bind ("this" ) Node inliningTarget ,
2424
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2425
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2426
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2427
- return fromLong (self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , negativeNumberProfile , overflowProfile );
2428
- }
2429
-
2430
2416
@ Specialization
2431
2417
PBytes fromLong (long self , int byteCount , TruffleString byteorder , boolean signed ,
2432
2418
@ Bind ("this" ) Node inliningTarget ,
2433
2419
@ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2434
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2420
+ @ Cached InlinedConditionProfile negativeNumberProfile ,
2435
2421
@ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2436
2422
if (negativeByteCountProfile .profile (inliningTarget , byteCount < 0 )) {
2437
2423
throw raise (PythonErrorType .ValueError , ErrorMessages .MESSAGE_LENGTH_ARGUMENT );
@@ -2489,52 +2475,6 @@ public static byte[] fromLong(long self, int byteCount, boolean isBigEndian, boo
2489
2475
return bytes ;
2490
2476
}
2491
2477
2492
- @ Specialization
2493
- PBytes fromLongLong (VirtualFrame frame , long self , long byteCount , TruffleString byteorder , PNone signed ,
2494
- @ Bind ("this" ) Node inliningTarget ,
2495
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2496
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2497
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2498
- return fromLongLong (frame , self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , negativeNumberProfile , overflowProfile );
2499
- }
2500
-
2501
- @ Specialization
2502
- PBytes fromLongLong (VirtualFrame frame , long self , long byteCount , TruffleString byteorder , boolean signed ,
2503
- @ Bind ("this" ) Node inliningTarget ,
2504
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2505
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2506
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2507
- int count = asSize (frame , byteCount );
2508
- return fromLong (self , count , byteorder , signed , inliningTarget , negativeByteCountProfile , negativeNumberProfile , overflowProfile );
2509
- }
2510
-
2511
- @ Specialization
2512
- PBytes fromLongPInt (VirtualFrame frame , long self , PInt byteCount , TruffleString byteorder , PNone signed ,
2513
- @ Bind ("this" ) Node inliningTarget ,
2514
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2515
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2516
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2517
- return fromLongPInt (frame , self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , negativeNumberProfile , overflowProfile );
2518
- }
2519
-
2520
- @ Specialization
2521
- PBytes fromLongPInt (VirtualFrame frame , long self , PInt byteCount , TruffleString byteorder , boolean signed ,
2522
- @ Bind ("this" ) Node inliningTarget ,
2523
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2524
- @ Shared @ Cached InlinedConditionProfile negativeNumberProfile ,
2525
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2526
- int count = asSize (frame , byteCount );
2527
- return fromLong (self , count , byteorder , signed , inliningTarget , negativeByteCountProfile , negativeNumberProfile , overflowProfile );
2528
- }
2529
-
2530
- @ Specialization
2531
- PBytes fromPIntInt (PInt self , int byteCount , TruffleString byteorder , PNone signed ,
2532
- @ Bind ("this" ) Node inliningTarget ,
2533
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2534
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2535
- return fromPIntInt (self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , overflowProfile );
2536
- }
2537
-
2538
2478
@ TruffleBoundary
2539
2479
private static byte getSingByte (BigInteger value , boolean signed ,
2540
2480
PRaiseNode raise ) {
@@ -2625,69 +2565,9 @@ public static byte[] fromBigInteger(PInt self, int byteCount, boolean isBigEndia
2625
2565
}
2626
2566
}
2627
2567
2628
- @ Specialization
2629
- PBytes fromPIntLong (VirtualFrame frame , PInt self , long byteCount , TruffleString byteorder , PNone signed ,
2630
- @ Bind ("this" ) Node inliningTarget ,
2631
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2632
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2633
- return fromPIntLong (frame , self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , overflowProfile );
2634
- }
2635
-
2636
- @ Specialization
2637
- PBytes fromPIntLong (VirtualFrame frame , PInt self , long byteCount , TruffleString byteorder , boolean signed ,
2638
- @ Bind ("this" ) Node inliningTarget ,
2639
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2640
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2641
- int count = asSize (frame , byteCount );
2642
- return fromPIntInt (self , count , byteorder , signed , inliningTarget , negativeByteCountProfile , overflowProfile );
2643
- }
2644
-
2645
- @ Specialization
2646
- PBytes fromPIntPInt (VirtualFrame frame , PInt self , PInt byteCount , TruffleString byteorder , PNone signed ,
2647
- @ Bind ("this" ) Node inliningTarget ,
2648
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2649
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2650
- return fromPIntPInt (frame , self , byteCount , byteorder , false , inliningTarget , negativeByteCountProfile , overflowProfile );
2651
- }
2652
-
2653
- @ Specialization
2654
- PBytes fromPIntPInt (VirtualFrame frame , PInt self , PInt byteCount , TruffleString byteorder , boolean signed ,
2655
- @ Bind ("this" ) Node inliningTarget ,
2656
- @ Shared @ Cached InlinedConditionProfile negativeByteCountProfile ,
2657
- @ Shared @ Cached InlinedConditionProfile overflowProfile ) {
2658
- int count = asSize (frame , byteCount );
2659
- return fromPIntInt (self , count , byteorder , signed , inliningTarget , negativeByteCountProfile , overflowProfile );
2660
- }
2661
-
2662
- static boolean isNumber (Object value ) {
2663
- return value instanceof Integer || value instanceof Long || value instanceof PInt ;
2664
- }
2665
-
2666
- @ Fallback
2667
- PBytes general (VirtualFrame frame , Object self , Object byteCount , Object byteorder , Object oSigned ) {
2668
- int count = asSize (frame , byteCount );
2669
- if (!PGuards .isString (byteorder )) {
2670
- throw raise (PythonErrorType .TypeError , ErrorMessages .ARG_D_MUST_BE_S_NOT_P , "to_bytes()" , 2 , "str" , byteorder );
2671
- }
2672
- boolean signed = oSigned instanceof Boolean && (boolean ) oSigned ;
2673
- if (recursiveNode == null ) {
2674
- CompilerDirectives .transferToInterpreterAndInvalidate ();
2675
- recursiveNode = insert (create ());
2676
- }
2677
- return recursiveNode .execute (frame , self , count , byteorder , signed );
2678
- }
2679
-
2680
- private int asSize (VirtualFrame frame , Object object ) {
2681
- if (asSizeNode == null ) {
2682
- CompilerDirectives .transferToInterpreterAndInvalidate ();
2683
- asSizeNode = insert (PyNumberAsSizeNode .create ());
2684
- }
2685
- return asSizeNode .executeExact (frame , object );
2686
- }
2687
-
2688
- @ NeverDefault
2689
- public static ToBytesNode create () {
2690
- return IntBuiltinsFactory .ToBytesNodeFactory .create (null );
2568
+ @ Override
2569
+ protected ArgumentClinicProvider getArgumentClinic () {
2570
+ return IntBuiltinsClinicProviders .ToBytesNodeClinicProviderGen .INSTANCE ;
2691
2571
}
2692
2572
}
2693
2573
0 commit comments