mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
isinstance
support tuple
This commit is contained in:
parent
b1df516bf1
commit
393302ae85
@ -121,6 +121,14 @@ void init_builtins(VM* _vm) {
|
||||
});
|
||||
|
||||
_vm->bind_builtin_func<2>("isinstance", [](VM* vm, ArgsView args) {
|
||||
if(is_non_tagged_type(args[1], vm->tp_tuple)){
|
||||
Tuple& types = _CAST(Tuple&, args[1]);
|
||||
for(PyObject* type : types){
|
||||
vm->check_non_tagged_type(type, vm->tp_type);
|
||||
if(vm->isinstance(args[0], PK_OBJ_GET(Type, type))) return vm->True;
|
||||
}
|
||||
return vm->False;
|
||||
}
|
||||
vm->check_non_tagged_type(args[1], vm->tp_type);
|
||||
Type type = PK_OBJ_GET(Type, args[1]);
|
||||
return VAR(vm->isinstance(args[0], type));
|
||||
|
@ -77,6 +77,24 @@ assert isinstance(d, A)
|
||||
assert isinstance(object, object)
|
||||
assert isinstance(type, object)
|
||||
|
||||
assert isinstance(1, (float, int))
|
||||
assert isinstance(1, (float, object))
|
||||
assert not isinstance(1, (float, str))
|
||||
assert isinstance(object, (int, type, float))
|
||||
assert not isinstance(object, (int, float, str))
|
||||
|
||||
try:
|
||||
isinstance(1, (1, 2))
|
||||
exit(1)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
isinstance(1, 1)
|
||||
exit(1)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
class A:
|
||||
a = 1
|
||||
b = 2
|
||||
|
Loading…
x
Reference in New Issue
Block a user