Permalink
Jul 28, 2018
Apr 3, 2020
Dec 18, 2019
Dec 17, 2020
Dec 17, 2020
Apr 23, 2018
Dec 17, 2020
Apr 23, 2018
Dec 17, 2020
Dec 17, 2020
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
Oct 14, 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
Mar 10, 2018
Mar 10, 2018
Feb 11, 2018
Feb 11, 2018
Oct 28, 2020
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
Mar 10, 2018
Nov 28, 2018
Jun 14, 2018
Jan 11, 2021
Mar 10, 2018
Oct 27, 2019
Oct 27, 2019
Feb 22, 2019
Feb 22, 2019
Oct 22, 2019
Jan 7, 2021
Oct 22, 2019
Oct 27, 2018
Oct 22, 2019
Feb 11, 2018
Newer
100644
947 lines (854 sloc)
28.4 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
42
var $ = $B.args("from_bytes", 3,
43
{bytes:null, byteorder:null, signed:null},
44
["bytes", "byteorder", "signed"],
45
arguments, {signed: false}, null, null)
46
47
var x = $.bytes,
48
byteorder = $.byteorder,
49
signed = $.signed,
50
_bytes, _len
51
if(_b_.isinstance(x, [_b_.bytes, _b_.bytearray])){
52
_bytes = x.source
53
_len = x.source.length
54
}else{
55
_bytes = _b_.list.$factory(x)
56
_len = _bytes.length
57
for(var i = 0; i < _len; i++){
58
_b_.bytes.$factory([_bytes[i]])
59
}
60
}
61
if(byteorder == "big"){
62
_bytes.reverse()
63
}else if(byteorder != "little"){
64
throw _b_.ValueError.$factory(
65
"byteorder must be either 'little' or 'big'")
66
}
67
var num = _bytes[0]
68
if(signed && num >= 128){
69
num = num - 256
70
}
71
var _mult = 256
72
for(var i = 1; i < _len; i++){
73
num = $B.add($B.mul(_mult, _bytes[i]), num)
74
_mult = $B.mul(_mult, 256)
75
}
76
if(! signed){
77
return num
78
}
79
if(_bytes[_len - 1] < 128){
80
return num
81
}
82
return $B.sub(num, _mult)
87
{self: null, len: null, byteorder: null, signed: null},
88
["self", "len", "byteorder", "*", "signed"],
89
arguments, {signed: false}, null, null),
94
if(! _b_.isinstance(len, _b_.int)){
95
throw _b_.TypeError.$factory("integer argument expected, got " +
99
throw _b_.ValueError.$factory(
100
"byteorder must be either 'little' or 'big'")
101
}
102
103
if(_b_.isinstance(self, $B.long_int)){
104
return $B.long_int.to_bytes(self, len, byteorder, signed)
119
var quotient = Math.floor(value / 256),
120
rest = value - 256 * quotient
121
res.push(rest)
122
if(res.length > len){
123
throw _b_.OverflowError.$factory("int too big to convert")
127
while(res.length < len){
128
res.push(0)
129
}
130
if(byteorder == "big"){
131
res.reverse()
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))
257
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
282
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
283
$B.long_int.$factory(other)))
285
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
286
if(rlshift !== _b_.None){return rlshift(self)}
293
if(other.__class__ === $B.long_int){
294
return $B.long_int.__mod__($B.long_int.$factory(self), other)
295
}
298
if(other === false){other = 0}
299
else if(other === true){other = 1}
300
if(other == 0){throw _b_.ZeroDivisionError.$factory(
304
if(_b_.hasattr(other, "__rmod__")){
305
return $B.$getattr(other, "__rmod__")(self)
306
}
323
var res = self * other
324
if(res > $B.min_int && res < $B.max_int){return res}
325
else{
326
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
327
$B.long_int.$factory(other)))
328
}
338
return $B.make_complex(int.__mul__(self, other.$real),
339
int.__mul__(self, other.$imag))
344
var $temp = other.slice(0, other.length)
345
for(var i = 0; i < val; i++){res = res.concat($temp)}
349
if(_b_.hasattr(other, "__rmul__")){
350
return $B.$getattr(other, "__rmul__")(self)
351
}
355
int.__ne__ = function(self, other){
356
var res = int.__eq__(self, other)
357
return (res === _b_.NotImplemented) ? res : !res
358
}
359
363
if(cls === undefined){
364
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
366
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
367
}
368
if(cls === int){return int.$factory(value)}
369
return {
370
__class__: cls,
378
function extended_euclidean(a, b){
379
var d, u, v
380
if(b == 0){
381
return [a, 1, 0]
382
}else{
383
[d, u, v] = extended_euclidean(b, a % b)
384
return [d, v, u - Math.floor(a / b) * v]
385
}
386
}
387
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
return {
442
__class__: $B.long_int,
443
value: ($B.BigInt(self) ** $B.BigInt(other)).toString(),
444
pos: true
445
}
446
}
447
return $B.long_int.__pow__($B.long_int.$from_int(self),
448
$B.long_int.$from_int(other))
452
other = _b_.float.numerator(other)
453
if(self >= 0){return new Number(Math.pow(self, other))}
463
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
464
if(rpow !== _b_.None){
465
return rpow(self)
466
}
470
function __newobj__(){
471
// __newobj__ is called with a generator as only argument
472
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
473
args = $.args
474
var res = args.slice(1)
475
res.__class__ = args[0]
476
return res
477
}
478
479
int.__reduce_ex__ = function(self){
480
return $B.fast_tuple([
481
__newobj__,
482
$B.fast_tuple([self.__class__ || int, int_value(self)]),
483
_b_.None,
484
_b_.None,
485
_b_.None])
486
}
487
496
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
497
$B.long_int.$factory(other)))
499
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
500
if(rrshift !== _b_.None){return rrshift(self)}
505
if(typeof self == "number"){
506
if(int.$factory[attr] === undefined){
507
throw _b_.AttributeError.$factory(
508
"'int' object has no attribute '" + attr + "'")
510
throw _b_.AttributeError.$factory(
511
"'int' object attribute '" + attr + "' is read-only")
527
if(other.__class__ === $B.long_int){
528
return new Number(self / parseInt(other.value))
529
}
530
return new Number(self / other)
545
if(_b_.hasattr(other, "__rtruediv__")){
546
return $B.$getattr(other, "__rtruediv__")(self)
552
s = _b_.bin(self)
553
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
558
int.numerator = function(self){return self}
559
int.denominator = function(self){return int.$factory(1)}
560
int.imag = function(self){return int.$factory(0)}
561
int.real = function(self){return self}
569
if(other.__class__ === $B.long_int){
570
return $B.long_int.__sub__($B.long_int.$factory(self),
571
$B.long_int.$factory(other))
574
if(self > $B.max_int32 || self < $B.min_int32 ||
575
other > $B.max_int32 || other < $B.min_int32){
576
return $B.long_int.__sub__($B.long_int.$factory(self),
577
$B.long_int.$factory(other))
594
var opf = $op_func.replace(/-/gm, $op)
595
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
596
eval("int.__" + $ops[$op] + "__ = " + opf)
603
if(typeof other == "number"){
604
var res = self.valueOf() - other.valueOf()
605
if(res > $B.min_int && res < $B.max_int){return res}
606
else{return $B.long_int.__sub__($B.long_int.$factory(self),
607
$B.long_int.$factory(other))}
611
return $B.long_int.__sub__($B.long_int.$factory(self),
612
$B.long_int.$factory(other))
619
if(other.$imag == 0){
620
// 1 - 0.0j is complex(1, 0.0) : the imaginary part is 0.0,
621
// *not* -0.0 (cf. https://bugs.python.org/issue22548)
622
return $B.make_complex(self - other.$real, 0)
623
}
627
var bool_value = 0;
628
if(other.valueOf()){bool_value = 1}
629
return self - bool_value
634
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
635
if(rsub !== _b_.None){return rsub(self)}
643
var opf = $op_func.replace(/-/gm, $op)
644
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
645
eval("int.__" + $ops[$op] + "__ = " + opf)
670
eval("int.__"+$B.$comps[$op] + "__ = " +
671
$comp_func.replace(/>/gm, $op).
672
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
673
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
679
var $valid_digits = function(base) {
680
var digits = ""
681
if(base === 0){return "0"}
682
if(base < 10){
698
if(typeof value == "number" &&
699
(base === undefined || base == 10)){return parseInt(value)}
705
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
706
{"base": 10}, null, null),
707
value = $ns["x"],
708
base = $ns["base"]
729
if(base == 10){
730
if(value < $B.min_int || value > $B.max_int){
731
return $B.long_int.$factory(value)
732
}
738
var res = parseInt(value, base)
739
if(value < $B.min_int || value > $B.max_int){
740
return $B.long_int.$factory(value, base)
741
}
746
if(value === true){return Number(1)}
747
if(value === false){return Number(0)}
748
if(value.__class__ === $B.long_int){
755
function invalid(value, base){
756
throw _b_.ValueError.$factory("invalid literal for int() with base " +
757
base + ": '" + _b_.str.$factory(value) + "'")
758
}
763
if(typeof value == "string") {
764
var _value = value.trim() // remove leading/trailing whitespace
765
if(_value.length == 2 && base == 0 &&
766
(_value == "0b" || _value == "0o" || _value == "0x")){
767
throw _b_.ValueError.$factory("invalid value")
768
}
770
var _pre = _value.substr(0, 2).toUpperCase()
771
if(base == 0){
772
if(_pre == "0B"){base = 2}
773
if(_pre == "0O"){base = 8}
774
if(_pre == "0X"){base = 16}
775
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
776
else if(_pre == "0O" && base != 8){invalid(_value, base)}
787
var _digits = $valid_digits(base),
788
_re = new RegExp("^[+-]?[" + _digits + "]" +
789
"[" + _digits + "_]*$", "i"),
790
match = _re.exec(_value)
817
throw _b_.TypeError.$factory(
818
"int() argument must be a string, a bytes-like " +
819
"object or a number, not '" + $B.class_name(value) + "'")
828
if(obj === null || obj === undefined ){ return false}
829
switch(typeof obj){
830
case "boolean":
831
return obj
832
case "number":
833
case "string":
834
if(obj){return true}
835
return false
836
default:
838
var klass = obj.__class__ || $B.get_class(obj),
839
missing = {},
840
bool_method = $B.$getattr(klass, "__bool__", missing)
841
if(bool_method === missing){
842
try{return _b_.len(obj) > 0}
845
var res = $B.$call(bool_method)(obj)
846
if(res !== true && res !== false){
847
throw _b_.TypeError.$factory("__bool__ should return " +
848
"bool, returned " + $B.class_name(res))
849
}
850
return res
867
var methods = $B.op2method.subset("operations", "binary", "comparisons",
868
"boolean")
870
for(var op in methods){
871
var method = "__" + methods[op] + "__"
872
bool[method] = (function(op){
873
return function(self, other){
874
var value = self ? 1 : 0
875
if(int[op] !== undefined){
876
return int[op](value, other)
877
}
878
}
879
})(method)
883
if(_b_.isinstance(other, bool)){
884
return self && other
885
}else if(_b_.isinstance(other, int)){
886
return int.__and__(bool.__index__(self), int.__index__(other))
887
}
888
return _b_.NotImplemented
891
bool.__float__ = function(self){
892
return self ? new Number(1) : new Number(0)
893
}
894
903
if(_b_.isinstance(other, bool)){
904
return self || other
905
}else if(_b_.isinstance(other, int)){
906
return int.__or__(bool.__index__(self), int.__index__(other))
907
}
908
return _b_.NotImplemented
918
if(_b_.dir(self).indexOf(attr) > -1){
919
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
920
}else{
921
var msg = "'bool' object has no attribute '" + attr + "'"
922
}
923
throw _b_.AttributeError.$factory(msg)
927
if(_b_.isinstance(other, bool)){
928
return self ^ other ? true : false
929
}else if(_b_.isinstance(other, int)){
930
return int.__xor__(bool.__index__(self), int.__index__(other))
931
}
932
return _b_.NotImplemented
935
bool.$factory = function(){
936
// Calls $B.$bool, which is used inside the generated JS code and skips
937
// arguments control.