class_store_class.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Inspired by urlparse.py from CPython 3.3 stdlib
  2. # There was a bug in MicroPython that under some conditions class stored
  3. # in instance attribute later was returned "bound" as if it was a method,
  4. # which caused class constructor to receive extra argument.
  5. try:
  6. from collections import namedtuple
  7. except ImportError:
  8. try:
  9. from ucollections import namedtuple
  10. except ImportError:
  11. print("SKIP")
  12. raise SystemExit
  13. _DefragResultBase = namedtuple('DefragResult', [ 'foo', 'bar' ])
  14. class _ResultMixinStr(object):
  15. def encode(self):
  16. return self._encoded_counterpart(*(x.encode() for x in self))
  17. class _ResultMixinBytes(object):
  18. def decode(self):
  19. return self._decoded_counterpart(*(x.decode() for x in self))
  20. class DefragResult(_DefragResultBase, _ResultMixinStr):
  21. pass
  22. class DefragResultBytes(_DefragResultBase, _ResultMixinBytes):
  23. pass
  24. DefragResult._encoded_counterpart = DefragResultBytes
  25. DefragResultBytes._decoded_counterpart = DefragResult
  26. # Due to differences in type and native subclass printing,
  27. # the best thing we can do here is to just test that no exceptions
  28. # happen
  29. #print(DefragResult, DefragResult._encoded_counterpart)
  30. #print(DefragResultBytes, DefragResultBytes._decoded_counterpart)
  31. o1 = DefragResult("a", "b")
  32. #print(o1, type(o1))
  33. o2 = DefragResultBytes("a", "b")
  34. #print(o2, type(o2))
  35. #print(o1._encoded_counterpart)
  36. _o1 = o1.encode()
  37. print(_o1[0], _o1[1])
  38. #print(_o1, type(_o1))
  39. print("All's ok")