Skip to content

Commit e0df7b1

Browse files
authored
Merge pull request #7 from bcmi-labs/mp_fix
ucloud: Fix set/get attr compatibility issue with CPython/MicroPython.
2 parents ee3a75c + 6d59644 commit e0df7b1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

arduino_iot_cloud/ucloud.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,28 @@ def value(self, value):
112112
)
113113
self._value = value
114114

115+
def __is_subrecord(self, attr):
116+
return (hasattr(super(), '__dict__')
117+
and isinstance(super().__dict__.get("_value", None), dict)
118+
and attr in super().value)
119+
115120
def __getattr__(self, attr):
116-
if isinstance(getattr(super(), "_value", None), dict) and attr in super().value:
121+
if self.__is_subrecord(attr):
117122
return super().value[attr].value
118123
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")
119124

120-
def __setattr__(self, name, value):
121-
if isinstance(getattr(super(), "_value", None), dict) and name in super().value:
122-
self.value[name].value = value
125+
def __setattr__(self, attr, value):
126+
if self.__is_subrecord(attr):
127+
self.value[attr].value = value
123128
else:
124-
super().__setattr__(name, value)
129+
super().__setattr__(attr, value)
125130

126131
def _build_rec_dict(self, naming_map, appendTo):
127132
if isinstance(self.value, dict):
128133
for r in self.value.values():
129134
if r.value is not None: # NOTE: should filter by updated when it's supported.
130135
r._build_rec_dict(naming_map, appendTo)
131-
else:
136+
elif self._value is not None:
132137
super()._build_rec_dict(naming_map, appendTo)
133138

134139
def add_to_pack(self, pack):

0 commit comments

Comments
 (0)