Skip to content

Commit 2149ac5

Browse files
committed
py: make Exception implement __{str,repr}__
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 50298b0 commit 2149ac5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

py/exception.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,22 @@ func (e *Exception) M__getattr__(name string) (Object, error) {
360360
return e.Args, nil // FIXME All attributes are args!
361361
}
362362

363+
func (e *Exception) M__str__() (Object, error) {
364+
msg := e.Args.(Tuple)[0]
365+
return msg, nil
366+
}
367+
368+
func (e *Exception) M__repr__() (Object, error) {
369+
msg := e.Args.(Tuple)[0].(String)
370+
typ := e.Base.Name
371+
return String(fmt.Sprintf("%s(%q)", typ, string(msg))), nil
372+
}
373+
363374
// Check Interfaces
364-
var _ error = (*Exception)(nil)
365-
var _ error = (*ExceptionInfo)(nil)
375+
var (
376+
_ error = (*ExceptionInfo)(nil)
377+
378+
_ error = (*Exception)(nil)
379+
_ I__str__ = (*Exception)(nil)
380+
_ I__repr__ = (*Exception)(nil)
381+
)

0 commit comments

Comments
 (0)