From 75ffcaeace8b201b45c395710a4252fa8351b241 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 15 Apr 2014 01:20:52 +0300 Subject: [PATCH 1/2] py: Implement __delitem__ method for classes. --- py/objtype.c | 14 +++++++++----- tests/basics/class_item.py | 6 +++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/py/objtype.c b/py/objtype.c index c1749ff5f3..198f76746a 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -309,16 +309,20 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { } bool class_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { + mp_obj_class_t *self = self_in; + mp_obj_t member; + uint meth_args; if (value == MP_OBJ_NULL) { // delete item - // TODO implement me! - return false; + member = mp_obj_class_lookup(self->base.type, MP_QSTR___delitem__); + meth_args = 2; + } else { + member = mp_obj_class_lookup(self->base.type, MP_QSTR___setitem__); + meth_args = 3; } - mp_obj_class_t *self = self_in; - mp_obj_t member = mp_obj_class_lookup(self->base.type, MP_QSTR___setitem__); if (member != MP_OBJ_NULL) { mp_obj_t args[3] = {self_in, index, value}; - mp_call_function_n_kw(member, 3, 0, args); + mp_call_function_n_kw(member, meth_args, 0, args); return true; } else { return false; diff --git a/tests/basics/class_item.py b/tests/basics/class_item.py index 6061f26075..a96817462f 100644 --- a/tests/basics/class_item.py +++ b/tests/basics/class_item.py @@ -1,4 +1,4 @@ -# test class with __getitem__ and __setitem__ methods +# test class with __getitem__, __setitem__, __delitem__ methods class C: def __getitem__(self, item): @@ -8,6 +8,10 @@ class C: def __setitem__(self, item, value): print('set', item, value) + def __delitem__(self, item): + print('del', item) + c = C() print(c[1]) c[1] = 2 +del c[3] From a5854d2bc5717cb5b6f589f367acaa6d0c8b0179 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 15 Apr 2014 01:23:40 +0300 Subject: [PATCH 2/2] builtinimport: Add basic support for namespace packages. That was easy - just avoid erroring out on seeing candidate dir for namespace package. That's far from being complete though - namespace packages should support importing portions of package from different sys.path entries, here we require first matching entry to contain all namespace package's portions. And yet, that's a way to put parts of the same Python package into multiple installable package - something we really need for *Micro*Python. --- py/builtinimport.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/py/builtinimport.c b/py/builtinimport.c index 3f63768ad4..262ee04a53 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -292,11 +292,10 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) { vstr_add_str(&path, "__init__.py"); if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) { vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, - "Per PEP-420 a dir without __init__.py (%s) is a namespace package; " - "namespace packages are not supported", vstr_str(&path))); + printf("Notice: %s is imported as namespace package\n", vstr_str(&path)); + } else { + do_load(module_obj, &path); } - do_load(module_obj, &path); vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py // https://docs.python.org/3.3/reference/import.html // "Specifically, any module that contains a __path__ attribute is considered a package."