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

NameError when using lambda function in a list comprehension in the interactive debugger under Python 3 #1037

Closed
alexwlchan opened this issue Nov 11, 2016 · 11 comments

Comments

Projects
None yet
6 participants
@alexwlchan
Copy link
Contributor

commented Nov 11, 2016

I’ve encountered a weird edge case around lambda functions in list comprehensions in the interactive debugger. I set up a function in the debugger as follows:

>>> f = lambda x: x
>>> f
<function <lambda> at 0x1068752a8>
>>> f(1)
1
>>> [f(x) for x in (1, 2, 3)]

If I run this code in Python 2.7.10, the output is exactly what you’d expected:

>>> [f(x) for x in (1, 2, 3)]
[1, 2, 3]

But if I run the same line in Python 3.5.0, I get a NameError:

>>> [f(x) for x in (1, 2, 3)]
Traceback (most recent call last):
  File "<debugger>", line 1, in <module>
    [f(x) for x in (1, 2, 3)]
 File "<debugger>", line 1, in <listcomp>
    [f(x) for x in (1, 2, 3)]
NameError: name 'f' is not defined

I’m running Werkzeug 0.11.11 on macOS 10.11.6. Each time I’m using a clean virtualenv, and using a minimal Flask app that drops straight into the interactive debugger.

I can reproduce the issue if I install from master at f34b02b.

I have a few other issues with defining functions in Python 3 in the interactive debugger, which might be related – I’ll raise as a separate issue.

@alexpantyukhin

This comment has been minimized.

Copy link
Contributor

commented Nov 12, 2016

I tried this usecase for python 2.7 and python 3.4 and reproduced the issue.

@untitaker

This comment has been minimized.

Copy link
Member

commented Nov 12, 2016

This may be a bug in InteractiveInterpreter from the stdlib. Could you try with that? (I think you're supposed to instantiate that and call runsource)

@alexpantyukhin

This comment has been minimized.

Copy link
Contributor

commented Nov 12, 2016

I tried InteractiveConsole .
It has a correct behavior.

import code
c = code.InteractiveConsole()
c.push('f=lambda x: x')
c.push('[f(x) for x in (1, 2, 3)]')
@alexpantyukhin

This comment has been minimized.

Copy link
Contributor

commented Nov 12, 2016

Also the problem is wider. The problem appears also when use external variables in func. Like there:

>>> f = 1
>>> g = lambda x: f
>>> g(3)
@alexwlchan

This comment has been minimized.

Copy link
Contributor Author

commented Nov 12, 2016

Like @alexpantyukhin, I can’t seem to reproduce this problem with InteractiveInterpreter:

>>> import code
>>> c = code.InteractiveInterpreter()
>>> c.runsource('f = lambda x: x')
False
>>> c.runsource('[f(x) for x in (1, 2, 3)]')
[1, 2, 3]
False

I’ve had a stab at reproducing it with the Werkzeug classes, but I’m probably not calling them properly – no repro there either.

>>> from werkzeug.debug.console import _local, _InteractiveConsole
>>> c = _InteractiveConsole({}, {})
>>> _local._current_ipy = c
>>> c.runsource('f = lambda x: x')
>>> c.runsource('[f(1)]')
>>> c.runsource('[f(x) for x in (1, 2, 3)]')
@alexpantyukhin

This comment has been minimized.

Copy link
Contributor

commented Nov 12, 2016

I've made a PR. It seems it fixes the problem. Can you help me to check it?

@xeor

This comment has been minimized.

Copy link

commented Dec 20, 2018

Any news here?

x = lambda x: x + 1
[x(i) for i in range(10)]

still an issue.. Using Werkzeug==0.14.1

@xeor

This comment has been minimized.

Copy link

commented Dec 20, 2018

@alexpantyukhin where is your pr? Can't find it? Did you refeer this issue?

@untitaker

This comment has been minimized.

Copy link
Member

commented Dec 20, 2018

It's #1038 but I don't think it was a complete solution IIRC. I'd be happy to merge something that solves this issue but doesn't introduce new ones (see code comments on that PR, no idea why they're collapsed)

@alexwlchan

This comment has been minimized.

Copy link
Contributor Author

commented May 20, 2019

@asottile Thanks for fixing this!

I filed this for completeness rather than expecting a fix, because it felt like the sort of very weird and obscure bug that maintainers might like to know about but was unlikely to be high on the priority list. Nice to see it finally be closed. 😁

@asottile

This comment has been minimized.

Copy link
Contributor

commented May 20, 2019

no problem! I've spent a lot of time peering into debugger internals for pytest -- figured I could help out with this during a pycon sprint :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.