1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
|
"""The builtin int type (W_AbstractInt) and the base impl (W_IntObject)
based on rpython ints.
In order to have the same behavior running on CPython, and after RPython
translation this module uses rarithmetic.ovfcheck to explicitly check
for overflows, something CPython does not do anymore.
"""
import operator
import sys
from rpython.rlib import jit
from rpython.rlib.objectmodel import instantiate, enforceargs
from rpython.rlib.rarithmetic import (
LONG_BIT, intmask, is_valid_int, ovfcheck, r_longlong, r_uint,
string_to_int)
from rpython.rlib.rbigint import (
InvalidEndiannessError, InvalidSignednessError, rbigint)
from rpython.rlib.rfloat import DBL_MANT_DIG
from rpython.rlib.rstring import (
ParseStringError, ParseStringOverflowError)
from rpython.tool.sourcetools import func_renamer, func_with_new_name
from pypy.interpreter import typedef
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.gateway import (
WrappedDefault, applevel, interp2app, interpindirect2app, unwrap_spec)
from pypy.interpreter.typedef import TypeDef
from pypy.objspace.std import newformat
from pypy.objspace.std.util import (
BINARY_OPS, CMP_OPS, COMMUTATIVE_OPS, IDTAG_INT, IDTAG_SHIFT, wrap_parsestringerror)
SENTINEL = object()
HASH_BITS = 61 if sys.maxsize > 2 ** 31 - 1 else 31
HASH_MODULUS = 2 ** HASH_BITS - 1
class W_AbstractIntObject(W_Root):
__slots__ = ()
def is_w(self, space, w_other):
from pypy.objspace.std.boolobject import W_BoolObject
if (not isinstance(w_other, W_AbstractIntObject) or
isinstance(w_other, W_BoolObject)):
return False
if self.user_overridden_class or w_other.user_overridden_class:
return self is w_other
x = space.bigint_w(self, allow_conversion=False)
y = space.bigint_w(w_other, allow_conversion=False)
return x.eq(y)
def immutable_unique_id(self, space):
if self.user_overridden_class:
return None
b = space.bigint_w(self)
b = b.lshift(IDTAG_SHIFT).int_or_(IDTAG_INT)
return space.newlong_from_rbigint(b)
@staticmethod
@unwrap_spec(byteorder='text', signed=bool)
def descr_from_bytes(space, w_inttype, w_obj, byteorder, signed=False):
"""int.from_bytes(bytes, byteorder, *, signed=False) -> int
Return the integer represented by the given array of bytes.
The bytes argument must either support the buffer protocol or be
an iterable object producing bytes. Bytes and bytearray are
examples of built-in objects that support the buffer protocol.
The byteorder argument determines the byte order used to
represent the integer. If byteorder is 'big', the most
significant byte is at the beginning of the byte array. If
byteorder is 'little', the most significant byte is at the end
of the byte array. To request the native byte order of the host
system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument indicates whether two's
complement is used to represent the integer.
"""
from pypy.objspace.std.bytesobject import makebytesdata_w
bytes = makebytesdata_w(space, w_obj)
try:
bigint = rbigint.frombytes(bytes, byteorder=byteorder,
signed=signed)
except InvalidEndiannessError:
raise oefmt(space.w_ValueError,
"byteorder must be either 'little' or 'big'")
try:
as_int = bigint.toint()
except OverflowError:
w_obj = space.newlong_from_rbigint(bigint)
else:
w_obj = space.newint(as_int)
if not space.is_w(w_inttype, space.w_int):
# That's what from_bytes() does in CPython 3.5.2 too
w_obj = space.call_function(w_inttype, w_obj)
return w_obj
@unwrap_spec(length=int, byteorder='text', signed=bool)
def descr_to_bytes(self, space, length, byteorder, signed=False):
"""to_bytes(...)
int.to_bytes(length, byteorder, *, signed=False) -> bytes
Return an array of bytes representing an integer.
The integer is represented using length bytes. An OverflowError
is raised if the integer is not representable with the given
number of bytes.
The byteorder argument determines the byte order used to
represent the integer. If byteorder is 'big', the most
significant byte is at the beginning of the byte array. If
byteorder is 'little', the most significant byte is at the end
of the byte array. To request the native byte order of the host
system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument determines whether two's
complement is used to represent the integer. If signed is False
and a negative integer is given, an OverflowError is raised.
"""
bigint = space.bigint_w(self)
try:
byte_string = bigint.tobytes(length, byteorder=byteorder,
signed=signed)
except InvalidEndiannessError:
raise oefmt(space.w_ValueError,
"byteorder must be either 'little' or 'big'")
except InvalidSignednessError:
raise oefmt(space.w_OverflowError,
"can't convert negative int to unsigned")
except OverflowError:
raise oefmt(space.w_OverflowError, "int too big to convert")
return space.newbytes(byte_string)
def descr_round(self, space, w_ndigits=None):
"""Rounding an Integral returns itself.
Rounding with an ndigits argument also returns an integer.
"""
# To round an integer m to the nearest 10**n (n positive), we
# make use of the divmod_near operation, defined by:
#
# divmod_near(a, b) = (q, r)
#
# where q is the nearest integer to the quotient a / b (the
# nearest even integer in the case of a tie) and r == a - q * b.
# Hence q * b = a - r is the nearest multiple of b to a,
# preferring even multiples in the case of a tie.
#
# So the nearest multiple of 10**n to m is:
#
# m - divmod_near(m, 10**n)[1]
# XXX: since divmod_near is pure python we can probably remove
# the longs used here. or this could at least likely be more
# efficient for W_IntObject
from pypy.objspace.std.longobject import newlong
if space.is_none(w_ndigits):
return self.int(space)
ndigits = space.bigint_w(space.index(w_ndigits))
# if ndigits >= 0 then no rounding is necessary; return self
# unchanged
if ndigits.ge(rbigint.fromint(0)):
return self.int(space)
# result = self - divmod_near(self, 10 ** -ndigits)[1]
right = rbigint.fromint(10).pow(ndigits.neg())
w_tuple = divmod_near(space, self, newlong(space, right))
_, w_r = space.fixedview(w_tuple, 2)
return space.sub(self, w_r)
def _self_unaryop(opname, doc=None):
@func_renamer('descr_' + opname)
def descr_unaryop(self, space):
return self.int(space)
descr_unaryop.__doc__ = doc
return descr_unaryop
descr_conjugate = _self_unaryop(
'conjugate', "Returns self, the complex conjugate of any int.")
descr_pos = _self_unaryop('pos', "x.__pos__() <==> +x")
descr_index = _self_unaryop('index',
"x[y:z] <==> x[y.__index__():z.__index__()]")
descr_trunc = _self_unaryop('trunc',
"Truncating an Integral returns itself.")
descr_floor = _self_unaryop('floor', "Flooring an Integral returns itself.")
descr_ceil = _self_unaryop('ceil', "Ceiling of an Integral returns itself.")
descr_get_numerator = _self_unaryop('get_numerator')
descr_get_real = _self_unaryop('get_real')
def descr_get_denominator(self, space):
return wrapint(space, 1)
def descr_get_imag(self, space):
return wrapint(space, 0)
def int(self, space):
"""x.__int__() <==> int(x)"""
raise NotImplementedError
def asbigint(self):
raise NotImplementedError
def descr_format(self, space, w_format_spec):
raise NotImplementedError
def descr_pow(self, space, w_exponent, w_modulus=None):
"""x.__pow__(y[, z]) <==> pow(x, y[, z])"""
raise NotImplementedError
descr_rpow = func_with_new_name(descr_pow, 'descr_rpow')
descr_rpow.__doc__ = "y.__rpow__(x[, z]) <==> pow(x, y[, z])"
def _abstract_unaryop(opname, doc=SENTINEL):
if doc is SENTINEL:
doc = 'x.__%s__() <==> %s(x)' % (opname, opname)
@func_renamer('descr_' + opname)
def descr_unaryop(self, space):
raise NotImplementedError
descr_unaryop.__doc__ = doc
return descr_unaryop
descr_repr = _abstract_unaryop('repr')
descr_str = _abstract_unaryop('str')
descr_bit_length = _abstract_unaryop('bit_length', """\
int.bit_length() -> int
Number of bits necessary to represent self in binary.
>>> bin(37)
'0b100101'
>>> (37).bit_length()
6""")
descr_hash = _abstract_unaryop('hash')
descr_getnewargs = _abstract_unaryop('getnewargs', None)
descr_float = _abstract_unaryop('float')
descr_neg = _abstract_unaryop('neg', "x.__neg__() <==> -x")
descr_abs = _abstract_unaryop('abs')
descr_bool = _abstract_unaryop('bool', "x.__bool__() <==> x != 0")
descr_invert = _abstract_unaryop('invert', "x.__invert__() <==> ~x")
def _abstract_cmpop(opname):
@func_renamer('descr_' + opname)
def descr_cmp(self, space, w_other):
raise NotImplementedError
descr_cmp.__doc__ = 'x.__%s__(y) <==> x%sy' % (opname, CMP_OPS[opname])
return descr_cmp
descr_lt = _abstract_cmpop('lt')
descr_le = _abstract_cmpop('le')
descr_eq = _abstract_cmpop('eq')
descr_ne = _abstract_cmpop('ne')
descr_gt = _abstract_cmpop('gt')
descr_ge = _abstract_cmpop('ge')
def _abstract_binop(opname):
oper = BINARY_OPS.get(opname)
if oper == '%':
oper = '%%'
oper = '%s(%%s, %%s)' % opname if not oper else '%%s%s%%s' % oper
@func_renamer('descr_' + opname)
def descr_binop(self, space, w_other):
raise NotImplementedError
descr_binop.__doc__ = "x.__%s__(y) <==> %s" % (opname,
oper % ('x', 'y'))
descr_rbinop = func_with_new_name(descr_binop, 'descr_r' + opname)
descr_rbinop.__doc__ = "x.__r%s__(y) <==> %s" % (opname,
oper % ('y', 'x'))
return descr_binop, descr_rbinop
descr_add, descr_radd = _abstract_binop('add')
descr_sub, descr_rsub = _abstract_binop('sub')
descr_mul, descr_rmul = _abstract_binop('mul')
descr_matmul, descr_rmatmul = _abstract_binop('matmul')
descr_and, descr_rand = _abstract_binop('and')
descr_or, descr_ror = _abstract_binop('or')
descr_xor, descr_rxor = _abstract_binop('xor')
descr_lshift, descr_rlshift = _abstract_binop('lshift')
descr_rshift, descr_rrshift = _abstract_binop('rshift')
descr_floordiv, descr_rfloordiv = _abstract_binop('floordiv')
descr_truediv, descr_rtruediv = _abstract_binop('truediv')
descr_mod, descr_rmod = _abstract_binop('mod')
descr_divmod, descr_rdivmod = _abstract_binop('divmod')
def descr_as_integer_ratio(self, space):
"""
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int
and with a positive denominator.
>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
"""
return space.newtuple([self.int(space), space.newint(1)])
def _floordiv(space, x, y):
try:
z = ovfcheck(x // y)
except ZeroDivisionError:
raise oefmt(space.w_ZeroDivisionError,
"integer division or modulo by zero")
return wrapint(space, z)
def _truediv(space, x, y):
if not y:
raise oefmt(space.w_ZeroDivisionError, "division by zero")
if (DBL_MANT_DIG < LONG_BIT and
(r_uint(abs(x)) >> DBL_MANT_DIG or r_uint(abs(y)) >> DBL_MANT_DIG)):
# large x or y, use long arithmetic
raise OverflowError
# both ints can be exactly represented as doubles, do a
# floating-point division
a = float(x)
b = float(y)
return space.newfloat(a / b)
def _mod(space, x, y):
try:
z = ovfcheck(x % y)
except ZeroDivisionError:
raise oefmt(space.w_ZeroDivisionError, "integer modulo by zero")
return wrapint(space, z)
def _divmod(space, x, y):
try:
z = ovfcheck(x // y)
except ZeroDivisionError:
raise oefmt(space.w_ZeroDivisionError, "integer divmod by zero")
# no overflow possible
m = x % y
return space.newtuple([space.newint(z), space.newint(m)])
def _divmod_ovf2small(space, x, y):
from pypy.objspace.std.smalllongobject import W_SmallLongObject
a = r_longlong(x)
b = r_longlong(y)
return space.newtuple([W_SmallLongObject(a // b),
W_SmallLongObject(a % b)])
def _lshift(space, a, b):
if r_uint(b) < LONG_BIT: # 0 <= b < LONG_BIT
c = ovfcheck(a << b)
return wrapint(space, c)
if b < 0:
raise oefmt(space.w_ValueError, "negative shift count")
# b >= LONG_BIT
if a == 0:
return wrapint(space, a)
raise OverflowError
def _lshift_ovf2small(space, a, b):
from pypy.objspace.std.smalllongobject import W_SmallLongObject
w_a = W_SmallLongObject.fromint(a)
w_b = W_SmallLongObject.fromint(b)
return w_a.descr_lshift(space, w_b)
def _rshift(space, a, b):
if r_uint(b) >= LONG_BIT: # not (0 <= b < LONG_BIT)
if b < 0:
raise oefmt(space.w_ValueError, "negative shift count")
# b >= LONG_BIT
if a == 0:
return wrapint(space, a)
a = -1 if a < 0 else 0
else:
a = a >> b
return wrapint(space, a)
def _pow(space, iv, iw, iz):
"""Helper for pow"""
if iz == 0:
return _pow_nomod(iv, iw)
else:
return _pow_mod(space, iv, iw, iz)
@jit.look_inside_iff(lambda iv, iw: jit.isconstant(iw))
def _pow_nomod(iv, iw):
if iw <= 0:
if iw == 0:
return 1
# bounce it, since it always returns float
raise ValueError
temp = iv
ix = 1
while True:
if iw & 1:
try:
ix = ovfcheck(ix * temp)
except OverflowError:
raise
iw >>= 1 # Shift exponent down by 1 bit
if iw == 0:
break
try:
temp = ovfcheck(temp * temp) # Square the value of temp
except OverflowError:
raise
return ix
@jit.look_inside_iff(lambda space, iv, iw, iz:
jit.isconstant(iw) and jit.isconstant(iz))
def _pow_mod(space, iv, iw, iz):
from rpython.rlib.rarithmetic import mulmod
if iw == 0:
return 1 % iz # != 1, for iz == 1 or iz < 0
if iz < 0:
try:
iz = ovfcheck(-iz)
except OverflowError:
raise
iz_negative = True
else:
iz_negative = False
if iw <= 0:
w_iv = invmod(space, space.newint(iv), space.newint(iz))
assert isinstance(w_iv, W_IntObject)
iv = w_iv.intval
try:
iw = ovfcheck(-iw)
except OverflowError:
raise
temp = iv
ix = 1
while True:
if iw & 1:
ix = mulmod(ix, temp, iz)
iw >>= 1 # Shift exponent down by 1 bit
if iw == 0:
break
temp = mulmod(temp, temp, iz)
if iz_negative and ix > 0:
ix -= iz
return ix
def _pow_ovf2long(space, iv, w_iv, iw, w_iw, w_modulus):
if space.is_none(w_modulus) and _recover_with_smalllong(space):
from pypy.objspace.std.smalllongobject import _pow as _pow_small
try:
# XXX: shouldn't have to pass r_longlong(0) here (see
# 4fa4c6b93a84)
return _pow_small(space, r_longlong(iv), iw, r_longlong(0))
except (OverflowError, ValueError):
pass
from pypy.objspace.std.longobject import W_LongObject, W_AbstractLongObject
if w_iv is None or not isinstance(w_iv, W_AbstractLongObject):
w_iv = W_LongObject.fromint(space, iv)
if w_iw is None or not isinstance(w_iw, W_AbstractLongObject):
w_iw = W_LongObject.fromint(space, iw)
return w_iv.descr_pow(space, w_iw, w_modulus)
def _make_ovf2long(opname, ovf2small=None):
op = getattr(operator, opname, None)
assert op or ovf2small
def ovf2long(space, x, w_x, y, w_y):
"""Handle overflowing to smalllong or long"""
if _recover_with_smalllong(space):
if ovf2small:
return ovf2small(space, x, y)
# Assume a generic operation without an explicit ovf2small
# handler
from pypy.objspace.std.smalllongobject import W_SmallLongObject
a = r_longlong(x)
b = r_longlong(y)
return W_SmallLongObject(op(a, b))
from pypy.objspace.std.longobject import W_LongObject, W_AbstractLongObject
if w_x is None or not isinstance(w_x, W_AbstractLongObject):
w_x = W_LongObject.fromint(space, x)
if w_y is None or not isinstance(w_y, W_AbstractLongObject):
w_y = W_LongObject.fromint(space, y)
return getattr(w_x, 'descr_' + opname)(space, w_y)
return ovf2long
class W_IntObject(W_AbstractIntObject):
__slots__ = 'intval'
_immutable_fields_ = ['intval']
def __init__(self, intval):
assert is_valid_int(intval)
self.intval = int(intval)
def __repr__(self):
"""representation for debugging purposes"""
return "%s(%d)" % (self.__class__.__name__, self.intval)
def is_w(self, space, w_other):
from pypy.objspace.std.boolobject import W_BoolObject
if (not isinstance(w_other, W_AbstractIntObject) or
isinstance(w_other, W_BoolObject)):
return False
if self.user_overridden_class or w_other.user_overridden_class:
return self is w_other
x = self.intval
try:
y = space.int_w(w_other)
except OperationError as e:
if e.match(space, space.w_OverflowError):
return False
raise
return x == y
def int_w(self, space, allow_conversion=True):
return self.intval
def _int_w(self, space):
return self.intval
unwrap = _int_w
def uint_w(self, space):
intval = self.intval
if intval < 0:
raise oefmt(space.w_ValueError,
"cannot convert negative integer to unsigned")
return r_uint(intval)
def bigint_w(self, space, allow_conversion=True):
return self.asbigint()
def _bigint_w(self, space):
return self.asbigint()
def float_w(self, space, allow_conversion=True):
return float(self.intval)
# note that we do NOT implement _float_w, because __float__ cannot return
# an int
def int(self, space):
if type(self) is W_IntObject:
return self
if not space.is_overloaded(self, space.w_int, '__int__'):
return space.newint(self.intval)
return W_Root.int(self, space)
def asbigint(self):
return rbigint.fromint(self.intval)
@staticmethod
@unwrap_spec(w_x=WrappedDefault(0))
def descr_new(space, w_inttype, w_x, __posonly__, w_base=None):
"Create and return a new object. See help(type) for accurate signature."
return _new_int(space, w_inttype, w_x, w_base)
def descr_hash(self, space):
return space.newint(_hash_int(self.intval))
def as_w_long(self, space):
return space.newlong(self.intval)
def descr_bool(self, space):
return space.newbool(self.intval != 0)
def descr_invert(self, space):
return wrapint(space, ~self.intval)
def descr_neg(self, space):
a = self.intval
try:
b = ovfcheck(-a)
except OverflowError:
if _recover_with_smalllong(space):
from pypy.objspace.std.smalllongobject import W_SmallLongObject
x = r_longlong(a)
return W_SmallLongObject(-x)
return self.as_w_long(space).descr_neg(space)
return wrapint(space, b)
def descr_abs(self, space):
pos = self.intval >= 0
return self.int(space) if pos else self.descr_neg(space)
def descr_float(self, space):
a = self.intval
x = float(a)
return space.newfloat(x)
def descr_getnewargs(self, space):
return space.newtuple([wrapint(space, self.intval)])
def descr_bit_length(self, space):
val = self.intval
bits = 0
if val < 0:
# warning, "-val" overflows here
val = -((val + 1) >> 1)
bits = 1
while val:
bits += 1
val >>= 1
return space.newint(bits)
def descr_repr(self, space):
res = str(self.intval)
return space.newutf8(res, len(res)) # res is always ASCII
descr_str = func_with_new_name(descr_repr, 'descr_str')
def descr_format(self, space, w_format_spec):
return newformat.run_formatter(space, w_format_spec,
"format_int_or_long", self,
newformat.INT_KIND)
@unwrap_spec(w_modulus=WrappedDefault(None))
def descr_pow(self, space, w_exponent, w_modulus=None):
if isinstance(w_exponent, W_IntObject):
y = w_exponent.intval
elif isinstance(w_exponent, W_AbstractIntObject):
self = self.as_w_long(space)
return self.descr_pow(space, w_exponent, w_modulus)
else:
return space.w_NotImplemented
x = self.intval
y = w_exponent.intval
if space.is_none(w_modulus):
z = 0
elif isinstance(w_modulus, W_IntObject):
z = w_modulus.intval
if z == 0:
raise oefmt(space.w_ValueError,
"pow() 3rd argument cannot be 0")
else:
# can't return NotImplemented (space.pow doesn't do full
# ternary, i.e. w_modulus.__zpow__(self, w_exponent)), so
# handle it ourselves
return _pow_ovf2long(space, x, self, y, w_exponent, w_modulus)
try:
result = _pow(space, x, y, z)
except OverflowError:
return _pow_ovf2long(space, x, self, y, w_exponent, w_modulus)
except ValueError:
# float result, so let avoid a roundtrip in rbigint.
self = self.descr_float(space)
w_exponent = w_exponent.descr_float(space)
return space.pow(self, w_exponent, space.w_None)
return space.newint(result)
@unwrap_spec(w_modulus=WrappedDefault(None))
def descr_rpow(self, space, w_base, w_modulus=None):
if isinstance(w_base, W_IntObject):
return w_base.descr_pow(space, self, w_modulus)
elif isinstance(w_base, W_AbstractIntObject):
self = self.as_w_long(space)
return self.descr_rpow(space, self, w_modulus)
return space.w_NotImplemented
def _make_descr_cmp(opname):
op = getattr(operator, opname)
descr_name = 'descr_' + opname
@func_renamer(descr_name)
def descr_cmp(self, space, w_other):
if isinstance(w_other, W_IntObject):
i = self.intval
j = w_other.intval
return space.newbool(op(i, j))
elif isinstance(w_other, W_AbstractIntObject):
self = self.as_w_long(space)
return getattr(self, descr_name)(space, w_other)
return space.w_NotImplemented
return descr_cmp
descr_lt = _make_descr_cmp('lt')
descr_le = _make_descr_cmp('le')
descr_eq = _make_descr_cmp('eq')
descr_ne = _make_descr_cmp('ne')
descr_gt = _make_descr_cmp('gt')
descr_ge = _make_descr_cmp('ge')
def _make_generic_descr_binop(opname, ovf=True):
op = getattr(operator,
opname + '_' if opname in ('and', 'or') else opname)
descr_name, descr_rname = 'descr_' + opname, 'descr_r' + opname
if ovf:
ovf2long = _make_ovf2long(opname)
@func_renamer(descr_name)
def descr_binop(self, space, w_other):
if isinstance(w_other, W_IntObject):
x = self.intval
y = w_other.intval
if ovf:
try:
z = ovfcheck(op(x, y))
except OverflowError:
return ovf2long(space, x, self, y, w_other)
else:
z = op(x, y)
return wrapint(space, z)
elif isinstance(w_other, W_AbstractIntObject):
self = self.as_w_long(space)
return getattr(self, descr_name)(space, w_other)
return space.w_NotImplemented
if opname in COMMUTATIVE_OPS:
@func_renamer(descr_rname)
def descr_rbinop(self, space, w_other):
return descr_binop(self, space, w_other)
return descr_binop, descr_rbinop
@func_renamer(descr_rname)
def descr_rbinop(self, space, w_other):
if isinstance(w_other, W_IntObject):
x = self.intval
y = w_other.intval
if ovf:
try:
z = ovfcheck(op(y, x))
except OverflowError:
return ovf2long(space, y, w_other, x, self) # XXX write a test
else:
z = op(y, x)
return wrapint(space, z)
elif isinstance(w_other, W_AbstractIntObject):
self = self.as_w_long(space)
return getattr(self, descr_rname)(space, w_other)
return space.w_NotImplemented
return descr_binop, descr_rbinop
descr_add, descr_radd = _make_generic_descr_binop('add')
descr_sub, descr_rsub = _make_generic_descr_binop('sub')
descr_mul, descr_rmul = _make_generic_descr_binop('mul')
descr_and, descr_rand = _make_generic_descr_binop('and', ovf=False)
descr_or, descr_ror = _make_generic_descr_binop('or', ovf=False)
descr_xor, descr_rxor = _make_generic_descr_binop('xor', ovf=False)
def _make_descr_binop(func, ovf=True, ovf2small=None, ovf_func=None):
opname = func.__name__[1:]
descr_name, descr_rname = 'descr_' + opname, 'descr_r' + opname
if ovf:
if ovf_func:
ovf2long = ovf_func
assert not ovf2small # must be part of ovf_func
else:
ovf2long = _make_ovf2long(opname, ovf2small)
@func_renamer(descr_name)
def descr_binop(self, space, w_other):
if isinstance(w_other, W_IntObject):
x = self.intval
y = w_other.intval
if ovf:
try:
return func(space, x, y)
except OverflowError:
return ovf2long(space, x, self, y, w_other)
else:
return func(space, x, y)
elif isinstance(w_other, W_AbstractIntObject):
self = self.as_w_long(space)
return getattr(self, descr_name)(space, w_other)
return space.w_NotImplemented
@func_renamer(descr_rname)
def descr_rbinop(self, space, w_other):
if isinstance(w_other, W_IntObject):
x = self.intval
y = w_other.intval
if ovf:
try:
return func(space, y, x)
except OverflowError:
return ovf2long(space, y, w_other, x, self)
else:
return func(space, y, x)
elif isinstance(w_other, W_AbstractIntObject):
self = self.as_w_long(space)
return getattr(self, descr_rname)(space, w_other)
return space.w_NotImplemented
return descr_binop, descr_rbinop
def _ovf2long_lshift(space, x, w_x, y, w_y):
if _recover_with_smalllong(space):
return _lshift_ovf2small(space, x, y)
from pypy.objspace.std.longobject import W_LongObject, W_AbstractLongObject
if w_x is None or not isinstance(w_x, W_AbstractLongObject):
w_x = W_LongObject.fromint(space, x)
# crucially, *don't* convert w_y to W_LongObject, it will just be
# converted back (huge lshifts always overflow)
return w_x._int_lshift(space, y)
descr_lshift, descr_rlshift = _make_descr_binop(
_lshift, ovf_func=_ovf2long_lshift)
descr_rshift, descr_rrshift = _make_descr_binop(_rshift, ovf=False)
descr_floordiv, descr_rfloordiv = _make_descr_binop(_floordiv)
descr_truediv, descr_rtruediv = _make_descr_binop(_truediv)
descr_mod, descr_rmod = _make_descr_binop(_mod)
descr_divmod, descr_rdivmod = _make_descr_binop(
_divmod, ovf2small=_divmod_ovf2small)
def setup_prebuilt(space):
if space.config.objspace.std.withprebuiltint:
W_IntObject.PREBUILT = []
for i in range(space.config.objspace.std.prebuiltintfrom,
space.config.objspace.std.prebuiltintto):
W_IntObject.PREBUILT.append(W_IntObject(i))
else:
W_IntObject.PREBUILT = None
def wrapint(space, x):
if not space.config.objspace.std.withprebuiltint:
return W_IntObject(x)
lower = space.config.objspace.std.prebuiltintfrom
upper = space.config.objspace.std.prebuiltintto
# use r_uint to perform a single comparison (this whole function is
# getting inlined into every caller so keeping the branching to a
# minimum is a good idea)
index = r_uint(x) - r_uint(lower)
if index >= r_uint(upper - lower):
w_res = instantiate(W_IntObject)
else:
w_res = W_IntObject.PREBUILT[index]
# obscure hack to help the CPU cache: we store 'x' even into a
# prebuilt integer's intval. This makes sure that the intval field
# is present in the cache in the common case where it is quickly
# reused. (we could use a prefetch hint if we had that)
w_res.intval = x
return w_res
divmod_near = applevel('''
def divmod_near(a, b):
"""Return a pair (q, r) such that a = b * q + r, and abs(r)
<= abs(b)/2, with equality possible only if q is even. In
other words, q == a / b, rounded to the nearest integer using
round-half-to-even."""
q, r = divmod(a, b)
# round up if either r / b > 0.5, or r / b == 0.5 and q is
# odd. The expression r / b > 0.5 is equivalent to 2 * r > b
# if b is positive, 2 * r < b if b negative.
greater_than_half = 2*r > b if b > 0 else 2*r < b
exactly_half = 2*r == b
if greater_than_half or exactly_half and q % 2 == 1:
q += 1
r -= b
return q, r
''', filename=__file__).interphook('divmod_near')
invmod = applevel('''
def invmod(a, n):
if n == 1:
return 0
if a == 1:
return 1
assert n >= 0
b, c = 1, 0
while n:
q, r = divmod(a, n)
a, b, c, n = n, c, b - q*c, r
# at this point a is the gcd of the original inputs
if a == 1:
return b
raise ValueError("base is not invertible for the given modulus")
''', filename=__file__).interphook("invmod")
def _recover_with_smalllong(space):
"""True if there is a chance that a SmallLong would fit when an Int
does not
"""
return (space.config.objspace.std.withsmalllong and
sys.maxint == 2147483647)
def _string_to_int_or_long(space, w_source, string, base=10):
try:
value = string_to_int(
string, base, allow_underscores=True, no_implicit_octal=True)
return wrapint(space, value)
except ParseStringError as e:
raise wrap_parsestringerror(space, e, w_source)
except ParseStringOverflowError as e:
return _retry_to_w_long(space, e.parser, w_source)
def _retry_to_w_long(space, parser, w_source):
from pypy.objspace.std.longobject import newbigint
parser.rewind()
try:
bigint = rbigint._from_numberstring_parser(parser)
except ParseStringError as e:
raise wrap_parsestringerror(space, e, w_source)
return newbigint(space, space.w_int, bigint)
def _new_int(space, w_inttype, w_x, w_base=None):
w_value = w_x # 'x' is the keyword argument name in CPython
if w_inttype is space.w_int:
return _new_baseint(space, w_x, w_base)
else:
w_tmp = _new_baseint(space, w_x, w_base)
return _as_subint(space, w_inttype, w_tmp)
def _new_baseint(space, w_value, w_base=None):
if w_base is None:
if space.is_w(space.type(w_value), space.w_int):
assert isinstance(w_value, W_AbstractIntObject)
return w_value
elif space.lookup(w_value, '__int__') is not None:
w_intvalue = space.int(w_value)
return _ensure_baseint(space, w_intvalue)
elif space.lookup(w_value, '__trunc__') is not None:
w_obj = space.trunc(w_value)
if not space.isinstance_w(w_obj, space.w_int):
try:
w_obj = space.int(w_obj)
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
w_obj = space.index(w_obj)
assert isinstance(w_obj, W_AbstractIntObject)
return _ensure_baseint(space, w_obj)
elif space.lookup(w_value, '__index__') is not None:
w_obj = space.index(w_value)
if not space.is_w(space.type(w_obj), space.w_int):
if space.isinstance_w(w_obj, space.w_int):
w_obj = space.int(w_obj)
else:
raise oefmt(space.w_TypeError,
"int() argument must be a string, a bytes-like "
"object or a number, not '%T'", w_value)
assert isinstance(w_obj, W_AbstractIntObject)
return _ensure_baseint(space, w_obj)
elif space.isinstance_w(w_value, space.w_unicode):
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
try:
b = unicode_to_decimal_w(space, w_value)
except Exception:
raise oefmt(space.w_ValueError,
'invalid literal for int() with base 10: %R',
w_value)
return _string_to_int_or_long(space, w_value, b)
elif (space.isinstance_w(w_value, space.w_bytearray) or
space.isinstance_w(w_value, space.w_bytes)):
return _string_to_int_or_long(space, w_value,
space.charbuf_w(w_value))
else:
# If object supports the buffer interface
try:
buf = space.charbuf_w(w_value)
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
raise oefmt(space.w_TypeError,
"int() argument must be a string, a bytes-like "
"object or a number, not '%T'", w_value)
else:
return _string_to_int_or_long(space, w_value, buf)
else:
try:
base = space.getindex_w(w_base, None)
except OperationError as e:
if not e.match(space, space.w_OverflowError):
raise
base = 37 # this raises the right error in string_to_bigint()
if space.isinstance_w(w_value, space.w_unicode):
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
try:
s = unicode_to_decimal_w(space, w_value)
except Exception:
raise oefmt(space.w_ValueError,
'invalid literal for int() with base %d: %R',
base, w_value)
elif (space.isinstance_w(w_value, space.w_bytes) or
space.isinstance_w(w_value, space.w_bytearray)):
s = space.charbuf_w(w_value)
else:
raise oefmt(space.w_TypeError,
"int() can't convert non-string with explicit base")
return _string_to_int_or_long(space, w_value, s, base)
@enforceargs(None, None, W_AbstractIntObject, typecheck=False)
def _as_subint(space, w_inttype, w_value):
from pypy.objspace.std.longobject import W_LongObject, newbigint
if space.config.objspace.std.withsmalllong:
from pypy.objspace.std.smalllongobject import W_SmallLongObject
else:
W_SmallLongObject = None
if type(w_value) is W_IntObject:
w_obj = space.allocate_instance(W_IntObject, w_inttype)
W_IntObject.__init__(w_obj, w_value.intval)
return w_obj
elif type(w_value) is W_LongObject:
return newbigint(space, w_inttype, w_value.num)
elif W_SmallLongObject and type(w_value) is W_SmallLongObject:
return newbigint(space, w_inttype, space.bigint_w(w_value))
@enforceargs(None, W_AbstractIntObject, typecheck=False)
def _ensure_baseint(space, w_intvalue):
from pypy.objspace.std.longobject import (
W_LongObject, W_AbstractLongObject, newlong)
if isinstance(w_intvalue, W_IntObject):
if type(w_intvalue) is not W_IntObject:
w_intvalue = wrapint(space, w_intvalue.intval)
return w_intvalue
elif isinstance(w_intvalue, W_AbstractLongObject):
if type(w_intvalue) is not W_LongObject:
w_intvalue = newlong(space, w_intvalue.asbigint())
return w_intvalue
else:
# shouldn't happen
raise oefmt(space.w_RuntimeError,
"internal error in int.__new__()")
W_AbstractIntObject.typedef = TypeDef("int",
__doc__ = """int([x]) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4""",
__new__ = interp2app(W_IntObject.descr_new),
numerator = typedef.GetSetProperty(
W_AbstractIntObject.descr_get_numerator,
doc="the numerator of a rational number in lowest terms"),
denominator = typedef.GetSetProperty(
W_AbstractIntObject.descr_get_denominator,
doc="the denominator of a rational number in lowest terms"),
real = typedef.GetSetProperty(
W_AbstractIntObject.descr_get_real,
doc="the real part of a complex number"),
imag = typedef.GetSetProperty(
W_AbstractIntObject.descr_get_imag,
doc="the imaginary part of a complex number"),
as_integer_ratio = interp2app(W_AbstractIntObject.descr_as_integer_ratio),
from_bytes = interp2app(W_AbstractIntObject.descr_from_bytes,
as_classmethod=True),
to_bytes = interpindirect2app(W_AbstractIntObject.descr_to_bytes),
__repr__ = interpindirect2app(W_AbstractIntObject.descr_repr),
__str__ = interpindirect2app(W_AbstractIntObject.descr_str),
conjugate = interpindirect2app(W_AbstractIntObject.descr_conjugate),
bit_length = interpindirect2app(W_AbstractIntObject.descr_bit_length),
__format__ = interpindirect2app(W_AbstractIntObject.descr_format),
__hash__ = interpindirect2app(W_AbstractIntObject.descr_hash),
__getnewargs__ = interpindirect2app(W_AbstractIntObject.descr_getnewargs),
__int__ = interpindirect2app(W_AbstractIntObject.int),
__index__ = interpindirect2app(W_AbstractIntObject.descr_index),
__trunc__ = interpindirect2app(W_AbstractIntObject.descr_trunc),
__float__ = interpindirect2app(W_AbstractIntObject.descr_float),
__round__ = interpindirect2app(W_AbstractIntObject.descr_round),
__pos__ = interpindirect2app(W_AbstractIntObject.descr_pos),
__neg__ = interpindirect2app(W_AbstractIntObject.descr_neg),
__abs__ = interpindirect2app(W_AbstractIntObject.descr_abs),
__bool__ = interpindirect2app(W_AbstractIntObject.descr_bool),
__invert__ = interpindirect2app(W_AbstractIntObject.descr_invert),
__floor__ = interpindirect2app(W_AbstractIntObject.descr_floor),
__ceil__ = interpindirect2app(W_AbstractIntObject.descr_ceil),
__lt__ = interpindirect2app(W_AbstractIntObject.descr_lt),
__le__ = interpindirect2app(W_AbstractIntObject.descr_le),
__eq__ = interpindirect2app(W_AbstractIntObject.descr_eq),
__ne__ = interpindirect2app(W_AbstractIntObject.descr_ne),
__gt__ = interpindirect2app(W_AbstractIntObject.descr_gt),
__ge__ = interpindirect2app(W_AbstractIntObject.descr_ge),
__add__ = interpindirect2app(W_AbstractIntObject.descr_add),
__radd__ = interpindirect2app(W_AbstractIntObject.descr_radd),
__sub__ = interpindirect2app(W_AbstractIntObject.descr_sub),
__rsub__ = interpindirect2app(W_AbstractIntObject.descr_rsub),
__mul__ = interpindirect2app(W_AbstractIntObject.descr_mul),
__rmul__ = interpindirect2app(W_AbstractIntObject.descr_rmul),
__and__ = interpindirect2app(W_AbstractIntObject.descr_and),
__rand__ = interpindirect2app(W_AbstractIntObject.descr_rand),
__or__ = interpindirect2app(W_AbstractIntObject.descr_or),
__ror__ = interpindirect2app(W_AbstractIntObject.descr_ror),
__xor__ = interpindirect2app(W_AbstractIntObject.descr_xor),
__rxor__ = interpindirect2app(W_AbstractIntObject.descr_rxor),
__lshift__ = interpindirect2app(W_AbstractIntObject.descr_lshift),
__rlshift__ = interpindirect2app(W_AbstractIntObject.descr_rlshift),
__rshift__ = interpindirect2app(W_AbstractIntObject.descr_rshift),
__rrshift__ = interpindirect2app(W_AbstractIntObject.descr_rrshift),
__floordiv__ = interpindirect2app(W_AbstractIntObject.descr_floordiv),
__rfloordiv__ = interpindirect2app(W_AbstractIntObject.descr_rfloordiv),
__truediv__ = interpindirect2app(W_AbstractIntObject.descr_truediv),
__rtruediv__ = interpindirect2app(W_AbstractIntObject.descr_rtruediv),
__mod__ = interpindirect2app(W_AbstractIntObject.descr_mod),
__rmod__ = interpindirect2app(W_AbstractIntObject.descr_rmod),
__divmod__ = interpindirect2app(W_AbstractIntObject.descr_divmod),
__rdivmod__ = interpindirect2app(W_AbstractIntObject.descr_rdivmod),
__pow__ = interpindirect2app(W_AbstractIntObject.descr_pow),
__rpow__ = interpindirect2app(W_AbstractIntObject.descr_rpow),
)
def _hash_int(a):
sign = 1
if a < 0:
sign = -1
a = -a
x = r_uint(a)
# efficient x % HASH_MODULUS: as HASH_MODULUS is a Mersenne
# prime
x = (x & HASH_MODULUS) + (x >> HASH_BITS)
if x >= HASH_MODULUS:
x -= HASH_MODULUS
h = intmask(intmask(x) * sign)
return h - (h == -1)
|