Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix: Unexpected behaviour in nested for loops #3157

Closed
peterhinch opened this issue Jun 21, 2017 · 4 comments
Closed

Unix: Unexpected behaviour in nested for loops #3157

peterhinch opened this issue Jun 21, 2017 · 4 comments

Comments

@peterhinch
Copy link
Contributor

This arose in this forum post

for foo in range(10):
  print(foo)
  for bar in range(10):
    pass
  else:
    continue
  break

Outcome under CPython (prints 0-9) and MicroPython (prints 0 only)

[adminpete@axolotl]: ~
$ upython 
MicroPython v1.8.7-797-ge5e49be-dirty on 2017-06-05; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats26
0
>>> 
[adminpete@axolotl]: ~
$ python3.5
Python 3.5.2 (default, Dec  2 2016, 15:29:25) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats26
0
1
2
3
4
5
6
7
8
9
>>> 
@dpgeorge
Copy link
Member

Thanks for the report, I can confirm that it's a bug with the optimised for loop. For now, to work around it force the compiler to not generate optimised loops by using an explicit iterator:

xrange = range
for foo in range(10):
  print(foo)
  for bar in xrange(10):
    pass
  else:
    continue
  break

@pfalcon
Copy link
Contributor

pfalcon commented Jun 21, 2017

@dpgeorge : My understanding is that it's incorrect handling of "continue" scoping in for's "else" clause: it no longer applies to that "for", but to the enclosing "for".

@dpgeorge
Copy link
Member

Should be fixed by 4c5f108. Tests added in 4492293

@dpgeorge
Copy link
Member

My understanding is that it's incorrect handling of "continue" scoping in for's "else" clause: it no longer applies to that "for", but to the enclosing "for".

No, the else is handled correctly. It was an issue with values remaining on the Python stack when they should have been popped.

tannewt added a commit to tannewt/circuitpython that referenced this issue Jul 18, 2020
…scoutmakes-azul

Addition of Tinkeringtech ScoutMakes Azul nRF52840 based platform to CircuitPython
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants