Do I just misunderstand super() on the pyboard?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
beede
Posts: 3
Joined: Wed Jan 03, 2018 10:37 pm

Do I just misunderstand super() on the pyboard?

Post by beede » Tue Feb 06, 2018 5:06 pm

super() seems to work differently on the pyboard than on the unix port of micropython. I have a file, foo.py, which has two classes derived from bytearray. The first ("A") works on Cpython and on the unix port of micropython but not on the pyboard. The second ("B") works on the pyboard. My question is, how is maxchars begin passed to bytearray.__init__? And is this behavior a bug I should report?

Note: the leading periods in the example should be spaces, but apparently BBcode is disabled for me.

foo.py:
-----------------------------------------------------------
__ALL__ = ("A")

class A(bytearray):
...def __init__(self,maxchars):
......super().__init__(maxchars)

class B(bytearray):
...def __init__(self,maxchars=0):
......super().__init__()

w = B()
x = B(10)
-----------------------------------------------------------

Entered at REPL:

>>> import foo
>>> len(foo.w)
0
>>> len(foo.x)
10
>>> q = foo.A(3)
q = foo.A(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "foo.py", line 5, in __init__
TypeError: function takes 1 positional arguments but 2 were given

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Do I just misunderstand super() on the pyboard?

Post by pythoncoder » Wed Feb 07, 2018 7:00 am

See differences implementation differences (3.).
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Do I just misunderstand super() on the pyboard?

Post by deshipu » Wed Feb 07, 2018 8:33 am

But shouldn't the unix micropython still behave the same as the pyboard one?

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Do I just misunderstand super() on the pyboard?

Post by cefn » Wed Feb 07, 2018 8:43 am

If it were me, I would report it. The variance between pyboard and Unix in such a fundamental behaviour as argument-passing in inheritance is surprising and I suspect could either be fixed or well-documented. If the inheritance strategy of a core object is fully invalid in Micropython because optimisations prevent it from behaving as expected, perhaps it should fail. The note in 3. wouldn't suggest to me that this would take place.

beede
Posts: 3
Joined: Wed Jan 03, 2018 10:37 pm

Re: Do I just misunderstand super() on the pyboard?

Post by beede » Wed Feb 07, 2018 9:32 pm

Thanks for the thoughts--I will put together a bug report and submit it.

beede
Posts: 3
Joined: Wed Jan 03, 2018 10:37 pm

Re: Do I just misunderstand super() on the pyboard?

Post by beede » Wed Feb 07, 2018 9:43 pm

Okay, I checked the bug reports (which I should have done first) and find that it was already reported and closed without explanation. The fact that it's different on the pyboard and the unix port suggests to me that it is a bug, but I can work around it now that I know it's there.

The earlier bug report is at https://github.com/micropython/micropython/issues/3554 .

Thanks again for the responses.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Do I just misunderstand super() on the pyboard?

Post by pythoncoder » Thu Feb 08, 2018 9:26 am

In my opinion if you attempt to use an unsupported feature all bets are off as to behaviour.

However subclassing built-in types is a widely used technique. So this issue regularly crops up. I think attempts to instantiate such a class should throw a runtime exception. Perhaps an issue should be raised to that effect?
Peter Hinch
Index to my micropython libraries.

Post Reply