Przeglądaj źródła

fix https://github.com/pocketpy/pocketpy/issues/329

blueloveTH 1 rok temu
rodzic
commit
1260987ead

+ 17 - 1
python/builtins.py

@@ -204,8 +204,24 @@ def help(obj):
 
 def complex(real, imag=0):
     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:
     def __init__(self, iterable=None):

Plik diff jest za duży
+ 0 - 0
src/common/_generated.c


+ 7 - 4
src/public/py_dict.c

@@ -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 == 1) return true;
     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);
-    py_Ref list = py_arg(1);
-    for(int i = 0; i < py_list_len(list); i++) {
-        py_Ref tuple = py_list_getitem(list, i);
+    for(int i = 0; i < length; i++) {
+        py_Ref tuple = &p[i];
         if(!py_istuple(tuple) || py_tuple_len(tuple) != 2) {
             return TypeError("dict.__init__() argument must be a list of tuple-2");
         }

+ 21 - 2
src/public/py_mappingproxy.c

@@ -52,9 +52,28 @@ static bool namedict_items(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     py_Ref object = py_getslot(argv, 0);
     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++) {
-        py_Ref slot = py_tuple_getitem(py_retval(), i);
+        py_Ref slot = py_list_emplace(py_retval());
         py_newtuple(slot, 2);
         NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
         py_newstr(py_tuple_getitem(slot, 0), py_name2str(kv->key));

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików