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
Jun 30, 2020
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
904 lines (817 sloc)
27.5 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
390
other = int_value(other)
391
switch(other.valueOf()) {
392
case 0:
393
return int.$factory(1)
394
case 1:
395
return int.$factory(self.valueOf())
398
// If z is provided, the algorithm is faster than computing
399
// self ** other then applying the modulo z
400
if(z == 1){return 0}
401
var result = 1,
402
base = self % z,
403
exponent = other,
404
long_int = $B.long_int
405
if(exponent < 0){
406
var gcd, inv, _
407
[gcd, inv, _] = extended_euclidean(self, z)
408
if(gcd !== 1){
409
throw _b_.ValueError.$factory("not relative primes: " +
410
self + ' and ' + z)
411
}
412
return int.__pow__(inv, -exponent, z)
413
}
414
while(exponent > 0){
415
if(exponent % 2 == 1){
416
if(result * base > $B.max_int){
417
result = long_int.__mul__(
418
long_int.$factory(result),
419
long_int.$factory(base))
420
result = long_int.__mod__(result, z)
421
}else{
422
result = (result * base) % z
423
}
424
}
425
exponent = exponent >> 1
426
if(base * base > $B.max_int){
427
base = long_int.__mul__(long_int.$factory(base),
428
long_int.$factory(base))
429
base = long_int.__mod__(base, z)
430
}else{
431
base = (base * base) % z
432
}
436
var res = Math.pow(self.valueOf(), other.valueOf())
437
if(res > $B.min_int && res < $B.max_int){return res}
440
if($B.BigInt){
441
$B.use_bigint++
442
return {
443
__class__: $B.long_int,
444
value: ($B.BigInt(self) ** $B.BigInt(other)).toString(),
445
pos: true
446
}
447
}
448
return $B.long_int.__pow__($B.long_int.$from_int(self),
449
$B.long_int.$from_int(other))
463
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
464
if(rpow !== _b_.None){
465
return rpow(self)
466
}
479
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
480
$B.long_int.$factory(other)))
482
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
483
if(rrshift !== _b_.None){return rrshift(self)}
488
if(typeof self == "number"){
489
if(int.$factory[attr] === undefined){
490
throw _b_.AttributeError.$factory(
491
"'int' object has no attribute '" + attr + "'")
493
throw _b_.AttributeError.$factory(
494
"'int' object attribute '" + attr + "' is read-only")
510
if(other.__class__ === $B.long_int){
511
return new Number(self / parseInt(other.value))
512
}
513
return new Number(self / other)
527
if(_b_.hasattr(other, "__rtruediv__")){
528
return $B.$getattr(other, "__rtruediv__")(self)
534
s = _b_.bin(self)
535
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
540
int.numerator = function(self){return self}
541
int.denominator = function(self){return int.$factory(1)}
542
int.imag = function(self){return int.$factory(0)}
543
int.real = function(self){return self}
551
if(other.__class__ === $B.long_int){
552
return $B.long_int.__sub__($B.long_int.$factory(self),
553
$B.long_int.$factory(other))
556
if(self > $B.max_int32 || self < $B.min_int32 ||
557
other > $B.max_int32 || other < $B.min_int32){
558
return $B.long_int.__sub__($B.long_int.$factory(self),
559
$B.long_int.$factory(other))
563
if(_b_.isinstance(other, _b_.bool)){return self - other}
564
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
565
if(rsub !== _b_.None){return rsub(self)}
572
var opf = $op_func.replace(/-/gm, $op)
573
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
574
eval("int.__" + $ops[$op] + "__ = " + opf)
581
if(typeof other == "number"){
582
var res = self.valueOf() - other.valueOf()
583
if(res > $B.min_int && res < $B.max_int){return res}
584
else{return $B.long_int.__sub__($B.long_int.$factory(self),
585
$B.long_int.$factory(other))}
589
return $B.long_int.__sub__($B.long_int.$factory(self),
590
$B.long_int.$factory(other))
600
var bool_value = 0;
601
if(other.valueOf()){bool_value = 1}
602
return self - bool_value
607
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
608
if(rsub !== _b_.None){return rsub(self)}
616
var opf = $op_func.replace(/-/gm, $op)
617
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
618
eval("int.__" + $ops[$op] + "__ = " + opf)
643
eval("int.__"+$B.$comps[$op] + "__ = " +
644
$comp_func.replace(/>/gm, $op).
645
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
646
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
652
var $valid_digits = function(base) {
653
var digits = ""
654
if(base === 0){return "0"}
655
if(base < 10){
671
if(typeof value == "number" &&
672
(base === undefined || base == 10)){return parseInt(value)}
678
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
679
{"base": 10}, null, null),
680
value = $ns["x"],
681
base = $ns["base"]
698
if(base == 10){
699
if(value < $B.min_int || value > $B.max_int){
700
return $B.long_int.$factory(value)
701
}
707
var res = parseInt(value, base)
708
if(value < $B.min_int || value > $B.max_int){
709
return $B.long_int.$factory(value, base)
710
}
715
if(value === true){return Number(1)}
716
if(value === false){return Number(0)}
717
if(value.__class__ === $B.long_int){
724
function invalid(value, base){
725
throw _b_.ValueError.$factory("invalid literal for int() with base " +
726
base + ": '" + _b_.str.$factory(value) + "'")
727
}
730
if(typeof value == "string") {
731
var _value = value.trim() // remove leading/trailing whitespace
732
if(_value.length == 2 && base == 0 &&
733
(_value == "0b" || _value == "0o" || _value == "0x")){
734
throw _b_.ValueError.$factory("invalid value")
735
}
736
if(_value.length >2) {
737
var _pre = _value.substr(0, 2).toUpperCase()
738
if(base == 0){
739
if(_pre == "0B"){base = 2}
740
if(_pre == "0O"){base = 8}
741
if(_pre == "0X"){base = 16}
742
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
743
else if(_pre == "0O" && base != 8){invalid(_value, base)}
754
var _digits = $valid_digits(base),
755
_re = new RegExp("^[+-]?[" + _digits + "]" +
756
"[" + _digits + "_]*$", "i"),
757
match = _re.exec(_value)
775
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
776
if(num_value === null){
777
throw _b_.TypeError.$factory(
778
"int() argument must be a string, a bytes-like " +
779
"object or a number, not '" + $B.class_name(value) + "'")
780
}
781
return num_value
790
if(obj === null || obj === undefined ){ return false}
791
switch(typeof obj){
792
case "boolean":
793
return obj
794
case "number":
795
case "string":
796
if(obj){return true}
797
return false
798
default:
800
var klass = obj.__class__ || $B.get_class(obj),
801
missing = {},
802
bool_method = $B.$getattr(klass, "__bool__", missing)
803
if(bool_method === missing){
804
try{return _b_.len(obj) > 0}
807
var res = $B.$call(bool_method)(obj)
808
if(res !== true && res !== false){
809
throw _b_.TypeError.$factory("__bool__ should return " +
810
"bool, returned " + $B.class_name(res))
811
}
812
return res
829
var methods = $B.op2method.subset("operations", "binary", "comparisons",
830
"boolean")
831
for(var op in methods){
832
var method = "__" + methods[op] + "__"
833
bool[method] = (function(op){
834
return function(self, other){
835
var value = self ? 1 : 0
836
if(int[op] !== undefined){
837
return int[op](value, other)
838
}
839
}
840
})(method)
844
if(_b_.isinstance(other, bool)){
845
return self && other
846
}else if(_b_.isinstance(other, int)){
847
return int.__and__(bool.__index__(self), int.__index__(other))
848
}
849
return _b_.NotImplemented
860
if(_b_.isinstance(other, bool)){
861
return self || other
862
}else if(_b_.isinstance(other, int)){
863
return int.__or__(bool.__index__(self), int.__index__(other))
864
}
865
return _b_.NotImplemented
875
if(_b_.dir(self).indexOf(attr) > -1){
876
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
877
}else{
878
var msg = "'bool' object has no attribute '" + attr + "'"
879
}
880
throw _b_.AttributeError.$factory(msg)
884
if(_b_.isinstance(other, bool)){
885
return self ^ other ? true : false
886
}else if(_b_.isinstance(other, int)){
887
return int.__xor__(bool.__index__(self), int.__index__(other))
888
}
889
return _b_.NotImplemented
892
bool.$factory = function(){
893
// Calls $B.$bool, which is used inside the generated JS code and skips
894
// arguments control.