Permalink
Jul 28, 2018
Apr 3, 2020
Dec 18, 2019
Mar 10, 2018
Apr 23, 2018
Apr 23, 2018
Apr 23, 2018
Jul 28, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Feb 22, 2019
Aug 2, 2018
Mar 10, 2018
Feb 15, 2020
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Feb 22, 2019
Feb 22, 2019
Mar 10, 2018
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
May 19, 2017
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
Mar 10, 2018
Feb 11, 2018
Feb 15, 2020
Feb 11, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 22, 2019
Feb 22, 2019
Feb 10, 2018
Mar 10, 2018
Mar 23, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Feb 11, 2018
Feb 11, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Nov 28, 2018
Mar 10, 2018
Nov 28, 2018
Jun 14, 2018
Nov 2, 2019
Mar 10, 2018
Oct 27, 2019
Oct 27, 2019
Feb 22, 2019
Oct 22, 2019
Oct 22, 2019
Oct 27, 2018
Oct 22, 2019
Feb 11, 2018
Newer
100644
892 lines (805 sloc)
27.1 KB
11
function int_value(obj){
12
// Instances of int subclasses that call int.__new__(cls, value)
13
// have an attribute $brython_value set
14
return obj.$brython_value !== undefined ? obj.$brython_value : obj
35
int.as_integer_ratio = function(){
36
var $ = $B.args("as_integer_ratio", 1, {self:null}, ["self"],
37
arguments, {}, null, null)
38
return $B.$list([$.self, 1])
39
}
40
62
case "big":
63
var num = _bytes[_len - 1]
64
var _mult = 256
65
for(var i = _len - 2; i >= 0; i--){
66
// For operations, use the functions that can take or return
67
// big integers
68
num = $B.add($B.mul(_mult, _bytes[i]), num)
69
_mult = $B.mul(_mult,256)
70
}
71
if(! signed){return num}
72
if(_bytes[0] < 128){return num}
73
return $B.sub(num, _mult)
74
case "little":
75
var num = _bytes[0]
76
if(num >= 128){num = num - 256}
77
var _mult = 256
78
for(var i = 1; i < _len; i++){
79
num = $B.add($B.mul(_mult, _bytes[i]), num)
80
_mult = $B.mul(_mult, 256)
81
}
82
if(! signed){return num}
83
if(_bytes[_len - 1] < 128){return num}
84
return $B.sub(num, _mult)
90
int.to_bytes = function(){
91
var $ = $B.args("to_bytes", 3,
92
{self: null, len: null, byteorder: null},
93
["self", "len", "byteorder"],
94
arguments, {}, "args", "kw"),
95
self = $.self,
96
len = $.len,
97
byteorder = $.byteorder,
98
kwargs = $.kw
99
if(! _b_.isinstance(len, _b_.int)){
100
throw _b_.TypeError.$factory("integer argument expected, got " +
102
}
103
if(["little", "big"].indexOf(byteorder) == -1){
104
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
105
}
106
var signed = kwargs.$string_dict["signed"] || false,
107
res = []
108
109
if(self < 0){
110
if(! signed){
111
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
112
}
113
self = Math.pow(256, len) + self
114
}
115
var value = self
116
while(true){
117
var quotient = Math.floor(value / 256),
118
rest = value - 256 * quotient
119
res.push(rest)
120
if(quotient == 0){
121
break
122
}
123
value = quotient
124
}
125
if(res.length > len){
126
throw _b_.OverflowError.$factory("int too big to convert")
131
}
132
if(byteorder == "big"){res = res.reverse()}
133
return {
134
__class__: _b_.bytes,
135
source: res
136
}
141
int.__bool__ = function(self){
142
return int_value(self).valueOf() == 0 ? false : true
143
}
158
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
159
if(_b_.isinstance(other, _b_.complex)){
172
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
173
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
201
if(fmt.sign !== undefined){
202
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
203
res = fmt.sign + res
204
}
205
}
224
for(var i = 0; i < nb; i++){
225
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
256
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
281
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
282
$B.long_int.$factory(other)))
284
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
285
if(rlshift !== _b_.None){return rlshift(self)}
292
if(other.__class__ === $B.long_int){
293
return $B.long_int.__mod__($B.long_int.$factory(self), other)
294
}
297
if(other === false){other = 0}
298
else if(other === true){other = 1}
299
if(other == 0){throw _b_.ZeroDivisionError.$factory(
303
if(_b_.hasattr(other, "__rmod__")){
304
return $B.$getattr(other, "__rmod__")(self)
305
}
322
var res = self * other
323
if(res > $B.min_int && res < $B.max_int){return res}
324
else{
325
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
326
$B.long_int.$factory(other)))
327
}
337
return $B.make_complex(int.__mul__(self, other.$real),
338
int.__mul__(self, other.$imag))
343
var $temp = other.slice(0, other.length)
344
for(var i = 0; i < val; i++){res = res.concat($temp)}
348
if(_b_.hasattr(other, "__rmul__")){
349
return $B.$getattr(other, "__rmul__")(self)
350
}
354
int.__ne__ = function(self, other){
355
var res = int.__eq__(self, other)
356
return (res === _b_.NotImplemented) ? res : !res
357
}
358
362
if(cls === undefined){
363
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
365
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
366
}
367
if(cls === int){return int.$factory(value)}
368
return {
369
__class__: cls,
377
function extended_euclidean(a, b){
378
var d, u, v
379
if(b == 0){
380
return [a, 1, 0]
381
}else{
382
[d, u, v] = extended_euclidean(b, a % b)
383
return [d, v, u - Math.floor(a / b) * v]
384
}
385
}
386
389
other = int_value(other)
390
switch(other.valueOf()) {
391
case 0:
392
return int.$factory(1)
393
case 1:
394
return int.$factory(self.valueOf())
397
// If z is provided, the algorithm is faster than computing
398
// self ** other then applying the modulo z
399
if(z == 1){return 0}
400
var result = 1,
401
base = self % z,
402
exponent = other,
403
long_int = $B.long_int
404
if(exponent < 0){
405
var gcd, inv, _
406
[gcd, inv, _] = extended_euclidean(self, z)
407
if(gcd !== 1){
408
throw _b_.ValueError.$factory("not relative primes: " +
409
self + ' and ' + z)
410
}
411
return int.__pow__(inv, -exponent, z)
412
}
413
while(exponent > 0){
414
if(exponent % 2 == 1){
415
if(result * base > $B.max_int){
416
result = long_int.__mul__(
417
long_int.$factory(result),
418
long_int.$factory(base))
419
result = long_int.__mod__(result, z)
420
}else{
421
result = (result * base) % z
422
}
423
}
424
exponent = exponent >> 1
425
if(base * base > $B.max_int){
426
base = long_int.__mul__(long_int.$factory(base),
427
long_int.$factory(base))
428
base = long_int.__mod__(base, z)
429
}else{
430
base = (base * base) % z
431
}
435
var res = Math.pow(self.valueOf(), other.valueOf())
436
if(res > $B.min_int && res < $B.max_int){return res}
439
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
440
$B.long_int.$factory(other)))
454
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
455
if(rpow !== _b_.None){
456
return rpow(self)
457
}
470
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
471
$B.long_int.$factory(other)))
473
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
474
if(rrshift !== _b_.None){return rrshift(self)}
479
if(typeof self == "number"){
480
if(int.$factory[attr] === undefined){
481
throw _b_.AttributeError.$factory(
482
"'int' object has no attribute '" + attr + "'")
484
throw _b_.AttributeError.$factory(
485
"'int' object attribute '" + attr + "' is read-only")
501
if(other.__class__ === $B.long_int){
502
return new Number(self / parseInt(other.value))
503
}
504
return new Number(self / other)
518
if(_b_.hasattr(other, "__rtruediv__")){
519
return $B.$getattr(other, "__rtruediv__")(self)
525
s = _b_.bin(self)
526
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
531
int.numerator = function(self){return self}
532
int.denominator = function(self){return int.$factory(1)}
533
int.imag = function(self){return int.$factory(0)}
534
int.real = function(self){return self}
542
if(other.__class__ === $B.long_int){
543
return $B.long_int.__sub__($B.long_int.$factory(self),
544
$B.long_int.$factory(other))
547
if(self > $B.max_int32 || self < $B.min_int32 ||
548
other > $B.max_int32 || other < $B.min_int32){
549
return $B.long_int.__sub__($B.long_int.$factory(self),
550
$B.long_int.$factory(other))
554
if(_b_.isinstance(other, _b_.bool)){return self - other}
555
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
556
if(rsub !== _b_.None){return rsub(self)}
563
var opf = $op_func.replace(/-/gm, $op)
564
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
565
eval("int.__" + $ops[$op] + "__ = " + opf)
572
if(typeof other == "number"){
573
var res = self.valueOf() - other.valueOf()
574
if(res > $B.min_int && res < $B.max_int){return res}
575
else{return $B.long_int.__sub__($B.long_int.$factory(self),
576
$B.long_int.$factory(other))}
580
return $B.long_int.__sub__($B.long_int.$factory(self),
581
$B.long_int.$factory(other))
591
var bool_value = 0;
592
if(other.valueOf()){bool_value = 1}
593
return self - bool_value
598
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
599
if(rsub !== _b_.None){return rsub(self)}
605
var opf = $op_func.replace(/-/gm, $op)
606
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
607
eval("int.__" + $ops[$op] + "__ = " + opf)
632
eval("int.__"+$B.$comps[$op] + "__ = " +
633
$comp_func.replace(/>/gm, $op).
634
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
635
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
641
var $valid_digits = function(base) {
642
var digits = ""
643
if(base === 0){return "0"}
644
if(base < 10){
660
if(typeof value == "number" &&
661
(base === undefined || base == 10)){return parseInt(value)}
667
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
668
{"base": 10}, null, null),
669
value = $ns["x"],
670
base = $ns["base"]
686
if(base == 10){
687
if(value < $B.min_int || value > $B.max_int){
688
return $B.long_int.$factory(value)
689
}
695
var res = parseInt(value, base)
696
if(value < $B.min_int || value > $B.max_int){
697
return $B.long_int.$factory(value, base)
698
}
703
if(value === true){return Number(1)}
704
if(value === false){return Number(0)}
705
if(value.__class__ === $B.long_int){
712
function invalid(value, base){
713
throw _b_.ValueError.$factory("invalid literal for int() with base " +
714
base + ": '" + _b_.str.$factory(value) + "'")
715
}
718
if(typeof value == "string") {
719
var _value = value.trim() // remove leading/trailing whitespace
720
if(_value.length == 2 && base == 0 &&
721
(_value == "0b" || _value == "0o" || _value == "0x")){
722
throw _b_.ValueError.$factory("invalid value")
723
}
724
if(_value.length >2) {
725
var _pre = _value.substr(0, 2).toUpperCase()
726
if(base == 0){
727
if(_pre == "0B"){base = 2}
728
if(_pre == "0O"){base = 8}
729
if(_pre == "0X"){base = 16}
730
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
731
else if(_pre == "0O" && base != 8){invalid(_value, base)}
742
var _digits = $valid_digits(base),
743
_re = new RegExp("^[+-]?[" + _digits + "]" +
744
"[" + _digits + "_]*$", "i"),
745
match = _re.exec(_value)
763
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
764
if(num_value === null){
765
throw _b_.TypeError.$factory(
766
"int() argument must be a string, a bytes-like " +
767
"object or a number, not '" + $B.class_name(value) + "'")
768
}
769
return num_value
778
if(obj === null || obj === undefined ){ return false}
779
switch(typeof obj){
780
case "boolean":
781
return obj
782
case "number":
783
case "string":
784
if(obj){return true}
785
return false
786
default:
788
var klass = obj.__class__ || $B.get_class(obj),
789
missing = {},
790
bool_method = $B.$getattr(klass, "__bool__", missing)
791
if(bool_method === missing){
792
try{return _b_.len(obj) > 0}
795
var res = $B.$call(bool_method)(obj)
796
if(res !== true && res !== false){
797
throw _b_.TypeError.$factory("__bool__ should return " +
798
"bool, returned " + $B.class_name(res))
799
}
800
return res
817
var methods = $B.op2method.subset("operations", "binary", "comparisons",
818
"boolean")
819
for(var op in methods){
820
var method = "__" + methods[op] + "__"
821
bool[method] = (function(op){
822
return function(self, other){
823
var value = self ? 1 : 0
824
if(int[op] !== undefined){
825
return int[op](value, other)
826
}
827
}
828
})(method)
832
if(_b_.isinstance(other, bool)){
833
return self && other
834
}else if(_b_.isinstance(other, int)){
835
return int.__and__(bool.__index__(self), int.__index__(other))
836
}
837
return _b_.NotImplemented
848
if(_b_.isinstance(other, bool)){
849
return self || other
850
}else if(_b_.isinstance(other, int)){
851
return int.__or__(bool.__index__(self), int.__index__(other))
852
}
853
return _b_.NotImplemented
863
if(_b_.dir(self).indexOf(attr) > -1){
864
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
865
}else{
866
var msg = "'bool' object has no attribute '" + attr + "'"
867
}
868
throw _b_.AttributeError.$factory(msg)
872
if(_b_.isinstance(other, bool)){
873
return self ^ other ? true : false
874
}else if(_b_.isinstance(other, int)){
875
return int.__xor__(bool.__index__(self), int.__index__(other))
876
}
877
return _b_.NotImplemented
880
bool.$factory = function(){
881
// Calls $B.$bool, which is used inside the generated JS code and skips
882
// arguments control.