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
Feb 22, 2019
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
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
918 lines (829 sloc)
27.7 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
}
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)
528
if(_b_.hasattr(other, "__rtruediv__")){
529
return $B.$getattr(other, "__rtruediv__")(self)
535
s = _b_.bin(self)
536
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
541
int.numerator = function(self){return self}
542
int.denominator = function(self){return int.$factory(1)}
543
int.imag = function(self){return int.$factory(0)}
544
int.real = function(self){return self}
552
if(other.__class__ === $B.long_int){
553
return $B.long_int.__sub__($B.long_int.$factory(self),
554
$B.long_int.$factory(other))
557
if(self > $B.max_int32 || self < $B.min_int32 ||
558
other > $B.max_int32 || other < $B.min_int32){
559
return $B.long_int.__sub__($B.long_int.$factory(self),
560
$B.long_int.$factory(other))
564
if(_b_.isinstance(other, _b_.bool)){return self - other}
565
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
566
if(rsub !== _b_.None){return rsub(self)}
573
var opf = $op_func.replace(/-/gm, $op)
574
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
575
eval("int.__" + $ops[$op] + "__ = " + opf)
582
if(typeof other == "number"){
583
var res = self.valueOf() - other.valueOf()
584
if(res > $B.min_int && res < $B.max_int){return res}
585
else{return $B.long_int.__sub__($B.long_int.$factory(self),
586
$B.long_int.$factory(other))}
590
return $B.long_int.__sub__($B.long_int.$factory(self),
591
$B.long_int.$factory(other))
598
if(other.$imag == 0){
599
// 1 - 0.0j is complex(1, 0.0) : the imaginary part is 0.0,
600
// *not* -0.0 (cf. https://bugs.python.org/issue22548)
601
return $B.make_complex(self - other.$real, 0)
602
}
606
var bool_value = 0;
607
if(other.valueOf()){bool_value = 1}
608
return self - bool_value
613
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
614
if(rsub !== _b_.None){return rsub(self)}
622
var opf = $op_func.replace(/-/gm, $op)
623
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
624
eval("int.__" + $ops[$op] + "__ = " + opf)
649
eval("int.__"+$B.$comps[$op] + "__ = " +
650
$comp_func.replace(/>/gm, $op).
651
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
652
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
658
var $valid_digits = function(base) {
659
var digits = ""
660
if(base === 0){return "0"}
661
if(base < 10){
677
if(typeof value == "number" &&
678
(base === undefined || base == 10)){return parseInt(value)}
684
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
685
{"base": 10}, null, null),
686
value = $ns["x"],
687
base = $ns["base"]
708
if(base == 10){
709
if(value < $B.min_int || value > $B.max_int){
710
return $B.long_int.$factory(value)
711
}
717
var res = parseInt(value, base)
718
if(value < $B.min_int || value > $B.max_int){
719
return $B.long_int.$factory(value, base)
720
}
725
if(value === true){return Number(1)}
726
if(value === false){return Number(0)}
727
if(value.__class__ === $B.long_int){
734
function invalid(value, base){
735
throw _b_.ValueError.$factory("invalid literal for int() with base " +
736
base + ": '" + _b_.str.$factory(value) + "'")
737
}
742
if(typeof value == "string") {
743
var _value = value.trim() // remove leading/trailing whitespace
744
if(_value.length == 2 && base == 0 &&
745
(_value == "0b" || _value == "0o" || _value == "0x")){
746
throw _b_.ValueError.$factory("invalid value")
747
}
749
var _pre = _value.substr(0, 2).toUpperCase()
750
if(base == 0){
751
if(_pre == "0B"){base = 2}
752
if(_pre == "0O"){base = 8}
753
if(_pre == "0X"){base = 16}
754
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
755
else if(_pre == "0O" && base != 8){invalid(_value, base)}
766
var _digits = $valid_digits(base),
767
_re = new RegExp("^[+-]?[" + _digits + "]" +
768
"[" + _digits + "_]*$", "i"),
769
match = _re.exec(_value)
789
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
790
if(num_value === null){
791
throw _b_.TypeError.$factory(
792
"int() argument must be a string, a bytes-like " +
793
"object or a number, not '" + $B.class_name(value) + "'")
794
}
795
return num_value
804
if(obj === null || obj === undefined ){ return false}
805
switch(typeof obj){
806
case "boolean":
807
return obj
808
case "number":
809
case "string":
810
if(obj){return true}
811
return false
812
default:
814
var klass = obj.__class__ || $B.get_class(obj),
815
missing = {},
816
bool_method = $B.$getattr(klass, "__bool__", missing)
817
if(bool_method === missing){
818
try{return _b_.len(obj) > 0}
821
var res = $B.$call(bool_method)(obj)
822
if(res !== true && res !== false){
823
throw _b_.TypeError.$factory("__bool__ should return " +
824
"bool, returned " + $B.class_name(res))
825
}
826
return res
843
var methods = $B.op2method.subset("operations", "binary", "comparisons",
844
"boolean")
845
for(var op in methods){
846
var method = "__" + methods[op] + "__"
847
bool[method] = (function(op){
848
return function(self, other){
849
var value = self ? 1 : 0
850
if(int[op] !== undefined){
851
return int[op](value, other)
852
}
853
}
854
})(method)
858
if(_b_.isinstance(other, bool)){
859
return self && other
860
}else if(_b_.isinstance(other, int)){
861
return int.__and__(bool.__index__(self), int.__index__(other))
862
}
863
return _b_.NotImplemented
874
if(_b_.isinstance(other, bool)){
875
return self || other
876
}else if(_b_.isinstance(other, int)){
877
return int.__or__(bool.__index__(self), int.__index__(other))
878
}
879
return _b_.NotImplemented
889
if(_b_.dir(self).indexOf(attr) > -1){
890
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
891
}else{
892
var msg = "'bool' object has no attribute '" + attr + "'"
893
}
894
throw _b_.AttributeError.$factory(msg)
898
if(_b_.isinstance(other, bool)){
899
return self ^ other ? true : false
900
}else if(_b_.isinstance(other, int)){
901
return int.__xor__(bool.__index__(self), int.__index__(other))
902
}
903
return _b_.NotImplemented
906
bool.$factory = function(){
907
// Calls $B.$bool, which is used inside the generated JS code and skips
908
// arguments control.