blueloveTH 2025-02-03 16:01:10 +08:00
parent c11007f059
commit 1260987ead
4 changed files with 46 additions and 8 deletions

View File

@ -204,8 +204,24 @@ def help(obj):
def complex(real, imag=0): def complex(real, imag=0):
import cmath import cmath
return cmath.complex(real, imag) return cmath.complex(real, imag) # type: ignore
def dir(obj) -> list[str]:
tp_module = type(__import__('math'))
if isinstance(obj, tp_module):
return [k for k, _ in obj.__dict__.items()]
names = set()
if not isinstance(obj, type):
obj_d = obj.__dict__
if obj_d is not None:
names.update([k for k, _ in obj_d.items()])
cls = type(obj)
else:
cls = obj
while cls is not None:
names.update([k for k, _ in cls.__dict__.items()])
cls = cls.__base__
return sorted(list(names))
class set: class set:
def __init__(self, iterable=None): def __init__(self, iterable=None):

File diff suppressed because one or more lines are too long

View File

@ -291,11 +291,14 @@ 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 > 2) return TypeError("dict.__init__() takes at most 2 arguments (%d given)", argc);
if(argc == 1) return true; if(argc == 1) return true;
assert(argc == 2); assert(argc == 2);
PY_CHECK_ARG_TYPE(1, tp_list);
py_TValue* p;
int length = pk_arrayview(py_arg(1), &p);
if(length == -1) { return TypeError("dict.__init__() expects a list or tuple"); }
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
py_Ref list = py_arg(1); for(int i = 0; i < length; i++) {
for(int i = 0; i < py_list_len(list); i++) { py_Ref tuple = &p[i];
py_Ref tuple = py_list_getitem(list, i);
if(!py_istuple(tuple) || py_tuple_len(tuple) != 2) { if(!py_istuple(tuple) || py_tuple_len(tuple) != 2) {
return TypeError("dict.__init__() argument must be a list of tuple-2"); return TypeError("dict.__init__() argument must be a list of tuple-2");
} }

View File

@ -52,9 +52,28 @@ static bool namedict_items(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_Ref object = py_getslot(argv, 0); py_Ref object = py_getslot(argv, 0);
NameDict* dict = PyObject__dict(object->_obj); NameDict* dict = PyObject__dict(object->_obj);
py_newtuple(py_retval(), dict->length); py_newlist(py_retval());
if(object->type == tp_type) {
py_TypeInfo* ti = pk__type_info(py_totype(object));
for(int j = 0; j < PK_MAGIC_SLOTS_COMMON_LENGTH; j++) {
if(py_isnil(ti->magic_0 + j)) continue;
py_Ref slot = py_list_emplace(py_retval());
py_newtuple(slot, 2);
py_newstr(py_tuple_getitem(slot, 0), py_name2str(j + PK_MAGIC_SLOTS_UNCOMMON_LENGTH));
py_assign(py_tuple_getitem(slot, 1), ti->magic_0 + j);
}
if(ti->magic_1) {
for(int j = 0; j < PK_MAGIC_SLOTS_UNCOMMON_LENGTH; j++) {
if(py_isnil(ti->magic_1 + j)) continue;
py_Ref slot = py_list_emplace(py_retval());
py_newtuple(slot, 2);
py_newstr(py_tuple_getitem(slot, 0), py_name2str(j));
py_assign(py_tuple_getitem(slot, 1), ti->magic_1 + j);
}
}
}
for(int i = 0; i < dict->length; i++) { for(int i = 0; i < dict->length; i++) {
py_Ref slot = py_tuple_getitem(py_retval(), i); py_Ref slot = py_list_emplace(py_retval());
py_newtuple(slot, 2); py_newtuple(slot, 2);
NameDict_KV* kv = c11__at(NameDict_KV, dict, i); NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
py_newstr(py_tuple_getitem(slot, 0), py_name2str(kv->key)); py_newstr(py_tuple_getitem(slot, 0), py_name2str(kv->key));