Skip to content

Commit 6a920d2

Browse files
committed
builtin: Implement builtin_bin
1 parent fff175c commit 6a920d2

File tree

4 files changed

+76
-27
lines changed

4 files changed

+76
-27
lines changed

‎builtin/builtin.go‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package builtin
77

88
import (
9+
"fmt"
10+
"math/big"
911
"unicode/utf8"
1012

1113
"github.com/go-python/gpython/compile"
@@ -25,7 +27,7 @@ func init(){
2527
py.MustNewMethod("all", builtin_all, 0, all_doc),
2628
py.MustNewMethod("any", builtin_any, 0, any_doc),
2729
py.MustNewMethod("ascii", builtin_ascii, 0, ascii_doc),
28-
// py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
30+
py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
2931
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
3032
py.MustNewMethod("chr", builtin_chr, 0, chr_doc),
3133
py.MustNewMethod("compile", builtin_compile, 0, compile_doc),
@@ -309,7 +311,12 @@ func builtin_any(self, seq py.Object) (py.Object, error){
309311
returnpy.False, nil
310312
}
311313

312-
constascii_doc=`
314+
constascii_doc=`Return an ASCII-only representation of an object.
315+
316+
As repr(), return a string containing a printable representation of an
317+
object, but escape the non-ASCII characters in the string returned by
318+
repr() using \\x, \\u or \\U escapes. This generates a string similar
319+
to that returned by repr() in Python 2.
313320
`
314321

315322
funcbuiltin_ascii(self, o py.Object) (py.Object, error){
@@ -322,6 +329,29 @@ func builtin_ascii(self, o py.Object) (py.Object, error){
322329
returnpy.String(out), err
323330
}
324331

332+
constbin_doc=`Return the binary representation of an integer.
333+
334+
>>> bin(2796202)
335+
'0b1010101010101010101010'
336+
`
337+
338+
funcbuiltin_bin(self, o py.Object) (py.Object, error){
339+
bigint, ok:=py.ConvertToBigInt(o)
340+
if!ok{
341+
returnnil, py.ExceptionNewf(py.TypeError, "'%s' object cannot be interpreted as an integer", o.Type().Name)
342+
}
343+
344+
value:= (*big.Int)(bigint)
345+
varoutstring
346+
ifvalue.Sign() <0{
347+
value=new(big.Int).Abs(value)
348+
out=fmt.Sprintf("-0b%b", value)
349+
} else{
350+
out=fmt.Sprintf("0b%b", value)
351+
}
352+
returnpy.String(out), nil
353+
}
354+
325355
constround_doc=`round(number[, ndigits]) -> number
326356
327357
Round a number to a given precision in decimal digits (default 0 digits).

‎builtin/tests/builtin.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
assertascii(chr(0x10001)) =="'\\U00010001'"
2626
assertascii('안녕 gpython') =="'\\uc548\\ub155 gpython'"
2727

28+
doc="bin"
29+
assertbin(False) =='0b0'
30+
assertbin(True) =='0b1'
31+
assertbin(0) =='0b0'
32+
assertbin(1) =='0b1'
33+
assertbin(-1) =='-0b1'
34+
assertbin(10) =='0b1010'
35+
assertbin(-10) =='-0b1010'
36+
assertbin(2**32) =='0b100000000000000000000000000000000'
37+
assertbin(2**32-1) =='0b11111111111111111111111111111111'
38+
assertbin(-(2**32)) =='-0b100000000000000000000000000000000'
39+
assertbin(-(2**32-1)) =='-0b11111111111111111111111111111111'
40+
41+
a=2**65
42+
assertbin(a) =='0b1'+'0'*65
43+
assertbin(a-1) =='0b'+'1'*65
44+
assertbin(-(a)) =='-0b1'+'0'*65
45+
assertbin(-(a-1)) =='-0b'+'1'*65
46+
2847
doc="chr"
2948
assertchr(65) =="A"
3049
assertchr(163) =="£"

‎py/bigint.go‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func BigIntCheck(obj Object) (*BigInt, error){
6666
// Convert an Object to an BigInt
6767
//
6868
// Retrurns ok as to whether the conversion worked or not
69-
funcconvertToBigInt(otherObject) (*BigInt, bool){
69+
funcConvertToBigInt(otherObject) (*BigInt, bool){
7070
switchb:=other.(type){
7171
caseInt:
7272
return (*BigInt)(big.NewInt(int64(b))), true
@@ -173,7 +173,7 @@ func (a *BigInt) M__invert__() (Object, error){
173173
}
174174

175175
func (a*BigInt) M__add__(otherObject) (Object, error){
176-
ifb, ok:=convertToBigInt(other); ok{
176+
ifb, ok:=ConvertToBigInt(other); ok{
177177
return (*BigInt)(new(big.Int).Add((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
178178
}
179179
returnNotImplemented, nil
@@ -188,14 +188,14 @@ func (a *BigInt) M__iadd__(other Object) (Object, error){
188188
}
189189

190190
func (a*BigInt) M__sub__(otherObject) (Object, error){
191-
ifb, ok:=convertToBigInt(other); ok{
191+
ifb, ok:=ConvertToBigInt(other); ok{
192192
return (*BigInt)(new(big.Int).Sub((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
193193
}
194194
returnNotImplemented, nil
195195
}
196196

197197
func (a*BigInt) M__rsub__(otherObject) (Object, error){
198-
ifb, ok:=convertToBigInt(other); ok{
198+
ifb, ok:=ConvertToBigInt(other); ok{
199199
return (*BigInt)(new(big.Int).Sub((*big.Int)(b), (*big.Int)(a))).MaybeInt(), nil
200200
}
201201
returnNotImplemented, nil
@@ -206,7 +206,7 @@ func (a *BigInt) M__isub__(other Object) (Object, error){
206206
}
207207

208208
func (a*BigInt) M__mul__(otherObject) (Object, error){
209-
ifb, ok:=convertToBigInt(other); ok{
209+
ifb, ok:=ConvertToBigInt(other); ok{
210210
return (*BigInt)(new(big.Int).Mul((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
211211
}
212212
returnNotImplemented, nil
@@ -306,14 +306,14 @@ func (a *BigInt) divMod(b *BigInt) (Object, Object, error){
306306
}
307307

308308
func (a*BigInt) M__divmod__(otherObject) (Object, Object, error){
309-
ifb, ok:=convertToBigInt(other); ok{
309+
ifb, ok:=ConvertToBigInt(other); ok{
310310
returna.divMod(b)
311311
}
312312
returnNotImplemented, NotImplemented, nil
313313
}
314314

315315
func (a*BigInt) M__rdivmod__(otherObject) (Object, Object, error){
316-
ifb, ok:=convertToBigInt(other); ok{
316+
ifb, ok:=ConvertToBigInt(other); ok{
317317
returnb.divMod(a)
318318
}
319319
returnNotImplemented, NotImplemented, nil
@@ -343,18 +343,18 @@ func (a *BigInt) M__pow__(other, modulus Object) (Object, error){
343343
varm*BigInt
344344
ifmodulus!=None{
345345
varokbool
346-
ifm, ok=convertToBigInt(modulus); !ok{
346+
ifm, ok=ConvertToBigInt(modulus); !ok{
347347
returnNotImplemented, nil
348348
}
349349
}
350-
ifb, ok:=convertToBigInt(other); ok{
350+
ifb, ok:=ConvertToBigInt(other); ok{
351351
returna.pow(b, m)
352352
}
353353
returnNotImplemented, nil
354354
}
355355

356356
func (a*BigInt) M__rpow__(otherObject) (Object, error){
357-
ifb, ok:=convertToBigInt(other); ok{
357+
ifb, ok:=ConvertToBigInt(other); ok{
358358
returnb.pow(a, nil)
359359
}
360360
returnNotImplemented, nil
@@ -365,7 +365,7 @@ func (a *BigInt) M__ipow__(other, modulus Object) (Object, error){
365365
}
366366

367367
func (a*BigInt) M__lshift__(otherObject) (Object, error){
368-
ifb, ok:=convertToBigInt(other); ok{
368+
ifb, ok:=ConvertToBigInt(other); ok{
369369
bb, err:=b.GoInt()
370370
iferr!=nil{
371371
returnnil, err
@@ -379,7 +379,7 @@ func (a *BigInt) M__lshift__(other Object) (Object, error){
379379
}
380380

381381
func (a*BigInt) M__rlshift__(otherObject) (Object, error){
382-
ifb, ok:=convertToBigInt(other); ok{
382+
ifb, ok:=ConvertToBigInt(other); ok{
383383
aa, err:=a.GoInt()
384384
iferr!=nil{
385385
returnnil, err
@@ -397,7 +397,7 @@ func (a *BigInt) M__ilshift__(other Object) (Object, error){
397397
}
398398

399399
func (a*BigInt) M__rshift__(otherObject) (Object, error){
400-
ifb, ok:=convertToBigInt(other); ok{
400+
ifb, ok:=ConvertToBigInt(other); ok{
401401
bb, err:=b.GoInt()
402402
iferr!=nil{
403403
returnnil, err
@@ -411,7 +411,7 @@ func (a *BigInt) M__rshift__(other Object) (Object, error){
411411
}
412412

413413
func (a*BigInt) M__rrshift__(otherObject) (Object, error){
414-
ifb, ok:=convertToBigInt(other); ok{
414+
ifb, ok:=ConvertToBigInt(other); ok{
415415
aa, err:=a.GoInt()
416416
iferr!=nil{
417417
returnnil, err
@@ -429,7 +429,7 @@ func (a *BigInt) M__irshift__(other Object) (Object, error){
429429
}
430430

431431
func (a*BigInt) M__and__(otherObject) (Object, error){
432-
ifb, ok:=convertToBigInt(other); ok{
432+
ifb, ok:=ConvertToBigInt(other); ok{
433433
return (*BigInt)(new(big.Int).And((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
434434
}
435435
returnNotImplemented, nil
@@ -444,7 +444,7 @@ func (a *BigInt) M__iand__(other Object) (Object, error){
444444
}
445445

446446
func (a*BigInt) M__xor__(otherObject) (Object, error){
447-
ifb, ok:=convertToBigInt(other); ok{
447+
ifb, ok:=ConvertToBigInt(other); ok{
448448
return (*BigInt)(new(big.Int).Xor((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
449449
}
450450
returnNotImplemented, nil
@@ -459,7 +459,7 @@ func (a *BigInt) M__ixor__(other Object) (Object, error){
459459
}
460460

461461
func (a*BigInt) M__or__(otherObject) (Object, error){
462-
ifb, ok:=convertToBigInt(other); ok{
462+
ifb, ok:=ConvertToBigInt(other); ok{
463463
return (*BigInt)(new(big.Int).Or((*big.Int)(a), (*big.Int)(b))).MaybeInt(), nil
464464
}
465465
returnNotImplemented, nil
@@ -498,7 +498,7 @@ func (a *BigInt) M__complex__() (Object, error){
498498
}
499499

500500
func (a*BigInt) M__round__(digitsObject) (Object, error){
501-
ifb, ok:=convertToBigInt(digits); ok{
501+
ifb, ok:=ConvertToBigInt(digits); ok{
502502
if (*big.Int)(b).Sign() >=0{
503503
returna, nil
504504
}
@@ -528,42 +528,42 @@ func (a *BigInt) M__round__(digits Object) (Object, error){
528528
// Rich comparison
529529

530530
func (a*BigInt) M__lt__(otherObject) (Object, error){
531-
ifb, ok:=convertToBigInt(other); ok{
531+
ifb, ok:=ConvertToBigInt(other); ok{
532532
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) <0), nil
533533
}
534534
returnNotImplemented, nil
535535
}
536536

537537
func (a*BigInt) M__le__(otherObject) (Object, error){
538-
ifb, ok:=convertToBigInt(other); ok{
538+
ifb, ok:=ConvertToBigInt(other); ok{
539539
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) <=0), nil
540540
}
541541
returnNotImplemented, nil
542542
}
543543

544544
func (a*BigInt) M__eq__(otherObject) (Object, error){
545-
ifb, ok:=convertToBigInt(other); ok{
545+
ifb, ok:=ConvertToBigInt(other); ok{
546546
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) ==0), nil
547547
}
548548
returnNotImplemented, nil
549549
}
550550

551551
func (a*BigInt) M__ne__(otherObject) (Object, error){
552-
ifb, ok:=convertToBigInt(other); ok{
552+
ifb, ok:=ConvertToBigInt(other); ok{
553553
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) !=0), nil
554554
}
555555
returnNotImplemented, nil
556556
}
557557

558558
func (a*BigInt) M__gt__(otherObject) (Object, error){
559-
ifb, ok:=convertToBigInt(other); ok{
559+
ifb, ok:=ConvertToBigInt(other); ok{
560560
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) >0), nil
561561
}
562562
returnNotImplemented, nil
563563
}
564564

565565
func (a*BigInt) M__ge__(otherObject) (Object, error){
566-
ifb, ok:=convertToBigInt(other); ok{
566+
ifb, ok:=ConvertToBigInt(other); ok{
567567
returnNewBool((*big.Int)(a).Cmp((*big.Int)(b)) >=0), nil
568568
}
569569
returnNotImplemented, nil

‎version.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ var (
88
version="dev"
99
commit="none"
1010
date="unknown"
11-
)
11+
)

0 commit comments

Comments
(0)