codecombat / aether

Lint, analyze, normalize, transform, sandbox, run, step through, and visualize user JavaScript, in node or the browser.
http://aetherjs.com
MIT License
191 stars 53 forks source link

Python list comprehensions give error when reusing comprehension variable name #133

Closed nemoyatpeace closed 8 years ago

nemoyatpeace commented 9 years ago
loop:
    items = [i for i in self.findItems() if i.pos.x<38]
    item = self.findNearest(items)
    if item:
        self.moveXY(item.pos.x, item.pos.y)
    else:
        items = [i for i in self.findItems() if i.pos.x>38 and i.pos.y>33]
        item = self.findNearest(items)
        self.moveXY(item.pos.x, item.pos.y)

This code gives a strange error. When the second items = is called it ends up as a list ['next','throw']. I tested with loop:

for k in items:
    self.say(String(k))

When I changed the code to the following it runs properly.

loop:
    top = [i for i in self.findItems() if i.pos.x>38 and i.pos.y>33]
    left = [i for i in self.findItems() if i.pos.x<38]
    if self.now()<52:
        items = left
    else:
        items = top
    item = self.findNearest(items)
    if item:
        self.moveXY(item.pos.x, item.pos.y)

Apparently having the comprehension inside the if caused problems. Not sure if the if is the problem or the fact that it is indented twice.

nwinter commented 8 years ago

When I try this now, I don't see the problem with the list being ['next', 'throw'], but I do see it throwing this error if I re-use the same list comprehension local variable:

screenshot 2016-05-22 08 35 47

When I name it i2, it works.

differentmatt commented 8 years ago

Should be fixed after next production deploy. Our Python parser doesn't support strict mode, which was recently turned on. https://github.com/codecombat/aether/commit/3f124071ad5b3e51564ec1a7368ad784e94e3145