move isinstance

This commit is contained in:
blueloveTH 2023-01-27 03:55:43 +08:00
parent f129b1913d
commit 3bfb18e2c3
5 changed files with 22 additions and 18 deletions

View File

@ -14,6 +14,15 @@ def round(x, ndigits=0):
else:
return int(x * 10**ndigits - 0.5) / 10**ndigits
def isinstance(obj, cls):
assert type(cls) is type
obj_t = type(obj)
while obj_t is not None:
if obj_t is cls:
return True
obj_t = obj_t.__base__
return False
def abs(x):
return x < 0 ? -x : x

View File

@ -70,11 +70,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->f_locals_copy());
});
_vm->bindBuiltinFunc("isinstance", [](VM* vm, const pkpy::ArgList& args) {
vm->check_args_size(args, 2);
return vm->PyBool(vm->isinstance(args[0], args[1]));
});
_vm->bindBuiltinFunc("repr", [](VM* vm, const pkpy::ArgList& args) {
vm->check_args_size(args, 1);
return vm->asRepr(args[0]);

View File

@ -42,7 +42,6 @@ __NOT_ENOUGH_LINES:
}
try{
// duplicated compile to catch NeedMoreLines
vm->compile(line, "<stdin>", SINGLE_MODE);
}catch(NeedMoreLines& ne){
buffer += line;

View File

@ -365,7 +365,7 @@ public:
}
initializeBuiltinClasses();
_small_integers.reserve(300);
_small_integers.reserve(270);
for(i64 i=-5; i<=256; i++) _small_integers.push_back(new_object(_tp_int, i));
}
@ -682,16 +682,6 @@ public:
setattr(module, funcName, func);
}
bool isinstance(PyVar obj, PyVar type){
check_type(type, _tp_type);
PyObject* t = obj->_type.get();
while (t != None.get()){
if (t == type.get()) return true;
t = t->attribs[__base__].get();
}
return false;
}
inline bool is_int_or_float(const PyVar& obj) const{
return obj->is_type(_tp_int) || obj->is_type(_tp_float);
}

View File

@ -64,4 +64,15 @@ assert D.__base__ is C
d = D(1, 2, 3, 4, 5)
assert d.add() == 15
assert d.sub() == -13
assert d.sub() == -13
assert isinstance(1, int)
assert isinstance(1, object)
assert isinstance(C, type)
assert isinstance(C, object)
assert isinstance(d, object)
assert isinstance(d, C)
assert isinstance(d, B)
assert isinstance(d, A)
assert isinstance(object, object)
assert isinstance(type, object)