Skip to content

Commit ece9d17

Browse files
committed
py: make bytes implement __{i,}add__
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 189965b commit ece9d17

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

py/bytes.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,27 @@ func (a Bytes) M__ge__(other Object) (Object, error) {
240240
return NotImplemented, nil
241241
}
242242

243+
func (a Bytes) M__add__(other Object) (Object, error) {
244+
if b, ok := convertToBytes(other); ok {
245+
o := make([]byte, len(a)+len(b))
246+
copy(o[:len(a)], a)
247+
copy(o[len(a):], b)
248+
return Bytes(o), nil
249+
}
250+
return NotImplemented, nil
251+
}
252+
253+
func (a *Bytes) M__iadd__(other Object) (Object, error) {
254+
if b, ok := convertToBytes(other); ok {
255+
*a = append(*a, b...)
256+
return *a, nil
257+
}
258+
return NotImplemented, nil
259+
}
260+
243261
// Check interface is satisfied
244-
var _ richComparison = (Bytes)(nil)
262+
var (
263+
_ richComparison = (Bytes)(nil)
264+
_ I__add__ = (Bytes)(nil)
265+
_ I__iadd__ = (*Bytes)(nil)
266+
)

0 commit comments

Comments
 (0)