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
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
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
950 lines (857 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
}
437
if(res > $B.min_int && res < $B.max_int){
438
return other > 0 ? res : new Number(res)
439
}else if(res !== Infinity && !isFinite(res)){
440
return res
441
}else{
442
if($B.BigInt){
443
return {
444
__class__: $B.long_int,
445
value: ($B.BigInt(self) ** $B.BigInt(other)).toString(),
446
pos: true
447
}
448
}
449
return $B.long_int.__pow__($B.long_int.$from_int(self),
450
$B.long_int.$from_int(other))
466
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
467
if(rpow !== _b_.None){
468
return rpow(self)
469
}
473
function __newobj__(){
474
// __newobj__ is called with a generator as only argument
475
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
476
args = $.args
477
var res = args.slice(1)
478
res.__class__ = args[0]
479
return res
480
}
481
482
int.__reduce_ex__ = function(self){
483
return $B.fast_tuple([
484
__newobj__,
485
$B.fast_tuple([self.__class__ || int, int_value(self)]),
486
_b_.None,
487
_b_.None,
488
_b_.None])
489
}
490
499
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
500
$B.long_int.$factory(other)))
502
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
503
if(rrshift !== _b_.None){return rrshift(self)}
508
if(typeof self == "number"){
509
if(int.$factory[attr] === undefined){
510
throw _b_.AttributeError.$factory(
511
"'int' object has no attribute '" + attr + "'")
513
throw _b_.AttributeError.$factory(
514
"'int' object attribute '" + attr + "' is read-only")
530
if(other.__class__ === $B.long_int){
531
return new Number(self / parseInt(other.value))
532
}
533
return new Number(self / other)
548
if(_b_.hasattr(other, "__rtruediv__")){
549
return $B.$getattr(other, "__rtruediv__")(self)
555
s = _b_.bin(self)
556
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
561
int.numerator = function(self){return self}
562
int.denominator = function(self){return int.$factory(1)}
563
int.imag = function(self){return int.$factory(0)}
564
int.real = function(self){return self}
572
if(other.__class__ === $B.long_int){
573
return $B.long_int.__sub__($B.long_int.$factory(self),
574
$B.long_int.$factory(other))
577
if(self > $B.max_int32 || self < $B.min_int32 ||
578
other > $B.max_int32 || other < $B.min_int32){
579
return $B.long_int.__sub__($B.long_int.$factory(self),
580
$B.long_int.$factory(other))
597
var opf = $op_func.replace(/-/gm, $op)
598
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
599
eval("int.__" + $ops[$op] + "__ = " + opf)
606
if(typeof other == "number"){
607
var res = self.valueOf() - other.valueOf()
608
if(res > $B.min_int && res < $B.max_int){return res}
609
else{return $B.long_int.__sub__($B.long_int.$factory(self),
610
$B.long_int.$factory(other))}
614
return $B.long_int.__sub__($B.long_int.$factory(self),
615
$B.long_int.$factory(other))
622
if(other.$imag == 0){
623
// 1 - 0.0j is complex(1, 0.0) : the imaginary part is 0.0,
624
// *not* -0.0 (cf. https://bugs.python.org/issue22548)
625
return $B.make_complex(self - other.$real, 0)
626
}
630
var bool_value = 0;
631
if(other.valueOf()){bool_value = 1}
632
return self - bool_value
637
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
638
if(rsub !== _b_.None){return rsub(self)}
646
var opf = $op_func.replace(/-/gm, $op)
647
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
648
eval("int.__" + $ops[$op] + "__ = " + opf)
673
eval("int.__"+$B.$comps[$op] + "__ = " +
674
$comp_func.replace(/>/gm, $op).
675
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
676
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
682
var $valid_digits = function(base) {
683
var digits = ""
684
if(base === 0){return "0"}
685
if(base < 10){
701
if(typeof value == "number" &&
702
(base === undefined || base == 10)){return parseInt(value)}
708
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
709
{"base": 10}, null, null),
710
value = $ns["x"],
711
base = $ns["base"]
732
if(base == 10){
733
if(value < $B.min_int || value > $B.max_int){
734
return $B.long_int.$factory(value)
735
}
741
var res = parseInt(value, base)
742
if(value < $B.min_int || value > $B.max_int){
743
return $B.long_int.$factory(value, base)
744
}
749
if(value === true){return Number(1)}
750
if(value === false){return Number(0)}
751
if(value.__class__ === $B.long_int){
758
function invalid(value, base){
759
throw _b_.ValueError.$factory("invalid literal for int() with base " +
760
base + ": '" + _b_.str.$factory(value) + "'")
761
}
766
if(typeof value == "string") {
767
var _value = value.trim() // remove leading/trailing whitespace
768
if(_value.length == 2 && base == 0 &&
769
(_value == "0b" || _value == "0o" || _value == "0x")){
770
throw _b_.ValueError.$factory("invalid value")
771
}
773
var _pre = _value.substr(0, 2).toUpperCase()
774
if(base == 0){
775
if(_pre == "0B"){base = 2}
776
if(_pre == "0O"){base = 8}
777
if(_pre == "0X"){base = 16}
778
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
779
else if(_pre == "0O" && base != 8){invalid(_value, base)}
790
var _digits = $valid_digits(base),
791
_re = new RegExp("^[+-]?[" + _digits + "]" +
792
"[" + _digits + "_]*$", "i"),
793
match = _re.exec(_value)
820
throw _b_.TypeError.$factory(
821
"int() argument must be a string, a bytes-like " +
822
"object or a number, not '" + $B.class_name(value) + "'")
831
if(obj === null || obj === undefined ){ return false}
832
switch(typeof obj){
833
case "boolean":
834
return obj
835
case "number":
836
case "string":
837
if(obj){return true}
838
return false
839
default:
841
var klass = obj.__class__ || $B.get_class(obj),
842
missing = {},
843
bool_method = $B.$getattr(klass, "__bool__", missing)
844
if(bool_method === missing){
845
try{return _b_.len(obj) > 0}
848
var res = $B.$call(bool_method)(obj)
849
if(res !== true && res !== false){
850
throw _b_.TypeError.$factory("__bool__ should return " +
851
"bool, returned " + $B.class_name(res))
852
}
853
return res
870
var methods = $B.op2method.subset("operations", "binary", "comparisons",
871
"boolean")
873
for(var op in methods){
874
var method = "__" + methods[op] + "__"
875
bool[method] = (function(op){
876
return function(self, other){
877
var value = self ? 1 : 0
878
if(int[op] !== undefined){
879
return int[op](value, other)
880
}
881
}
882
})(method)
886
if(_b_.isinstance(other, bool)){
887
return self && other
888
}else if(_b_.isinstance(other, int)){
889
return int.__and__(bool.__index__(self), int.__index__(other))
890
}
891
return _b_.NotImplemented
894
bool.__float__ = function(self){
895
return self ? new Number(1) : new Number(0)
896
}
897
906
if(_b_.isinstance(other, bool)){
907
return self || other
908
}else if(_b_.isinstance(other, int)){
909
return int.__or__(bool.__index__(self), int.__index__(other))
910
}
911
return _b_.NotImplemented
921
if(_b_.dir(self).indexOf(attr) > -1){
922
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
923
}else{
924
var msg = "'bool' object has no attribute '" + attr + "'"
925
}
926
throw _b_.AttributeError.$factory(msg)
930
if(_b_.isinstance(other, bool)){
931
return self ^ other ? true : false
932
}else if(_b_.isinstance(other, int)){
933
return int.__xor__(bool.__index__(self), int.__index__(other))
934
}
935
return _b_.NotImplemented
938
bool.$factory = function(){
939
// Calls $B.$bool, which is used inside the generated JS code and skips
940
// arguments control.