Skip to content

Commit e54f133

Browse files
committed
Refactored isinstance and builtin_isinstance function
1 parent 0df565c commit e54f133

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

builtin/builtin.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -835,39 +835,37 @@ check against. This is equivalent to isinstance(x, A) or isinstance(x, B)
835835
or ... etc.
836836
`
837837

838-
func isinstance(args py.Tuple) (py.Bool, error) {
839-
switch args[1].(type) {
838+
func isinstance(obj py.Object, classOrTuple py.Object) (py.Bool, error) {
839+
print(obj.Type().Name, "\t", classOrTuple.Type().Name, "\n")
840+
switch classOrTuple.(type) {
840841
case py.Tuple:
841-
{
842-
var res py.Bool
843-
var class = args[1].(py.Tuple)
844-
for idx := range class {
845-
var new_args []py.Object
846-
new_args = append(new_args, args[0], class[idx])
847-
temp, err := isinstance(new_args)
848-
if err != nil {
849-
return false, err
850-
}
851-
res = res || temp
842+
var res py.Bool
843+
var class_tuple = classOrTuple.(py.Tuple)
844+
for idx := range class_tuple {
845+
temp, err := isinstance(obj, class_tuple[idx])
846+
if err != nil {
847+
return false, err
852848
}
853-
return res, nil
849+
res = res || temp
854850
}
851+
return res, nil
855852
default:
856-
{
857-
if args[0].Type() != py.TypeType {
858-
return false, py.ExceptionNewf(py.TypeError, "isinstance() arg 2 must be a type or tuple of types")
859-
}
860-
return args[0].Type() == args[1], nil
853+
if classOrTuple.Type().ObjectType != py.TypeType {
854+
return false, py.ExceptionNewf(py.TypeError, "isinstance() arg 2 must be a type or tuple of types")
861855
}
856+
return obj.Type() == classOrTuple, nil
862857
}
863858
}
864859

865860
func builtin_isinstance(self py.Object, args py.Tuple) (py.Object, error) {
866-
if len(args) > 2 {
867-
return nil, py.ExceptionNewf(py.TypeError, "isinstance expected 2 arguments, got %d", len(args))
861+
var obj py.Object
862+
var classOrTuple py.Object
863+
err := py.UnpackTuple(args, nil, "isinstance", 2, 2, &obj, &classOrTuple)
864+
if err != nil {
865+
return nil, err
868866
}
869867

870-
return isinstance(args)
868+
return isinstance(obj, classOrTuple)
871869
}
872870

873871
const iter_doc = `iter(iterable) -> iterator

0 commit comments

Comments
 (0)