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
895 lines (808 sloc)
27.2 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)}
607
var opf = $op_func.replace(/-/gm, $op)
608
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
609
eval("int.__" + $ops[$op] + "__ = " + opf)
634
eval("int.__"+$B.$comps[$op] + "__ = " +
635
$comp_func.replace(/>/gm, $op).
636
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
637
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
643
var $valid_digits = function(base) {
644
var digits = ""
645
if(base === 0){return "0"}
646
if(base < 10){
662
if(typeof value == "number" &&
663
(base === undefined || base == 10)){return parseInt(value)}
669
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
670
{"base": 10}, null, null),
671
value = $ns["x"],
672
base = $ns["base"]
689
if(base == 10){
690
if(value < $B.min_int || value > $B.max_int){
691
return $B.long_int.$factory(value)
692
}
698
var res = parseInt(value, base)
699
if(value < $B.min_int || value > $B.max_int){
700
return $B.long_int.$factory(value, base)
701
}
706
if(value === true){return Number(1)}
707
if(value === false){return Number(0)}
708
if(value.__class__ === $B.long_int){
715
function invalid(value, base){
716
throw _b_.ValueError.$factory("invalid literal for int() with base " +
717
base + ": '" + _b_.str.$factory(value) + "'")
718
}
721
if(typeof value == "string") {
722
var _value = value.trim() // remove leading/trailing whitespace
723
if(_value.length == 2 && base == 0 &&
724
(_value == "0b" || _value == "0o" || _value == "0x")){
725
throw _b_.ValueError.$factory("invalid value")
726
}
727
if(_value.length >2) {
728
var _pre = _value.substr(0, 2).toUpperCase()
729
if(base == 0){
730
if(_pre == "0B"){base = 2}
731
if(_pre == "0O"){base = 8}
732
if(_pre == "0X"){base = 16}
733
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
734
else if(_pre == "0O" && base != 8){invalid(_value, base)}
745
var _digits = $valid_digits(base),
746
_re = new RegExp("^[+-]?[" + _digits + "]" +
747
"[" + _digits + "_]*$", "i"),
748
match = _re.exec(_value)
766
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
767
if(num_value === null){
768
throw _b_.TypeError.$factory(
769
"int() argument must be a string, a bytes-like " +
770
"object or a number, not '" + $B.class_name(value) + "'")
771
}
772
return num_value
781
if(obj === null || obj === undefined ){ return false}
782
switch(typeof obj){
783
case "boolean":
784
return obj
785
case "number":
786
case "string":
787
if(obj){return true}
788
return false
789
default:
791
var klass = obj.__class__ || $B.get_class(obj),
792
missing = {},
793
bool_method = $B.$getattr(klass, "__bool__", missing)
794
if(bool_method === missing){
795
try{return _b_.len(obj) > 0}
798
var res = $B.$call(bool_method)(obj)
799
if(res !== true && res !== false){
800
throw _b_.TypeError.$factory("__bool__ should return " +
801
"bool, returned " + $B.class_name(res))
802
}
803
return res
820
var methods = $B.op2method.subset("operations", "binary", "comparisons",
821
"boolean")
822
for(var op in methods){
823
var method = "__" + methods[op] + "__"
824
bool[method] = (function(op){
825
return function(self, other){
826
var value = self ? 1 : 0
827
if(int[op] !== undefined){
828
return int[op](value, other)
829
}
830
}
831
})(method)
835
if(_b_.isinstance(other, bool)){
836
return self && other
837
}else if(_b_.isinstance(other, int)){
838
return int.__and__(bool.__index__(self), int.__index__(other))
839
}
840
return _b_.NotImplemented
851
if(_b_.isinstance(other, bool)){
852
return self || other
853
}else if(_b_.isinstance(other, int)){
854
return int.__or__(bool.__index__(self), int.__index__(other))
855
}
856
return _b_.NotImplemented
866
if(_b_.dir(self).indexOf(attr) > -1){
867
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
868
}else{
869
var msg = "'bool' object has no attribute '" + attr + "'"
870
}
871
throw _b_.AttributeError.$factory(msg)
875
if(_b_.isinstance(other, bool)){
876
return self ^ other ? true : false
877
}else if(_b_.isinstance(other, int)){
878
return int.__xor__(bool.__index__(self), int.__index__(other))
879
}
880
return _b_.NotImplemented
883
bool.$factory = function(){
884
// Calls $B.$bool, which is used inside the generated JS code and skips
885
// arguments control.