Skip to content

Commit 50298b0

Browse files
committed
stdlib/binascii: first import
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 8ca157b commit 50298b0

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

stdlib/binascii/binascii.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package binascii provides the implementation of the python's 'binascii' module.
6+
package binascii
7+
8+
import (
9+
"encoding/base64"
10+
"hash/crc32"
11+
12+
"github.com/go-python/gpython/py"
13+
)
14+
15+
var (
16+
Incomplete = py.ExceptionType.NewType("binascii.Incomplete", "", nil, nil)
17+
Error = py.ValueError.NewType("binascii.Error", "", nil, nil)
18+
)
19+
20+
func init() {
21+
py.RegisterModule(&py.ModuleImpl{
22+
Info: py.ModuleInfo{
23+
Name: "binascii",
24+
Doc: "Conversion between binary data and ASCII",
25+
},
26+
Methods: []*py.Method{
27+
py.MustNewMethod("a2b_base64", a2b_base64, 0, "Decode a line of base64 data."),
28+
py.MustNewMethod("b2a_base64", b2a_base64, 0, "Base64-code line of data."),
29+
py.MustNewMethod("crc32", crc32_, 0, "Compute CRC-32 incrementally."),
30+
},
31+
Globals: py.StringDict{
32+
"Incomplete": Incomplete,
33+
"Error": Error,
34+
},
35+
})
36+
}
37+
38+
func b2a_base64(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
39+
var (
40+
pydata py.Object
41+
pynewl py.Object = py.True
42+
)
43+
err := py.ParseTupleAndKeywords(args, kwargs, "y*|p:binascii.b2a_base64", []string{"data", "newline"}, &pydata, &pynewl)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
var (
49+
buf = []byte(pydata.(py.Bytes))
50+
newline = bool(pynewl.(py.Bool))
51+
)
52+
53+
out := base64.StdEncoding.EncodeToString(buf)
54+
if newline {
55+
out += "\n"
56+
}
57+
return py.Bytes(out), nil
58+
}
59+
60+
func a2b_base64(self py.Object, args py.Tuple) (py.Object, error) {
61+
var pydata py.Object
62+
err := py.ParseTuple(args, "s:binascii.a2b_base64", &pydata)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
out, err := base64.StdEncoding.DecodeString(string(pydata.(py.String)))
68+
if err != nil {
69+
return nil, py.ExceptionNewf(Error, "could not decode base64 data: %+v", err)
70+
}
71+
72+
return py.Bytes(out), nil
73+
}
74+
75+
func crc32_(self py.Object, args py.Tuple) (py.Object, error) {
76+
var (
77+
pydata py.Object
78+
pycrc py.Object = py.Int(0)
79+
)
80+
81+
err := py.ParseTuple(args, "y*|i:binascii.crc32", &pydata, &pycrc)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
crc := crc32.Update(uint32(pycrc.(py.Int)), crc32.IEEETable, []byte(pydata.(py.Bytes)))
87+
return py.Int(crc), nil
88+
89+
}

stdlib/binascii/binascii_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package binascii_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/go-python/gpython/pytest"
11+
)
12+
13+
func TestBinascii(t *testing.T) {
14+
pytest.RunScript(t, "./testdata/test.py")
15+
}

stdlib/binascii/testdata/test.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
import binascii
6+
7+
print("globals:")
8+
for name in ("Error", "Incomplete"):
9+
v = getattr(binascii, name)
10+
print("\nbinascii.%s:\n%s" % (name,repr(v)))
11+
12+
def assertEqual(x, y):
13+
assert x == y, "got: %s, want: %s" % (repr(x), repr(y))
14+
15+
## base64
16+
assertEqual(binascii.b2a_base64(b'hello world!'), b'aGVsbG8gd29ybGQh\n')
17+
assertEqual(binascii.b2a_base64(b'hello\nworld!'), b'aGVsbG8Kd29ybGQh\n')
18+
assertEqual(binascii.b2a_base64(b'hello world!', newline=False), b'aGVsbG8gd29ybGQh')
19+
assertEqual(binascii.b2a_base64(b'hello\nworld!', newline=False), b'aGVsbG8Kd29ybGQh')
20+
assertEqual(binascii.a2b_base64("aGVsbG8gd29ybGQh\n"), b'hello world!')
21+
22+
try:
23+
binascii.b2a_base64("string")
24+
print("expected an exception")
25+
except TypeError as e:
26+
print("expected an exception:", e)
27+
pass
28+
29+
## crc32
30+
assertEqual(binascii.crc32(b'hello world!'), 62177901)
31+
assertEqual(binascii.crc32(b'hello world!', 0), 62177901)
32+
assertEqual(binascii.crc32(b'hello world!', 42), 4055036404)
33+
34+
print("done.")
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
globals:
2+
3+
binascii.Error:
4+
<class 'binascii.Error'>
5+
6+
binascii.Incomplete:
7+
<class 'binascii.Incomplete'>
8+
expected an exception: binascii.b2a_base64() argument 1 must be bytes-like, not str
9+
done.

stdlib/stdlib.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/go-python/gpython/stdlib/marshal"
1919
"github.com/go-python/gpython/vm"
2020

21+
_ "github.com/go-python/gpython/stdlib/binascii"
2122
_ "github.com/go-python/gpython/stdlib/builtin"
2223
_ "github.com/go-python/gpython/stdlib/math"
2324
_ "github.com/go-python/gpython/stdlib/string"

0 commit comments

Comments
 (0)