mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
6805b418b5
commit
1c1e898950
@ -452,7 +452,6 @@ int py_list_len(py_Ref self);
|
|||||||
void py_list_append(py_Ref self, py_Ref val);
|
void py_list_append(py_Ref self, py_Ref val);
|
||||||
void py_list_clear(py_Ref self);
|
void py_list_clear(py_Ref self);
|
||||||
void py_list_insert(py_Ref self, int i, py_Ref val);
|
void py_list_insert(py_Ref self, int i, py_Ref val);
|
||||||
void py_list_reverse(py_Ref self);
|
|
||||||
|
|
||||||
py_TmpRef py_dict_getitem(py_Ref self, py_Ref key) PY_RAISE;
|
py_TmpRef py_dict_getitem(py_Ref self, py_Ref key) PY_RAISE;
|
||||||
void py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE;
|
void py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from pkpy import next as __builtins_next
|
from pkpy import next as __pkpy_next
|
||||||
|
|
||||||
def all(iterable):
|
def all(iterable):
|
||||||
for i in iterable:
|
for i in iterable:
|
||||||
@ -37,8 +37,8 @@ def zip(a, b):
|
|||||||
a = iter(a)
|
a = iter(a)
|
||||||
b = iter(b)
|
b = iter(b)
|
||||||
while True:
|
while True:
|
||||||
ai = __builtins_next(a)
|
ai = __pkpy_next(a)
|
||||||
bi = __builtins_next(b)
|
bi = __pkpy_next(b)
|
||||||
if ai is StopIteration or bi is StopIteration:
|
if ai is StopIteration or bi is StopIteration:
|
||||||
break
|
break
|
||||||
yield ai, bi
|
yield ai, bi
|
||||||
@ -181,37 +181,3 @@ def complex(*args, **kwargs):
|
|||||||
def long(*args, **kwargs):
|
def long(*args, **kwargs):
|
||||||
import _long
|
import _long
|
||||||
return _long.long(*args, **kwargs)
|
return _long.long(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
# builtin exceptions
|
|
||||||
class StackOverflowError(Exception): pass
|
|
||||||
class IOError(Exception): pass
|
|
||||||
class NotImplementedError(Exception): pass
|
|
||||||
class TypeError(Exception): pass
|
|
||||||
class IndexError(Exception): pass
|
|
||||||
class ValueError(Exception): pass
|
|
||||||
class RuntimeError(Exception): pass
|
|
||||||
class ZeroDivisionError(Exception): pass
|
|
||||||
class NameError(Exception): pass
|
|
||||||
class UnboundLocalError(Exception): pass
|
|
||||||
class AttributeError(Exception): pass
|
|
||||||
class ImportError(Exception): pass
|
|
||||||
class AssertionError(Exception): pass
|
|
||||||
|
|
||||||
class KeyError(Exception):
|
|
||||||
def __init__(self, key=...):
|
|
||||||
self.key = key
|
|
||||||
if key is ...:
|
|
||||||
super().__init__()
|
|
||||||
else:
|
|
||||||
super().__init__(repr(key))
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if self.key is ...:
|
|
||||||
return ''
|
|
||||||
return str(self.key)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
if self.key is ...:
|
|
||||||
return 'KeyError()'
|
|
||||||
return f'KeyError({self.key!r})'
|
|
||||||
|
File diff suppressed because one or more lines are too long
@ -197,11 +197,15 @@ void VM__ctor(VM* self) {
|
|||||||
|
|
||||||
// add python builtins
|
// add python builtins
|
||||||
do {
|
do {
|
||||||
bool ok = py_exec(kPythonLibs__set, "<builtins>", EXEC_MODE, &self->builtins);
|
bool ok;
|
||||||
if(!ok) {
|
ok = py_exec(kPythonLibs_builtins, "<builtins>", EXEC_MODE, &self->builtins);
|
||||||
py_printexc();
|
if(!ok) goto __ABORT;
|
||||||
c11__abort("failed to load python builtins!");
|
ok = py_exec(kPythonLibs__set, "<builtins>", EXEC_MODE, &self->builtins);
|
||||||
}
|
if(!ok) goto __ABORT;
|
||||||
|
break;
|
||||||
|
__ABORT:
|
||||||
|
py_printexc();
|
||||||
|
c11__abort("failed to load python builtins!");
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
self->main = *py_newmodule("__main__");
|
self->main = *py_newmodule("__main__");
|
||||||
|
@ -170,14 +170,6 @@ static bool builtins_len(int argc, py_Ref argv) {
|
|||||||
return py_len(argv);
|
return py_len(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool builtins_reversed(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(1);
|
|
||||||
// convert _0 to list object
|
|
||||||
if(!py_tpcall(tp_list, 1, argv)) return false;
|
|
||||||
py_list_reverse(py_retval());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool builtins_hex(int argc, py_Ref argv) {
|
static bool builtins_hex(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
PY_CHECK_ARG_TYPE(0, tp_int);
|
PY_CHECK_ARG_TYPE(0, tp_int);
|
||||||
@ -221,23 +213,6 @@ static bool builtins_next(int argc, py_Ref argv) {
|
|||||||
return py_exception(tp_StopIteration, "");
|
return py_exception(tp_StopIteration, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool builtins_sorted(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(3);
|
|
||||||
// convert _0 to list object
|
|
||||||
if(!py_tpcall(tp_list, 1, py_arg(0))) return false;
|
|
||||||
py_push(py_retval()); // duptop
|
|
||||||
py_push(py_retval()); // [| <list>]
|
|
||||||
bool ok = py_pushmethod(py_name("sort")); // [| list.sort, <list>]
|
|
||||||
if(!ok) return false;
|
|
||||||
py_push(py_arg(1)); // [| list.sort, <list>, key]
|
|
||||||
py_push(py_arg(2)); // [| list.sort, <list>, key, reverse]
|
|
||||||
ok = py_vectorcall(2, 0); // [| ]
|
|
||||||
if(!ok) return false;
|
|
||||||
py_assign(py_retval(), py_peek(-1));
|
|
||||||
py_pop();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool builtins_hash(int argc, py_Ref argv) {
|
static bool builtins_hash(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
py_i64 val;
|
py_i64 val;
|
||||||
@ -251,43 +226,6 @@ static bool builtins_abs(int argc, py_Ref argv) {
|
|||||||
return pk_callmagic(__abs__, 1, argv);
|
return pk_callmagic(__abs__, 1, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool builtins_sum(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(1);
|
|
||||||
|
|
||||||
if(!py_iter(py_arg(0))) return false;
|
|
||||||
py_push(py_retval()); // iter
|
|
||||||
|
|
||||||
py_i64 total_i64 = 0;
|
|
||||||
py_f64 total_f64 = 0.0;
|
|
||||||
bool is_float = false;
|
|
||||||
while(true) {
|
|
||||||
int res = py_next(py_peek(-1));
|
|
||||||
if(res == -1) {
|
|
||||||
py_pop();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if(res == 0) break;
|
|
||||||
|
|
||||||
py_Ref item = py_retval();
|
|
||||||
switch(item->type) {
|
|
||||||
case tp_int: total_i64 += item->_i64; break;
|
|
||||||
case tp_float:
|
|
||||||
is_float = true;
|
|
||||||
total_f64 += item->_f64;
|
|
||||||
break;
|
|
||||||
default: return TypeError("sum() expects an iterable of numbers");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(is_float) {
|
|
||||||
py_newfloat(py_retval(), total_f64 + total_i64);
|
|
||||||
} else {
|
|
||||||
py_newint(py_retval(), total_i64);
|
|
||||||
}
|
|
||||||
py_pop();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool builtins_divmod(int argc, py_Ref argv) {
|
static bool builtins_divmod(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
return pk_callmagic(__divmod__, 2, argv);
|
return pk_callmagic(__divmod__, 2, argv);
|
||||||
@ -439,20 +377,17 @@ py_TValue pk_builtins__register() {
|
|||||||
py_bindfunc(builtins, "repr", builtins_repr);
|
py_bindfunc(builtins, "repr", builtins_repr);
|
||||||
py_bindfunc(builtins, "exit", builtins_exit);
|
py_bindfunc(builtins, "exit", builtins_exit);
|
||||||
py_bindfunc(builtins, "len", builtins_len);
|
py_bindfunc(builtins, "len", builtins_len);
|
||||||
py_bindfunc(builtins, "reversed", builtins_reversed);
|
|
||||||
py_bindfunc(builtins, "hex", builtins_hex);
|
py_bindfunc(builtins, "hex", builtins_hex);
|
||||||
py_bindfunc(builtins, "iter", builtins_iter);
|
py_bindfunc(builtins, "iter", builtins_iter);
|
||||||
py_bindfunc(builtins, "next", builtins_next);
|
py_bindfunc(builtins, "next", builtins_next);
|
||||||
py_bindfunc(builtins, "hash", builtins_hash);
|
py_bindfunc(builtins, "hash", builtins_hash);
|
||||||
py_bindfunc(builtins, "abs", builtins_abs);
|
py_bindfunc(builtins, "abs", builtins_abs);
|
||||||
py_bindfunc(builtins, "sum", builtins_sum);
|
|
||||||
py_bindfunc(builtins, "divmod", builtins_divmod);
|
py_bindfunc(builtins, "divmod", builtins_divmod);
|
||||||
|
|
||||||
py_bindfunc(builtins, "exec", builtins_exec);
|
py_bindfunc(builtins, "exec", builtins_exec);
|
||||||
py_bindfunc(builtins, "eval", builtins_eval);
|
py_bindfunc(builtins, "eval", builtins_eval);
|
||||||
|
|
||||||
py_bind(builtins, "print(*args, sep=' ', end='\\n')", builtins_print);
|
py_bind(builtins, "print(*args, sep=' ', end='\\n')", builtins_print);
|
||||||
py_bind(builtins, "sorted(iterable, key=None, reverse=False)", builtins_sorted);
|
|
||||||
|
|
||||||
py_bindfunc(builtins, "isinstance", builtins_isinstance);
|
py_bindfunc(builtins, "isinstance", builtins_isinstance);
|
||||||
py_bindfunc(builtins, "issubclass", builtins_issubclass);
|
py_bindfunc(builtins, "issubclass", builtins_issubclass);
|
||||||
|
@ -59,11 +59,6 @@ void py_list_insert(py_Ref self, int i, py_Ref val) {
|
|||||||
c11_vector__insert(py_TValue, userdata, i, *val);
|
c11_vector__insert(py_TValue, userdata, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_reverse(py_Ref self) {
|
|
||||||
List* userdata = py_touserdata(self);
|
|
||||||
c11__reverse(py_TValue, userdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
static bool list__len__(int argc, py_Ref argv) {
|
static bool list__len__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user