strange behaviour...

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

strange behaviour...

Post by gratefulfrog » Wed Aug 31, 2016 3:25 pm

Hello again,
I have run into some odd behavior.
I am trying to look up values in a dictionary, with the keys stored in a list. Each time I try this on the pyboard it fails, but the exact same sequence on micropython on linux works.

I have the following:
p.header = a list of strings, keys to the dictionary
rd a dictionary
look at this:

Code: Select all

>>> rd
{'HorizontalSelector': 0, 'VerticalSelector': 0, 'CTone': 5, 'ToneRange': 5, 'ATone': 5, 'TREM': 0, 'VIB': 0, 'S': '(|(+AB)(+CD))', 'BVol': 5, 'AUX0': 0, 'AUX1': 0, 'AVol': 5, 'CVol': 5, 'BTone': 5, 'MasterVol': 5, 'DTone': 5, 'DVol': 5, 'MasterTone': 5, 'Name': '(0,0)'}
>>> p.header
['HorizontalSelector', 'VerticalSelector', 'Name', 'MasterVol', 'MasterTone', 'AVol', 'ATone', 'BVol', 'BTone', 'CVol', 'CTone', 'DVol', 'DTone', 'ToneRange', 'S', 'TREM', 'VIB', 'AUX0', 'AUX1']
>>> rd.keys()
dict_keys(['HorizontalSelector', 'VerticalSelector', 'CTone', 'ToneRange', 'ATone', 'TREM', 'VIB', 'S', 'BVol', 'AUX0', 'AUX1', 'AVol', 'CVol', 'BTone', 'MasterVol', 'DTone', 'DVol', 'MasterTone', 'Name'])
>>> list(rd.keys())[0]
'HorizontalSelector'
>>> rd[list(rd.keys())[0]]
0   ### this is correct
>>> list(rd.keys())[0] == p.header[0]
True  ### so the value at p.header[0] is the same as  list(rd.keys())[0]
>>> rd[p.header[0]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: <value>   ### How can this be explained!??
>>> 
I am really at a loss, and this is blocking my project at the very last stages... sigh...

I am using the micropython firmware built locally (no changes to the makefile) from the github micropython repo...

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: strange behaviour...

Post by gratefulfrog » Wed Aug 31, 2016 3:53 pm

This may be related to: dict map lookup error with str key #523 ??

Any help would be urgently appreciated!

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

Re: strange behaviour...

Post by pythoncoder » Wed Aug 31, 2016 4:10 pm

Bear in mind that keys() iterates over dictionary keys in an arbitrary order. Thus

Code: Select all

list(rd.keys())[0]
will not produce anything consistent or repeatable between Python implementations or between program runs.
Peter Hinch
Index to my micropython libraries.

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: strange behaviour...

Post by gratefulfrog » Wed Aug 31, 2016 4:13 pm

I am aware of that, but I was using that to illustrate the bug...

I am now pretty much convinced this is related to the bug #523...

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

Re: strange behaviour...

Post by pythoncoder » Wed Aug 31, 2016 4:44 pm

Well that issue was fixed long ago. I can't replicate this fault here, either on the Unix build or on a Pyboard:

Code: Select all

$ ./upython 
MicroPython v1.8.1-39-gdb4addd on 2016-07-01; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> rd = {'HorizontalSelector': 0, 'VerticalSelector': 0, 'CTone': 5, 'ToneRange': 5, 'ATone': 5, 'TREM': 0, 'VIB': 0, 'S': '(|(+AB)(+CD))', 'BVol': 5, 'AUX0': 0, 'AUX1': 0, 'AVol': 5, 'CVol': 5, 'BTone': 5, 'MasterVol': 5, 'DTone': 5, 'DVol': 5, 'MasterTone': 5, 'Name': '(0,0)'}
>>> class P():
...     pass
... 
>>> p = P()
>>> p.header =['HorizontalSelector', 'VerticalSelector', 'Name', 'MasterVol', 'MasterTone', 'AVol', 'ATone', 'BVol', 'BTone', 'CVol', 'CTone', 'DVol', 'DTone', 'ToneRange', 'S', 'TREM', 'VIB', 'AUX0', 'AUX1']
>>> rd[p.header[0]]
0
Peter Hinch
Index to my micropython libraries.

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: strange behaviour...

Post by gratefulfrog » Wed Aug 31, 2016 4:50 pm

That's because the original dictionary is built in a much more complex manner. It is not simply assigned as you did in you example...

I will try to make a set of files that can be used to replicate the bug systematically and attach them, but now I've got to run...

I assure you, that there is still an issue with the way str's are used in dictionary keys... It works perfectly on my linux build of micropython, but fails consistently on the pyboard...

More soo, and as always, big thanks for you kind support!

Ciao,
Bob

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

Re: strange behaviour...

Post by pythoncoder » Thu Sep 01, 2016 9:33 am

gratefulfrog wrote:...I assure you, that there is still an issue with the way str's are used in dictionary keys...
In general strings work fine as keys. But I'm not doubting you: you've evidently hit on a special case. But until you can produce a simple, reproducible test case the developers have nothing to go on.

Producing a test case can be hard work - I've done it a few times and once or twice it's taken days to narrow down the cause - but it's worthwhile as it contributes to the quality of MicroPython. Good luck.
Peter Hinch
Index to my micropython libraries.

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: strange behaviour...

Post by gratefulfrog » Thu Sep 01, 2016 10:51 am

pythoncoder wrote: Producing a test case can be hard work
You ar right about that! I've been working on the test case fo 5 hours, and only now can I consistently produce the error in a practical way!

It seems to have something to do with strings that are loaded from frozen code vs. strings that are loaded from opening a file and reading from the sd-card, and then using either as keys in a dictionary...

I am still at it!

gratefulfrog
Posts: 149
Joined: Sun Mar 01, 2015 12:10 pm

Re: strange behaviour...

Post by gratefulfrog » Thu Sep 01, 2016 12:27 pm

I have just submitted an issue to the github repo:

Dictionaries in frozen code behave wrongly when accessed with non-frzoen string keys #2378

I also posted "minimal??" code that is need to reproduce the error.

I attach it here as well.

Let me know!

Ciao,
Bob
Attachments
FrozenStringBug.zip
code used to produce issue #2378, instruction in bug.py
(7.28 KiB) Downloaded 269 times

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

Re: strange behaviour...

Post by pythoncoder » Thu Sep 01, 2016 12:56 pm

Kudos for sheer bravery in submitting such a complicated test case ;) I await with interest the response of the dev team...

As a confirmed yellow-belly I have to confess to quietly finding a workround when confronted with a bug which only pokes its head above the parapet in a 1000 line program...
Peter Hinch
Index to my micropython libraries.

Post Reply