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.
5try:
6    from collections import namedtuple
7except ImportError:
8    try:
9        from ucollections import namedtuple
10    except ImportError:
11        print("SKIP")
12        raise SystemExit
13
14_DefragResultBase = namedtuple('DefragResult', [ 'foo', 'bar' ])
15
16class _ResultMixinStr(object):
17    def encode(self):
18        return self._encoded_counterpart(*(x.encode() for x in self))
19
20class _ResultMixinBytes(object):
21    def decode(self):
22        return self._decoded_counterpart(*(x.decode() for x in self))
23
24class DefragResult(_DefragResultBase, _ResultMixinStr):
25    pass
26
27class DefragResultBytes(_DefragResultBase, _ResultMixinBytes):
28    pass
29
30
31DefragResult._encoded_counterpart = DefragResultBytes
32DefragResultBytes._decoded_counterpart = DefragResult
33
34# Due to differences in type and native subclass printing,
35# the best thing we can do here is to just test that no exceptions
36# happen
37
38#print(DefragResult, DefragResult._encoded_counterpart)
39#print(DefragResultBytes, DefragResultBytes._decoded_counterpart)
40
41o1 = DefragResult("a", "b")
42#print(o1, type(o1))
43o2 = DefragResultBytes("a", "b")
44#print(o2, type(o2))
45
46#print(o1._encoded_counterpart)
47_o1 = o1.encode()
48print(_o1[0], _o1[1])
49#print(_o1, type(_o1))
50
51print("All's ok")
52