diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 23306d04..49b2257c 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -203,6 +203,7 @@ void py_bind(py_Ref obj, const char* sig, py_CFunction f); // old style argc-based bindings void py_bindmethod(py_Type type, const char* name, py_CFunction f); void py_bindfunc(py_Ref obj, const char* name, py_CFunction f); +void py_bindproperty(py_Type type, const char* name, py_CFunction getter, py_CFunction setter); #define py_bindmagic(type, __magic__, f) py_newnativefunc(py_tpmagic((type), __magic__), (f)) diff --git a/src/public/modules.c b/src/public/modules.c index 5fbf3ddd..65216ce8 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -148,12 +148,12 @@ __SUCCESS: ////////////////////////// -static bool _py_builtins__repr(int argc, py_Ref argv) { +static bool builtins__repr(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return py_repr(argv); } -static bool _py_builtins__exit(int argc, py_Ref argv) { +static bool builtins__exit(int argc, py_Ref argv) { int code = 0; if(argc > 1) return TypeError("exit() takes at most 1 argument"); if(argc == 1) { @@ -165,12 +165,12 @@ static bool _py_builtins__exit(int argc, py_Ref argv) { return false; } -static bool _py_builtins__len(int argc, py_Ref argv) { +static bool builtins__len(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return py_len(argv); } -static bool _py_builtins__reversed(int argc, py_Ref 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; @@ -178,7 +178,7 @@ static bool _py_builtins__reversed(int argc, py_Ref argv) { return true; } -static bool _py_builtins__hex(int argc, py_Ref argv) { +static bool builtins__hex(int argc, py_Ref argv) { PY_CHECK_ARGC(1); PY_CHECK_ARG_TYPE(0, tp_int); @@ -208,12 +208,12 @@ static bool _py_builtins__hex(int argc, py_Ref argv) { return true; } -static bool _py_builtins__iter(int argc, py_Ref argv) { +static bool builtins__iter(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return py_iter(argv); } -static bool _py_builtins__next(int argc, py_Ref argv) { +static bool builtins__next(int argc, py_Ref argv) { PY_CHECK_ARGC(1); int res = py_next(argv); if(res == -1) return false; @@ -221,7 +221,7 @@ static bool _py_builtins__next(int argc, py_Ref argv) { return py_exception("StopIteration", ""); } -static bool _py_builtins__sorted(int argc, py_Ref argv) { +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; @@ -238,7 +238,7 @@ static bool _py_builtins__sorted(int argc, py_Ref argv) { return true; } -static bool _py_builtins__hash(int argc, py_Ref argv) { +static bool builtins__hash(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_i64 val; if(!py_hash(argv, &val)) return false; @@ -246,12 +246,12 @@ static bool _py_builtins__hash(int argc, py_Ref argv) { return true; } -static bool _py_builtins__abs(int argc, py_Ref argv) { +static bool builtins__abs(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return pk_callmagic(__abs__, 1, argv); } -static bool _py_builtins__sum(int argc, py_Ref argv) { +static bool builtins__sum(int argc, py_Ref argv) { PY_CHECK_ARGC(1); if(!py_iter(py_arg(0))) return false; @@ -288,7 +288,7 @@ static bool _py_builtins__sum(int argc, py_Ref argv) { return true; } -static bool _py_builtins__print(int argc, py_Ref argv) { +static bool builtins__print(int argc, py_Ref argv) { int length; py_TValue* args = pk_arrayview(argv, &length); assert(args != NULL); @@ -309,18 +309,18 @@ static bool _py_builtins__print(int argc, py_Ref argv) { return true; } -static bool _py_NoneType__repr__(int argc, py_Ref argv) { +static bool NoneType__repr__(int argc, py_Ref argv) { py_newstr(py_retval(), "None"); return true; } -static bool _py_builtins__exec(int argc, py_Ref argv) { +static bool builtins__exec(int argc, py_Ref argv) { PY_CHECK_ARGC(1); PY_CHECK_ARG_TYPE(0, tp_str); return py_exec(py_tostr(argv), "", EXEC_MODE, NULL); } -static bool _py_builtins__eval(int argc, py_Ref argv) { +static bool builtins__eval(int argc, py_Ref argv) { PY_CHECK_ARGC(1); PY_CHECK_ARG_TYPE(0, tp_str); return py_exec(py_tostr(argv), "", EVAL_MODE, NULL); @@ -328,25 +328,25 @@ static bool _py_builtins__eval(int argc, py_Ref argv) { py_TValue pk_builtins__register() { py_Ref builtins = py_newmodule("builtins"); - py_bindfunc(builtins, "repr", _py_builtins__repr); - py_bindfunc(builtins, "exit", _py_builtins__exit); - py_bindfunc(builtins, "len", _py_builtins__len); - py_bindfunc(builtins, "reversed", _py_builtins__reversed); - py_bindfunc(builtins, "hex", _py_builtins__hex); - py_bindfunc(builtins, "iter", _py_builtins__iter); - py_bindfunc(builtins, "next", _py_builtins__next); - py_bindfunc(builtins, "hash", _py_builtins__hash); - py_bindfunc(builtins, "abs", _py_builtins__abs); - py_bindfunc(builtins, "sum", _py_builtins__sum); + py_bindfunc(builtins, "repr", builtins__repr); + py_bindfunc(builtins, "exit", builtins__exit); + py_bindfunc(builtins, "len", builtins__len); + py_bindfunc(builtins, "reversed", builtins__reversed); + py_bindfunc(builtins, "hex", builtins__hex); + py_bindfunc(builtins, "iter", builtins__iter); + py_bindfunc(builtins, "next", builtins__next); + py_bindfunc(builtins, "hash", builtins__hash); + py_bindfunc(builtins, "abs", builtins__abs); + py_bindfunc(builtins, "sum", builtins__sum); - py_bindfunc(builtins, "exec", _py_builtins__exec); - py_bindfunc(builtins, "eval", _py_builtins__eval); + py_bindfunc(builtins, "exec", builtins__exec); + py_bindfunc(builtins, "eval", builtins__eval); - py_bind(builtins, "print(*args, sep=' ', end='\\n')", _py_builtins__print); - py_bind(builtins, "sorted(iterable, key=None, reverse=False)", _py_builtins__sorted); + py_bind(builtins, "print(*args, sep=' ', end='\\n')", builtins__print); + py_bind(builtins, "sorted(iterable, key=None, reverse=False)", builtins__sorted); // None __repr__ - py_bindmagic(tp_NoneType, __repr__, _py_NoneType__repr__); + py_bindmagic(tp_NoneType, __repr__, NoneType__repr__); return *builtins; } @@ -356,7 +356,7 @@ py_Type pk_function__register() { return type; } -static bool _py_nativefunc__repr(int argc, py_Ref argv) { +static bool nativefunc__repr(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_newstr(py_retval(), ""); return true; @@ -364,6 +364,6 @@ static bool _py_nativefunc__repr(int argc, py_Ref argv) { py_Type pk_nativefunc__register() { py_Type type = pk_newtype("nativefunc", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __repr__, _py_nativefunc__repr); + py_bindmagic(type, __repr__, nativefunc__repr); return type; } \ No newline at end of file diff --git a/src/public/py_array.c b/src/public/py_array.c index c38bc318..0e0c0c03 100644 --- a/src/public/py_array.c +++ b/src/public/py_array.c @@ -61,13 +61,13 @@ bool pk_arraycontains(py_Ref self, py_Ref val) { return true; } -static bool _py_array_iterator__iter__(int argc, py_Ref argv) { +static bool array_iterator__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); *py_retval() = *argv; return true; } -static bool _py_array_iterator__next__(int argc, py_Ref argv) { +static bool array_iterator__next__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); array_iterator* ud = py_touserdata(argv); if(ud->index < ud->length) { @@ -79,7 +79,7 @@ static bool _py_array_iterator__next__(int argc, py_Ref argv) { py_Type pk_array_iterator__register() { py_Type type = pk_newtype("array_iterator", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __iter__, _py_array_iterator__iter__); - py_bindmagic(type, __next__, _py_array_iterator__next__); + py_bindmagic(type, __iter__, array_iterator__iter__); + py_bindmagic(type, __next__, array_iterator__next__); return type; } diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 025dc521..f3a03c3b 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -204,12 +204,12 @@ static DictEntry* DictIterator__next(DictIterator* self) { } /////////////////////////////// -static bool _py_dict__new__(int argc, py_Ref argv) { +static bool dict__new__(int argc, py_Ref argv) { py_newdict(py_retval()); return true; } -static bool _py_dict__init__(int argc, py_Ref argv) { +static bool dict__init__(int argc, py_Ref argv) { if(argc > 2) return TypeError("dict.__init__() takes at most 2 arguments (%d given)", argc); if(argc == 1) return true; assert(argc == 2); @@ -228,7 +228,7 @@ static bool _py_dict__init__(int argc, py_Ref argv) { return true; } -static bool _py_dict__getitem__(int argc, py_Ref argv) { +static bool dict__getitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); Dict* self = py_touserdata(argv); DictEntry* entry; @@ -240,20 +240,20 @@ static bool _py_dict__getitem__(int argc, py_Ref argv) { return KeyError(py_arg(1)); } -static bool _py_dict__setitem__(int argc, py_Ref argv) { +static bool dict__setitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(3); Dict* self = py_touserdata(argv); return Dict__set(self, py_arg(1), py_arg(2)); } -static bool _py_dict__delitem__(int argc, py_Ref argv) { +static bool dict__delitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); Dict* self = py_touserdata(argv); if(!Dict__pop(self, py_arg(1))) return false; return true; } -static bool _py_dict__contains__(int argc, py_Ref argv) { +static bool dict__contains__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); Dict* self = py_touserdata(argv); DictEntry* entry; @@ -262,14 +262,14 @@ static bool _py_dict__contains__(int argc, py_Ref argv) { return true; } -static bool _py_dict__len__(int argc, py_Ref argv) { +static bool dict__len__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); py_newint(py_retval(), self->length); return true; } -static bool _py_dict__repr__(int argc, py_Ref argv) { +static bool dict__repr__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); c11_sbuf buf; @@ -292,7 +292,7 @@ static bool _py_dict__repr__(int argc, py_Ref argv) { return true; } -static bool _py_dict__eq__(int argc, py_Ref argv) { +static bool dict__eq__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); Dict* self = py_touserdata(py_arg(0)); if(!py_isdict(py_arg(1))) { @@ -326,8 +326,8 @@ static bool _py_dict__eq__(int argc, py_Ref argv) { return true; } -static bool _py_dict__ne__(int argc, py_Ref argv) { - if(!_py_dict__eq__(argc, argv)) return false; +static bool dict__ne__(int argc, py_Ref argv) { + if(!dict__eq__(argc, argv)) return false; if(py_isbool(py_retval())) { bool res = py_tobool(py_retval()); py_newbool(py_retval(), !res); @@ -335,14 +335,14 @@ static bool _py_dict__ne__(int argc, py_Ref argv) { return true; } -static bool _py_dict__clear(int argc, py_Ref argv) { +static bool dict__clear(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); Dict__clear(self); return true; } -static bool _py_dict__copy(int argc, py_Ref argv) { +static bool dict__copy(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); Dict* new_dict = py_newobject(py_retval(), tp_dict, 0, sizeof(Dict)); @@ -355,7 +355,7 @@ static bool _py_dict__copy(int argc, py_Ref argv) { return true; } -static bool _py_dict__update(int argc, py_Ref argv) { +static bool dict__update(int argc, py_Ref argv) { PY_CHECK_ARGC(2); PY_CHECK_ARG_TYPE(1, tp_dict); Dict* self = py_touserdata(argv); @@ -368,7 +368,7 @@ static bool _py_dict__update(int argc, py_Ref argv) { return true; } -static bool _py_dict__get(int argc, py_Ref argv) { +static bool dict__get(int argc, py_Ref argv) { Dict* self = py_touserdata(argv); if(argc > 3) return TypeError("get() takes at most 3 arguments (%d given)", argc); py_Ref default_val = argc == 3 ? py_arg(2) : py_None; @@ -378,7 +378,7 @@ static bool _py_dict__get(int argc, py_Ref argv) { return true; } -static bool _py_dict__pop(int argc, py_Ref argv) { +static bool dict__pop(int argc, py_Ref argv) { Dict* self = py_touserdata(argv); if(argc < 2 || argc > 3) return TypeError("pop() takes 2 or 3 arguments (%d given)", argc); py_Ref default_val = argc == 3 ? py_arg(2) : py_None; @@ -387,7 +387,7 @@ static bool _py_dict__pop(int argc, py_Ref argv) { return true; } -static bool _py_dict__items(int argc, py_Ref argv) { +static bool dict__items(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); DictIterator* ud = py_newobject(py_retval(), tp_dict_items, 1, sizeof(DictIterator)); @@ -396,7 +396,7 @@ static bool _py_dict__items(int argc, py_Ref argv) { return true; } -static bool _py_dict__keys(int argc, py_Ref argv) { +static bool dict__keys(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); py_newtuple(py_retval(), self->length); @@ -412,7 +412,7 @@ static bool _py_dict__keys(int argc, py_Ref argv) { return true; } -static bool _py_dict__values(int argc, py_Ref argv) { +static bool dict__values(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); py_newtuple(py_retval(), self->length); @@ -431,38 +431,38 @@ static bool _py_dict__values(int argc, py_Ref argv) { py_Type pk_dict__register() { py_Type type = pk_newtype("dict", tp_object, NULL, (void (*)(void*))Dict__dtor, false, false); - py_bindmagic(type, __new__, _py_dict__new__); - py_bindmagic(type, __init__, _py_dict__init__); - py_bindmagic(type, __getitem__, _py_dict__getitem__); - py_bindmagic(type, __setitem__, _py_dict__setitem__); - py_bindmagic(type, __delitem__, _py_dict__delitem__); - py_bindmagic(type, __contains__, _py_dict__contains__); - py_bindmagic(type, __len__, _py_dict__len__); - py_bindmagic(type, __repr__, _py_dict__repr__); - py_bindmagic(type, __eq__, _py_dict__eq__); - py_bindmagic(type, __ne__, _py_dict__ne__); + py_bindmagic(type, __new__, dict__new__); + py_bindmagic(type, __init__, dict__init__); + py_bindmagic(type, __getitem__, dict__getitem__); + py_bindmagic(type, __setitem__, dict__setitem__); + py_bindmagic(type, __delitem__, dict__delitem__); + py_bindmagic(type, __contains__, dict__contains__); + py_bindmagic(type, __len__, dict__len__); + py_bindmagic(type, __repr__, dict__repr__); + py_bindmagic(type, __eq__, dict__eq__); + py_bindmagic(type, __ne__, dict__ne__); - py_bindmethod(type, "clear", _py_dict__clear); - py_bindmethod(type, "copy", _py_dict__copy); - py_bindmethod(type, "update", _py_dict__update); - py_bindmethod(type, "get", _py_dict__get); - py_bindmethod(type, "pop", _py_dict__pop); - py_bindmethod(type, "items", _py_dict__items); - py_bindmethod(type, "keys", _py_dict__keys); - py_bindmethod(type, "values", _py_dict__values); + py_bindmethod(type, "clear", dict__clear); + py_bindmethod(type, "copy", dict__copy); + py_bindmethod(type, "update", dict__update); + py_bindmethod(type, "get", dict__get); + py_bindmethod(type, "pop", dict__pop); + py_bindmethod(type, "items", dict__items); + py_bindmethod(type, "keys", dict__keys); + py_bindmethod(type, "values", dict__values); py_setdict(py_tpobject(type), __hash__, py_None); return type; } ////////////////////////// -static bool _py_dict_items__iter__(int argc, py_Ref argv) { +static bool dict_items__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); *py_retval() = *argv; return true; } -static bool _py_dict_items__next__(int argc, py_Ref argv) { +static bool dict_items__next__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); DictIterator* iter = py_touserdata(py_arg(0)); DictEntry* entry = (DictIterator__next(iter)); @@ -477,8 +477,8 @@ static bool _py_dict_items__next__(int argc, py_Ref argv) { py_Type pk_dict_items__register() { py_Type type = pk_newtype("dict_items", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __iter__, _py_dict_items__iter__); - py_bindmagic(type, __next__, _py_dict_items__next__); + py_bindmagic(type, __iter__, dict_items__iter__); + py_bindmagic(type, __next__, dict_items__next__); return type; } @@ -524,7 +524,7 @@ int py_dict__len(py_Ref self) { return ud->length; } -bool py_dict__apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void *), void *ctx){ +bool py_dict__apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) { Dict* ud = py_touserdata(self); for(int i = 0; i < ud->entries.count; i++) { DictEntry* entry = c11__at(DictEntry, &ud->entries, i); diff --git a/src/public/py_list.c b/src/public/py_list.c index 46b0c3e0..40566897 100644 --- a/src/public/py_list.c +++ b/src/public/py_list.c @@ -65,14 +65,14 @@ void py_list__reverse(py_Ref self) { } //////////////////////////////// -static bool _py_list__len__(int argc, py_Ref argv) { +static bool list__len__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_i64 res = py_list__len(py_arg(0)); py_newint(py_retval(), res); return true; } -static bool _py_list__eq__(int argc, py_Ref argv) { +static bool list__eq__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); if(py_istype(py_arg(1), tp_list)) { int length0, length1; @@ -87,8 +87,8 @@ static bool _py_list__eq__(int argc, py_Ref argv) { return true; } -static bool _py_list__ne__(int argc, py_Ref argv) { - if(!_py_list__eq__(argc, argv)) return false; +static bool list__ne__(int argc, py_Ref argv) { + if(!list__eq__(argc, argv)) return false; if(py_isbool(py_retval())) { bool res = py_tobool(py_retval()); py_newbool(py_retval(), !res); @@ -96,7 +96,7 @@ static bool _py_list__ne__(int argc, py_Ref argv) { return true; } -static bool _py_list__new__(int argc, py_Ref argv) { +static bool list__new__(int argc, py_Ref argv) { if(argc == 1) { py_newlist(py_retval()); return true; @@ -120,7 +120,7 @@ static bool _py_list__new__(int argc, py_Ref argv) { py_newlist(list); while(true) { int res = py_next(iter); - if(res == -1){ + if(res == -1) { py_shrink(2); return false; } @@ -134,7 +134,7 @@ static bool _py_list__new__(int argc, py_Ref argv) { return TypeError("list() takes at most 1 argument"); } -static bool _py_list__getitem__(int argc, py_Ref argv) { +static bool list__getitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); List* self = py_touserdata(py_arg(0)); py_Ref _1 = py_arg(1); @@ -158,7 +158,7 @@ static bool _py_list__getitem__(int argc, py_Ref argv) { } } -static bool _py_list__setitem__(int argc, py_Ref argv) { +static bool list__setitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(3); PY_CHECK_ARG_TYPE(1, tp_int); List* self = py_touserdata(py_arg(0)); @@ -168,7 +168,7 @@ static bool _py_list__setitem__(int argc, py_Ref argv) { return true; } -static bool _py_list__delitem__(int argc, py_Ref argv) { +static bool list__delitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); PY_CHECK_ARG_TYPE(1, tp_int); List* self = py_touserdata(py_arg(0)); @@ -179,7 +179,7 @@ static bool _py_list__delitem__(int argc, py_Ref argv) { return true; } -static bool _py_list__add__(int argc, py_Ref argv) { +static bool list__add__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); py_Ref _0 = py_arg(0); py_Ref _1 = py_arg(1); @@ -196,7 +196,7 @@ static bool _py_list__add__(int argc, py_Ref argv) { return true; } -static bool _py_list__mul__(int argc, py_Ref argv) { +static bool list__mul__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); py_Ref _0 = py_arg(0); py_Ref _1 = py_arg(1); @@ -214,16 +214,16 @@ static bool _py_list__mul__(int argc, py_Ref argv) { return true; } -static bool _py_list__rmul__(int argc, py_Ref argv) { return _py_list__mul__(argc, argv); } +static bool list__rmul__(int argc, py_Ref argv) { return list__mul__(argc, argv); } -static bool _py_list__append(int argc, py_Ref argv) { +static bool list__append(int argc, py_Ref argv) { PY_CHECK_ARGC(2); py_list__append(py_arg(0), py_arg(1)); py_newnone(py_retval()); return true; } -static bool _py_list__repr__(int argc, py_Ref argv) { +static bool list__repr__(int argc, py_Ref argv) { List* self = py_touserdata(py_arg(0)); c11_sbuf buf; c11_sbuf__ctor(&buf); @@ -243,7 +243,7 @@ static bool _py_list__repr__(int argc, py_Ref argv) { return true; } -static bool _py_list__extend(int argc, py_Ref argv) { +static bool list__extend(int argc, py_Ref argv) { PY_CHECK_ARGC(2); List* self = py_touserdata(py_arg(0)); PY_CHECK_ARG_TYPE(1, tp_list); @@ -253,7 +253,7 @@ static bool _py_list__extend(int argc, py_Ref argv) { return true; } -static bool _py_list__count(int argc, py_Ref argv) { +static bool list__count(int argc, py_Ref argv) { PY_CHECK_ARGC(2); int count = 0; for(int i = 0; i < py_list__len(py_arg(0)); i++) { @@ -265,14 +265,14 @@ static bool _py_list__count(int argc, py_Ref argv) { return true; } -static bool _py_list__clear(int argc, py_Ref argv) { +static bool list__clear(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_list__clear(py_arg(0)); py_newnone(py_retval()); return true; } -static bool _py_list__copy(int argc, py_Ref argv) { +static bool list__copy(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_newlist(py_retval()); List* self = py_touserdata(py_arg(0)); @@ -281,7 +281,7 @@ static bool _py_list__copy(int argc, py_Ref argv) { return true; } -static bool _py_list__index(int argc, py_Ref argv) { +static bool list__index(int argc, py_Ref argv) { if(argc > 3) return TypeError("index() takes at most 3 arguments"); int start = 0; if(argc == 3) { @@ -299,7 +299,7 @@ static bool _py_list__index(int argc, py_Ref argv) { return ValueError("list.index(x): x not in list"); } -static bool _py_list__reverse(int argc, py_Ref argv) { +static bool list__reverse(int argc, py_Ref argv) { PY_CHECK_ARGC(1); List* self = py_touserdata(py_arg(0)); c11__reverse(py_TValue, self); @@ -307,7 +307,7 @@ static bool _py_list__reverse(int argc, py_Ref argv) { return true; } -static bool _py_list__remove(int argc, py_Ref argv) { +static bool list__remove(int argc, py_Ref argv) { PY_CHECK_ARGC(2); for(int i = 0; i < py_list__len(py_arg(0)); i++) { int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1)); @@ -321,7 +321,7 @@ static bool _py_list__remove(int argc, py_Ref argv) { return ValueError("list.remove(x): x not in list"); } -static bool _py_list__pop(int argc, py_Ref argv) { +static bool list__pop(int argc, py_Ref argv) { int index; if(argc == 1) { index = -1; @@ -339,7 +339,7 @@ static bool _py_list__pop(int argc, py_Ref argv) { return true; } -static bool _py_list__insert(int argc, py_Ref argv) { +static bool list__insert(int argc, py_Ref argv) { PY_CHECK_ARGC(3); PY_CHECK_ARG_TYPE(1, tp_int); List* self = py_touserdata(py_arg(0)); @@ -352,7 +352,7 @@ static bool _py_list__insert(int argc, py_Ref argv) { return true; } -static int _py_lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) { +static int lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) { if(!key) return py_less(a, b); pk_VM* vm = pk_current_vm; // project a @@ -375,7 +375,7 @@ static int _py_lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) { } // sort(self, key=None, reverse=False) -static bool _py_list__sort(int argc, py_Ref argv) { +static bool list__sort(int argc, py_Ref argv) { List* self = py_touserdata(py_arg(0)); py_Ref key = py_arg(1); @@ -384,7 +384,7 @@ static bool _py_list__sort(int argc, py_Ref argv) { bool ok = c11__stable_sort(self->data, self->count, sizeof(py_TValue), - (int (*)(const void*, const void*, void*))_py_lt_with_key, + (int (*)(const void*, const void*, void*))lt_with_key, key); if(!ok) return false; @@ -395,12 +395,12 @@ static bool _py_list__sort(int argc, py_Ref argv) { return true; } -static bool _py_list__iter__(int argc, py_Ref argv) { +static bool list__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return pk_arrayiter(argv); } -static bool _py_list__contains__(int argc, py_Ref argv) { +static bool list__contains__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); return pk_arraycontains(py_arg(0), py_arg(1)); } @@ -409,33 +409,33 @@ py_Type pk_list__register() { py_Type type = pk_newtype("list", tp_object, NULL, (void (*)(void*))c11_vector__dtor, false, true); - py_bindmagic(type, __len__, _py_list__len__); - py_bindmagic(type, __eq__, _py_list__eq__); - py_bindmagic(type, __ne__, _py_list__ne__); - py_bindmagic(type, __new__, _py_list__new__); - py_bindmagic(type, __getitem__, _py_list__getitem__); - py_bindmagic(type, __setitem__, _py_list__setitem__); - py_bindmagic(type, __delitem__, _py_list__delitem__); - py_bindmagic(type, __add__, _py_list__add__); - py_bindmagic(type, __mul__, _py_list__mul__); - py_bindmagic(type, __rmul__, _py_list__rmul__); - py_bindmagic(type, __repr__, _py_list__repr__); - py_bindmagic(type, __iter__, _py_list__iter__); - py_bindmagic(type, __contains__, _py_list__contains__); + py_bindmagic(type, __len__, list__len__); + py_bindmagic(type, __eq__, list__eq__); + py_bindmagic(type, __ne__, list__ne__); + py_bindmagic(type, __new__, list__new__); + py_bindmagic(type, __getitem__, list__getitem__); + py_bindmagic(type, __setitem__, list__setitem__); + py_bindmagic(type, __delitem__, list__delitem__); + py_bindmagic(type, __add__, list__add__); + py_bindmagic(type, __mul__, list__mul__); + py_bindmagic(type, __rmul__, list__rmul__); + py_bindmagic(type, __repr__, list__repr__); + py_bindmagic(type, __iter__, list__iter__); + py_bindmagic(type, __contains__, list__contains__); - py_bindmethod(type, "append", _py_list__append); - py_bindmethod(type, "extend", _py_list__extend); - py_bindmethod(type, "count", _py_list__count); - py_bindmethod(type, "clear", _py_list__clear); - py_bindmethod(type, "copy", _py_list__copy); - py_bindmethod(type, "index", _py_list__index); - py_bindmethod(type, "reverse", _py_list__reverse); - py_bindmethod(type, "remove", _py_list__remove); - py_bindmethod(type, "pop", _py_list__pop); - py_bindmethod(type, "insert", _py_list__insert); - py_bindmethod(type, "sort", _py_list__sort); + py_bindmethod(type, "append", list__append); + py_bindmethod(type, "extend", list__extend); + py_bindmethod(type, "count", list__count); + py_bindmethod(type, "clear", list__clear); + py_bindmethod(type, "copy", list__copy); + py_bindmethod(type, "index", list__index); + py_bindmethod(type, "reverse", list__reverse); + py_bindmethod(type, "remove", list__remove); + py_bindmethod(type, "pop", list__pop); + py_bindmethod(type, "insert", list__insert); + py_bindmethod(type, "sort", list__sort); - py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", _py_list__sort); + py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", list__sort); py_setdict(py_tpobject(type), __hash__, py_None); return type; diff --git a/src/public/py_object.c b/src/public/py_object.c index e2022813..7bf22f92 100644 --- a/src/public/py_object.c +++ b/src/public/py_object.c @@ -2,7 +2,7 @@ #include "pocketpy/common/sstream.h" #include "pocketpy/pocketpy.h" -static bool _py_object__new__(int argc, py_Ref argv) { +static bool object__new__(int argc, py_Ref argv) { if(argc == 0) return TypeError("object.__new__(): not enough arguments"); py_Type cls = py_totype(py_arg(0)); pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, cls); @@ -13,28 +13,28 @@ static bool _py_object__new__(int argc, py_Ref argv) { return true; } -static bool _py_object__hash__(int argc, py_Ref argv) { +static bool object__hash__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); assert(argv->is_ptr); py_newint(py_retval(), (py_i64)argv->_obj); return true; } -static bool _py_object__eq__(int argc, py_Ref argv) { +static bool object__eq__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); bool res = py_isidentical(py_arg(0), py_arg(1)); py_newbool(py_retval(), res); return true; } -static bool _py_object__ne__(int argc, py_Ref argv) { +static bool object__ne__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); bool res = py_isidentical(py_arg(0), py_arg(1)); py_newbool(py_retval(), !res); return true; } -static bool _py_object__repr__(int argc, py_Ref argv) { +static bool object__repr__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); assert(argv->is_ptr); c11_sbuf buf; @@ -44,7 +44,7 @@ static bool _py_object__repr__(int argc, py_Ref argv) { return true; } -static bool _py_type__repr__(int argc, py_Ref argv) { +static bool type__repr__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_sbuf buf; c11_sbuf__ctor(&buf); @@ -53,23 +53,33 @@ static bool _py_type__repr__(int argc, py_Ref argv) { return true; } -static bool _py_type__new__(int argc, py_Ref argv){ +static bool type__new__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); py_Type type = py_typeof(py_arg(1)); py_assign(py_retval(), py_tpobject(type)); return true; } +static bool type__base__getter(int argc, py_Ref argv) { + PY_CHECK_ARGC(1); + py_Type type = py_totype(argv); + pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, type); + py_assign(py_retval(), py_tpobject(ti->base)); + return true; +} + void pk_object__register() { // use staticmethod - py_bindmagic(tp_object, __new__, _py_object__new__); + py_bindmagic(tp_object, __new__, object__new__); - py_bindmagic(tp_object, __hash__, _py_object__hash__); - py_bindmagic(tp_object, __eq__, _py_object__eq__); - py_bindmagic(tp_object, __ne__, _py_object__ne__); - py_bindmagic(tp_object, __repr__, _py_object__repr__); + py_bindmagic(tp_object, __hash__, object__hash__); + py_bindmagic(tp_object, __eq__, object__eq__); + py_bindmagic(tp_object, __ne__, object__ne__); + py_bindmagic(tp_object, __repr__, object__repr__); // type patch... - py_bindmagic(tp_type, __repr__, _py_type__repr__); - py_bindmagic(tp_type, __new__, _py_type__new__); + py_bindmagic(tp_type, __repr__, type__repr__); + py_bindmagic(tp_type, __new__, type__new__); + + py_bindproperty(tp_type, "__base__", type__base__getter, NULL); } \ No newline at end of file diff --git a/src/public/py_range.c b/src/public/py_range.c index dd79d1d1..2b9d47ef 100644 --- a/src/public/py_range.c +++ b/src/public/py_range.c @@ -10,7 +10,7 @@ typedef struct Range { py_i64 step; } Range; -static bool _py_range__new__(int argc, py_Ref argv) { +static bool range__new__(int argc, py_Ref argv) { Range* ud = py_newobject(py_retval(), tp_range, 0, sizeof(Range)); switch(argc - 1) { // skip cls case 1: { @@ -41,7 +41,7 @@ static bool _py_range__new__(int argc, py_Ref argv) { return true; } -static bool _py_range__iter__(int argc, py_Ref argv) { +static bool range__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return py_tpcall(tp_range_iterator, 1, argv); } @@ -49,8 +49,8 @@ static bool _py_range__iter__(int argc, py_Ref argv) { py_Type pk_range__register() { py_Type type = pk_newtype("range", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __new__, _py_range__new__); - py_bindmagic(type, __iter__, _py_range__iter__); + py_bindmagic(type, __new__, range__new__); + py_bindmagic(type, __iter__, range__iter__); return type; } @@ -59,7 +59,7 @@ typedef struct RangeIterator { py_i64 current; } RangeIterator; -static bool _py_range_iterator__new__(int argc, py_Ref argv) { +static bool range_iterator__new__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); PY_CHECK_ARG_TYPE(1, tp_range); RangeIterator* ud = py_newobject(py_retval(), tp_range_iterator, 0, sizeof(RangeIterator)); @@ -68,13 +68,13 @@ static bool _py_range_iterator__new__(int argc, py_Ref argv) { return true; } -static bool _py_range_iterator__iter__(int argc, py_Ref argv) { +static bool range_iterator__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); *py_retval() = *py_arg(0); return true; } -static bool _py_range_iterator__next__(int argc, py_Ref argv) { +static bool range_iterator__next__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); RangeIterator* ud = py_touserdata(py_arg(0)); if(ud->range.step > 0) { @@ -90,8 +90,8 @@ static bool _py_range_iterator__next__(int argc, py_Ref argv) { py_Type pk_range_iterator__register() { py_Type type = pk_newtype("range_iterator", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __new__, _py_range_iterator__new__); - py_bindmagic(type, __iter__, _py_range_iterator__iter__); - py_bindmagic(type, __next__, _py_range_iterator__next__); + py_bindmagic(type, __new__, range_iterator__new__); + py_bindmagic(type, __iter__, range_iterator__iter__); + py_bindmagic(type, __next__, range_iterator__next__); return type; } \ No newline at end of file diff --git a/src/public/py_slice.c b/src/public/py_slice.c index 50da145b..738c0a8b 100644 --- a/src/public/py_slice.c +++ b/src/public/py_slice.c @@ -13,7 +13,7 @@ void py_newslice(py_Ref out) { out->_obj = obj; } -static bool _py_slice__new__(int argc, py_Ref argv) { +static bool slice__new__(int argc, py_Ref argv) { PY_CHECK_ARGC(1 + 3); py_Ref slice = py_retval(); py_newslice(slice); @@ -23,7 +23,7 @@ static bool _py_slice__new__(int argc, py_Ref argv) { return true; } -static bool _py_slice__repr__(int argc, py_Ref argv) { +static bool slice__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); c11_sbuf__write_cstr(&buf, "slice("); @@ -45,7 +45,7 @@ static bool _py_slice__repr__(int argc, py_Ref argv) { py_Type pk_slice__register() { py_Type type = pk_newtype("slice", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __new__, _py_slice__new__); - py_bindmagic(type, __repr__, _py_slice__repr__); + py_bindmagic(type, __new__, slice__new__); + py_bindmagic(type, __repr__, slice__repr__); return type; } \ No newline at end of file diff --git a/src/public/py_str.c b/src/public/py_str.c index d1944cd0..04bea8fd 100644 --- a/src/public/py_str.c +++ b/src/public/py_str.c @@ -58,7 +58,7 @@ unsigned char* py_tobytes(py_Ref self, int* size) { } //////////////////////////////// -static bool _py_str__new__(int argc, py_Ref argv) { +static bool str__new__(int argc, py_Ref argv) { assert(argc >= 1); if(argc == 1) { py_newstr(py_retval(), ""); @@ -68,7 +68,7 @@ static bool _py_str__new__(int argc, py_Ref argv) { return py_str(py_arg(1)); } -static bool _py_str__hash__(int argc, py_Ref argv) { +static bool str__hash__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); int size; const char* data = py_tostrn(&argv[0], &size); @@ -80,14 +80,14 @@ static bool _py_str__hash__(int argc, py_Ref argv) { return true; } -static bool _py_str__len__(int argc, py_Ref argv) { +static bool str__len__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_string* self = py_touserdata(&argv[0]); py_newint(py_retval(), c11_sv__u8_length((c11_sv){self->data, self->size})); return true; } -static bool _py_str__add__(int argc, py_Ref argv) { +static bool str__add__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); if(py_arg(1)->type != tp_str) { @@ -105,7 +105,7 @@ static bool _py_str__add__(int argc, py_Ref argv) { return true; } -static bool _py_str__mul__(int argc, py_Ref argv) { +static bool str__mul__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); if(py_arg(1)->type != tp_int) { @@ -128,9 +128,9 @@ static bool _py_str__mul__(int argc, py_Ref argv) { return true; } -static bool _py_str__rmul__(int argc, py_Ref argv) { return _py_str__mul__(argc, argv); } +static bool str__rmul__(int argc, py_Ref argv) { return str__mul__(argc, argv); } -static bool _py_str__contains__(int argc, py_Ref argv) { +static bool str__contains__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); if(py_arg(1)->type != tp_str) { @@ -143,13 +143,13 @@ static bool _py_str__contains__(int argc, py_Ref argv) { return true; } -static bool _py_str__str__(int argc, py_Ref argv) { +static bool str__str__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); *py_retval() = argv[0]; return true; } -static bool _py_str__repr__(int argc, py_Ref argv) { +static bool str__repr__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_sbuf buf; c11_sbuf__ctor(&buf); @@ -158,7 +158,7 @@ static bool _py_str__repr__(int argc, py_Ref argv) { return true; } -static bool _py_str__iter__(int argc, py_Ref argv) { +static bool str__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); int* ud = py_newobject(py_retval(), tp_str_iterator, 1, sizeof(int)); *ud = 0; @@ -166,7 +166,7 @@ static bool _py_str__iter__(int argc, py_Ref argv) { return true; } -static bool _py_str__getitem__(int argc, py_Ref argv) { +static bool str__getitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_sv self = c11_string__sv(py_touserdata(&argv[0])); py_Ref _1 = py_arg(1); @@ -190,7 +190,7 @@ static bool _py_str__getitem__(int argc, py_Ref argv) { } #define DEF_STR_CMP_OP(op, __f, __cond) \ - static bool _py_str##op(int argc, py_Ref argv) { \ + static bool str##op(int argc, py_Ref argv) { \ PY_CHECK_ARGC(2); \ c11_string* self = py_touserdata(&argv[0]); \ if(py_arg(1)->type != tp_str) { \ @@ -212,7 +212,7 @@ DEF_STR_CMP_OP(__ge__, c11_sv__cmp, res >= 0) #undef DEF_STR_CMP_OP -static bool _py_str__lower(int argc, py_Ref argv) { +static bool str__lower(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_string* self = py_touserdata(&argv[0]); int total_size = sizeof(c11_string) + self->size + 1; @@ -227,7 +227,7 @@ static bool _py_str__lower(int argc, py_Ref argv) { return true; } -static bool _py_str__upper(int argc, py_Ref argv) { +static bool str__upper(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_string* self = py_touserdata(&argv[0]); int total_size = sizeof(c11_string) + self->size + 1; @@ -242,7 +242,7 @@ static bool _py_str__upper(int argc, py_Ref argv) { return true; } -static bool _py_str__startswith(int argc, py_Ref argv) { +static bool str__startswith(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); PY_CHECK_ARG_TYPE(1, tp_str); @@ -251,7 +251,7 @@ static bool _py_str__startswith(int argc, py_Ref argv) { return true; } -static bool _py_str__endswith(int argc, py_Ref argv) { +static bool str__endswith(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); PY_CHECK_ARG_TYPE(1, tp_str); @@ -260,7 +260,7 @@ static bool _py_str__endswith(int argc, py_Ref argv) { return true; } -static bool _py_str__join(int argc, py_Ref argv) { +static bool str__join(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_sv self = c11_string__sv(py_touserdata(&argv[0])); py_Ref _1 = py_arg(1); @@ -292,7 +292,7 @@ static bool _py_str__join(int argc, py_Ref argv) { return true; } -static bool _py_str__replace(int argc, py_Ref argv) { +static bool str__replace(int argc, py_Ref argv) { PY_CHECK_ARGC(3); c11_string* self = py_touserdata(&argv[0]); PY_CHECK_ARG_TYPE(1, tp_str); @@ -306,7 +306,7 @@ static bool _py_str__replace(int argc, py_Ref argv) { return true; } -static bool _py_str__split(int argc, py_Ref argv) { +static bool str__split(int argc, py_Ref argv) { c11_sv self = c11_string__sv(py_touserdata(&argv[0])); c11_vector res; if(argc > 2) return TypeError("split() takes at most 2 arguments"); @@ -329,7 +329,7 @@ static bool _py_str__split(int argc, py_Ref argv) { return true; } -static bool _py_str__count(int argc, py_Ref argv) { +static bool str__count(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_string* self = py_touserdata(&argv[0]); PY_CHECK_ARG_TYPE(1, tp_str); @@ -339,7 +339,7 @@ static bool _py_str__count(int argc, py_Ref argv) { return true; } -static bool _py_str__strip_impl(bool left, bool right, int argc, py_Ref argv) { +static bool str__strip_impl(bool left, bool right, int argc, py_Ref argv) { c11_sv self = c11_string__sv(py_touserdata(&argv[0])); c11_sv chars; if(argc == 1) { @@ -355,19 +355,19 @@ static bool _py_str__strip_impl(bool left, bool right, int argc, py_Ref argv) { return true; } -static bool _py_str__strip(int argc, py_Ref argv) { - return _py_str__strip_impl(true, true, argc, argv); +static bool str__strip(int argc, py_Ref argv) { + return str__strip_impl(true, true, argc, argv); } -static bool _py_str__lstrip(int argc, py_Ref argv) { - return _py_str__strip_impl(true, false, argc, argv); +static bool str__lstrip(int argc, py_Ref argv) { + return str__strip_impl(true, false, argc, argv); } -static bool _py_str__rstrip(int argc, py_Ref argv) { - return _py_str__strip_impl(false, true, argc, argv); +static bool str__rstrip(int argc, py_Ref argv) { + return str__strip_impl(false, true, argc, argv); } -static bool _py_str__zfill(int argc, py_Ref argv) { +static bool str__zfill(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_sv self = c11_string__sv(py_touserdata(&argv[0])); PY_CHECK_ARG_TYPE(1, tp_int); @@ -387,7 +387,7 @@ static bool _py_str__zfill(int argc, py_Ref argv) { return true; } -static bool _py_str__widthjust_impl(bool left, int argc, py_Ref argv) { +static bool str__widthjust_impl(bool left, int argc, py_Ref argv) { if(argc > 1 + 2) return TypeError("expected at most 2 arguments"); char pad; if(argc == 1 + 1) { @@ -423,15 +423,15 @@ static bool _py_str__widthjust_impl(bool left, int argc, py_Ref argv) { return true; } -static bool _py_str__ljust(int argc, py_Ref argv) { - return _py_str__widthjust_impl(true, argc, argv); +static bool str__ljust(int argc, py_Ref argv) { + return str__widthjust_impl(true, argc, argv); } -static bool _py_str__rjust(int argc, py_Ref argv) { - return _py_str__widthjust_impl(false, argc, argv); +static bool str__rjust(int argc, py_Ref argv) { + return str__widthjust_impl(false, argc, argv); } -static bool _py_str__find(int argc, py_Ref argv) { +static bool str__find(int argc, py_Ref argv) { if(argc > 3) return TypeError("find() takes at most 3 arguments"); int start = 0; if(argc == 3) { @@ -446,8 +446,8 @@ static bool _py_str__find(int argc, py_Ref argv) { return true; } -static bool _py_str__index(int argc, py_Ref argv) { - bool ok = _py_str__find(argc, argv); +static bool str__index(int argc, py_Ref argv) { + bool ok = str__find(argc, argv); if(!ok) return false; if(py_toint(py_retval()) == -1) return ValueError("substring not found"); return true; @@ -457,51 +457,51 @@ py_Type pk_str__register() { py_Type type = pk_newtype("str", tp_object, NULL, NULL, false, true); // no need to dtor because the memory is controlled by the object - py_bindmagic(tp_str, __new__, _py_str__new__); - py_bindmagic(tp_str, __hash__, _py_str__hash__); - py_bindmagic(tp_str, __len__, _py_str__len__); - py_bindmagic(tp_str, __add__, _py_str__add__); - py_bindmagic(tp_str, __mul__, _py_str__mul__); - py_bindmagic(tp_str, __rmul__, _py_str__rmul__); - py_bindmagic(tp_str, __contains__, _py_str__contains__); - py_bindmagic(tp_str, __str__, _py_str__str__); - py_bindmagic(tp_str, __repr__, _py_str__repr__); - py_bindmagic(tp_str, __iter__, _py_str__iter__); - py_bindmagic(tp_str, __getitem__, _py_str__getitem__); + py_bindmagic(tp_str, __new__, str__new__); + py_bindmagic(tp_str, __hash__, str__hash__); + py_bindmagic(tp_str, __len__, str__len__); + py_bindmagic(tp_str, __add__, str__add__); + py_bindmagic(tp_str, __mul__, str__mul__); + py_bindmagic(tp_str, __rmul__, str__rmul__); + py_bindmagic(tp_str, __contains__, str__contains__); + py_bindmagic(tp_str, __str__, str__str__); + py_bindmagic(tp_str, __repr__, str__repr__); + py_bindmagic(tp_str, __iter__, str__iter__); + py_bindmagic(tp_str, __getitem__, str__getitem__); - py_bindmagic(tp_str, __eq__, _py_str__eq__); - py_bindmagic(tp_str, __ne__, _py_str__ne__); - py_bindmagic(tp_str, __lt__, _py_str__lt__); - py_bindmagic(tp_str, __le__, _py_str__le__); - py_bindmagic(tp_str, __gt__, _py_str__gt__); - py_bindmagic(tp_str, __ge__, _py_str__ge__); + py_bindmagic(tp_str, __eq__, str__eq__); + py_bindmagic(tp_str, __ne__, str__ne__); + py_bindmagic(tp_str, __lt__, str__lt__); + py_bindmagic(tp_str, __le__, str__le__); + py_bindmagic(tp_str, __gt__, str__gt__); + py_bindmagic(tp_str, __ge__, str__ge__); - py_bindmethod(tp_str, "lower", _py_str__lower); - py_bindmethod(tp_str, "upper", _py_str__upper); - py_bindmethod(tp_str, "startswith", _py_str__startswith); - py_bindmethod(tp_str, "endswith", _py_str__endswith); - py_bindmethod(tp_str, "join", _py_str__join); - py_bindmethod(tp_str, "replace", _py_str__replace); - py_bindmethod(tp_str, "split", _py_str__split); - py_bindmethod(tp_str, "count", _py_str__count); - py_bindmethod(tp_str, "strip", _py_str__strip); - py_bindmethod(tp_str, "lstrip", _py_str__lstrip); - py_bindmethod(tp_str, "rstrip", _py_str__rstrip); - py_bindmethod(tp_str, "zfill", _py_str__zfill); - py_bindmethod(tp_str, "ljust", _py_str__ljust); - py_bindmethod(tp_str, "rjust", _py_str__rjust); - py_bindmethod(tp_str, "find", _py_str__find); - py_bindmethod(tp_str, "index", _py_str__index); + py_bindmethod(tp_str, "lower", str__lower); + py_bindmethod(tp_str, "upper", str__upper); + py_bindmethod(tp_str, "startswith", str__startswith); + py_bindmethod(tp_str, "endswith", str__endswith); + py_bindmethod(tp_str, "join", str__join); + py_bindmethod(tp_str, "replace", str__replace); + py_bindmethod(tp_str, "split", str__split); + py_bindmethod(tp_str, "count", str__count); + py_bindmethod(tp_str, "strip", str__strip); + py_bindmethod(tp_str, "lstrip", str__lstrip); + py_bindmethod(tp_str, "rstrip", str__rstrip); + py_bindmethod(tp_str, "zfill", str__zfill); + py_bindmethod(tp_str, "ljust", str__ljust); + py_bindmethod(tp_str, "rjust", str__rjust); + py_bindmethod(tp_str, "find", str__find); + py_bindmethod(tp_str, "index", str__index); return type; } -static bool _py_str_iterator__iter__(int argc, py_Ref argv) { +static bool str_iterator__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); *py_retval() = argv[0]; return true; } -static bool _py_str_iterator__next__(int argc, py_Ref argv) { +static bool str_iterator__next__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); int* ud = py_touserdata(&argv[0]); int size; @@ -517,8 +517,8 @@ static bool _py_str_iterator__next__(int argc, py_Ref argv) { py_Type pk_str_iterator__register() { py_Type type = pk_newtype("str_iterator", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __iter__, _py_str_iterator__iter__); - py_bindmagic(type, __next__, _py_str_iterator__next__); + py_bindmagic(type, __iter__, str_iterator__iter__); + py_bindmagic(type, __next__, str_iterator__next__); return type; } diff --git a/src/public/py_tuple.c b/src/public/py_tuple.c index 95a8aa6d..b961162e 100644 --- a/src/public/py_tuple.c +++ b/src/public/py_tuple.c @@ -22,12 +22,12 @@ void py_tuple__setitem(py_Ref self, int i, py_Ref val) { py_setslot(self, i, val int py_tuple__len(py_Ref self) { return self->_obj->slots; } ////////////// -static bool _py_tuple__len__(int argc, py_Ref argv) { +static bool tuple__len__(int argc, py_Ref argv) { py_newint(py_retval(), py_tuple__len(argv)); return true; } -static bool _py_tuple__repr__(int argc, py_Ref argv) { +static bool tuple__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); c11_sbuf__write_char(&buf, '('); @@ -48,7 +48,7 @@ static bool _py_tuple__repr__(int argc, py_Ref argv) { return true; } -static bool _py_tuple__new__(int argc, py_Ref argv) { +static bool tuple__new__(int argc, py_Ref argv) { if(argc == 1 + 0) { py_newtuple(py_retval(), 0); return true; @@ -69,7 +69,7 @@ static bool _py_tuple__new__(int argc, py_Ref argv) { return TypeError("tuple() takes at most 1 argument"); } -static bool _py_tuple__getitem__(int argc, py_Ref argv) { +static bool tuple__getitem__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); int length = py_tuple__len(argv); py_Ref _1 = py_arg(1); @@ -97,7 +97,7 @@ static bool _py_tuple__getitem__(int argc, py_Ref argv) { } } -static bool _py_tuple__eq__(int argc, py_Ref argv) { +static bool tuple__eq__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); if(py_istype(py_arg(1), tp_tuple)) { int length0, length1; @@ -112,8 +112,8 @@ static bool _py_tuple__eq__(int argc, py_Ref argv) { return true; } -static bool _py_tuple__ne__(int argc, py_Ref argv) { - if(!_py_tuple__eq__(argc, argv)) return false; +static bool tuple__ne__(int argc, py_Ref argv) { + if(!tuple__eq__(argc, argv)) return false; if(py_isbool(py_retval())) { bool res = py_tobool(py_retval()); py_newbool(py_retval(), !res); @@ -121,12 +121,12 @@ static bool _py_tuple__ne__(int argc, py_Ref argv) { return true; } -static bool _py_tuple__iter__(int argc, py_Ref argv) { +static bool tuple__iter__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); return pk_arrayiter(argv); } -static bool _py_tuple__contains__(int argc, py_Ref argv) { +static bool tuple__contains__(int argc, py_Ref argv) { PY_CHECK_ARGC(2); return pk_arraycontains(py_arg(0), py_arg(1)); } @@ -134,13 +134,13 @@ static bool _py_tuple__contains__(int argc, py_Ref argv) { py_Type pk_tuple__register() { py_Type type = pk_newtype("tuple", tp_object, NULL, NULL, false, true); - py_bindmagic(type, __len__, _py_tuple__len__); - py_bindmagic(type, __repr__, _py_tuple__repr__); - py_bindmagic(type, __new__, _py_tuple__new__); - py_bindmagic(type, __getitem__, _py_tuple__getitem__); - py_bindmagic(type, __eq__, _py_tuple__eq__); - py_bindmagic(type, __ne__, _py_tuple__ne__); - py_bindmagic(type, __iter__, _py_tuple__iter__); - py_bindmagic(type, __contains__, _py_tuple__contains__); + py_bindmagic(type, __len__, tuple__len__); + py_bindmagic(type, __repr__, tuple__repr__); + py_bindmagic(type, __new__, tuple__new__); + py_bindmagic(type, __getitem__, tuple__getitem__); + py_bindmagic(type, __eq__, tuple__eq__); + py_bindmagic(type, __ne__, tuple__ne__); + py_bindmagic(type, __iter__, tuple__iter__); + py_bindmagic(type, __contains__, tuple__contains__); return type; } diff --git a/src/public/values.c b/src/public/values.c index e7055c63..4b7b1cc5 100644 --- a/src/public/values.c +++ b/src/public/values.c @@ -60,17 +60,26 @@ void py_bindfunc(py_Ref obj, const char* name, py_CFunction f) { py_setdict(obj, py_name(name), &tmp); } +void py_bindproperty(py_Type type, const char* name, py_CFunction getter, py_CFunction setter) { + py_TValue tmp; + py_newobject(&tmp, tp_property, 2, 0); + py_newnativefunc(py_getslot(&tmp, 0), getter); + if(setter) { + py_newnativefunc(py_getslot(&tmp, 1), setter); + } else { + py_setslot(&tmp, 1, py_None); + } + py_setdict(py_tpobject(type), py_name(name), &tmp); +} + void py_bind(py_Ref obj, const char* sig, py_CFunction f) { py_TValue tmp; py_Name name = py_newfunction(&tmp, sig, f, NULL, 0); py_setdict(obj, name, &tmp); } -py_Name py_newfunction(py_Ref out, - const char* sig, - py_CFunction f, - const char* docstring, - int slots) { +py_Name + py_newfunction(py_Ref out, const char* sig, py_CFunction f, const char* docstring, int slots) { char buffer[256]; snprintf(buffer, sizeof(buffer), "def %s: pass", sig); // fn(a, b, *c, d=1) -> None