8000 Pass subobject into native subscr · adafruit/circuitpython@be30c12 · GitHub
[go: up one dir, main page]

Skip to content

Commit be30c12

Browse files
committed
Pass subobject into native subscr
This allows PixelBuf to call transmit after setting a value. Fixes #8488
1 parent c3cc76d commit be30c12

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

ports/unix/native_base_class.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ STATIC const mp_rom_map_elem_t native_base_class_locals_dict_table[] = {
7676
};
7777
STATIC MP_DEFINE_CONST_DICT(native_base_class_locals_dict, native_base_class_locals_dict_table);
7878

79+
STATIC mp_obj_t native_base_class_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
80+
mp_obj_t attribute_value = mp_load_attr(self_in, MP_QSTR_new_attribute);
81+
mp_printf(&mp_plat_print, "native base class subscr .new_attribute set to: ");
82+
mp_obj_print_helper(&mp_plat_print, attribute_value, PRINT_REPR);
83+
mp_printf(&mp_plat_print, "\n");
84+
return attribute_value;
85+
}
86+
7987
MP_DEFINE_CONST_OBJ_TYPE(
8088
native_base_class_type,
8189
MP_QSTR_NativeBaseClass,
8290
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
8391
make_new, &native_base_class_make_new,
84-
locals_dict, &native_base_class_locals_dict
92+
locals_dict, &native_base_class_locals_dict,
93+
subscr, &native_base_class_subscr
8594
);
8695

8796
#endif

py/objtype.c

Lines changed: 5 additions & 1 deletion
10000
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,11 @@ STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value
855855
}
856856
mp_obj_class_lookup(&lookup, self->base.type);
857857
if (member[0] == MP_OBJ_SENTINEL) {
858-
return mp_obj_subscr(self->subobj[0], index, value);
858+
// CIRCUITPY-CHANGE: We pass the native subscr a copy of the original
859+
// object so it can access info about the subobject.
860+
const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
861+
mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, subscr)(self_in, index, value);
862+
return ret;
859863
} else if (member[0] != MP_OBJ_NULL) {
860864
size_t n_args = value == MP_OBJ_NULL || value == MP_OBJ_SENTINEL ? 1 : 2;
861865
mp_obj_t ret = mp_call_method_n_kw(n_args, 0, member);

tests/unix/subclass_native.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ class A(NativeBaseClass):
2626
a.test = "test set indirectly"
2727
print(".test:", a.test)
2828

29-
a._new_attribute = True
30-
print("._new_attribute", a._new_attribute)
29+
a.new_attribute = True
30+
print(".new_attribute", a.new_attribute)
3131

32-
a.print_subclass_attr("_new_attribute")
32+
a.print_subclass_attr("new_attribute")
33+
34+
print(a[0])
3335

3436

3537
class B(NativeBaseClass):
@@ -43,7 +45,8 @@ def __init__(self, suffix):
4345
b.test = "test set indirectly through b"
4446
print(".test:", b.test)
4547

46-
b._new_attribute = True
47-
print("._new_attribute", b._new_attribute)
48+
b.new_attribute = "hello"
49+
print(".new_attribute", b.new_attribute)
4850

49-
b.print_subclass_attr("_new_attribute")
51+
b.print_subclass_attr("new_attribute")
52+
print(b[0])

tests/unix/subclass_native.py.exp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ native base class .test set to: 'test set directly'
44
.test: subclass kwarg
55
native base class .test set to: 'test set indirectly'
66
.test: test set indirectly
7-
._new_attribute True
8-
native base class ._new_attribute set to: True
7+
.new_attribute True
8+
native base class .new_attribute set to: True
9+
native base class subscr .new_attribute set to: True
10+
True
911
.test: super init suffix
1012
native base class .test set to: 'test set indirectly through b'
1113
.test: test set indirectly through b
12-
._new_attribute True
13-
native base class ._new_attribute set to: True
14+
.new_attribute hello
15+
native base class .new_attribute set to: 'hello'
16+
native base class subscr .new_attribute set to: 'hello'
17+
hello

0 commit comments

Comments
 (0)
0