adafruit/circup

UnboundLocalError: local variable 'loc' referenced before assignment

bablokb opened this issue · 4 comments

Running circup fails because a variable is not assigned. Seems like an issue with the upcoming change in the mpy-format. In the logic before line 536 an additional else is missing.

circup --path src install asyncio
Found device at src, running CircuitPython 7.3.0.
Searching for dependencies for: ['asyncio']
Traceback (most recent call last):
...
File "/usr/lib/python3.6/site-packages/circup/__init__.py", line 536, in extract_metadata
  if loc > -1:
UnboundLocalError: local variable 'loc' referenced before assignment

Hm, I just had a look at the mpy-files and noticed that they are not in binary format but only pretend to be. So this seems also like a packaging error of the bundle-files and therefore circup can't detect the magic-bytes at the beginning of the files.

Nevertheless the code does not catch this issue correctly.

Yes, it's a bug with the bundle, and an issue with circup not correctly reporting the error.
I did look at the circup bug, but that requires quite a bit of work to make sure that the error is reported in the right place.

FYI the bug with the bundle is fixed (the Adafruit Bundle's latest release is good, the Community Bundle should be updated tonight).
The problem with circup crashing on bad mpy files is still there though, so that issue should remain open.

I ran into this too yesterday. mpy files that were not inside directories shipped as raw Python code, so the version information was undetectable, so the variable loc was undefined.

A suggested fix would be to wrap the section linked above in a try/except, where the except clause is just `print("Can't find version information in current MPY file being examined.")

If that seems sensible, I could write a patch.