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

Fix interactive debugger while accessing cell vars #1532

Merged
merged 2 commits into from May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -15,6 +15,9 @@ Unreleased
cookies with names such as "expires" and "version". (:issue:`1495`)
- Add ``charset=utf-8`` to an HTTP exception response's
``CONTENT_TYPE`` header. (:pr:`1526`)
- The interactive debugger handles outer variables in nested scopes
such as lambdas and comprehensions. :issue:`913`, :issue:`1037`,
:pr:`1532`


Version 0.15.5
Expand Down
11 changes: 6 additions & 5 deletions src/werkzeug/debug/console.py
Expand Up @@ -148,11 +148,12 @@ def func(source, filename, symbol):

class _InteractiveConsole(code.InteractiveInterpreter):
def __init__(self, globals, locals):
locals = dict(globals)
locals.update(locals)
locals["dump"] = dump
locals["help"] = helper
locals["__loader__"] = self.loader = _ConsoleLoader()
code.InteractiveInterpreter.__init__(self, locals)
self.globals = dict(globals)
self.globals["dump"] = dump
self.globals["help"] = helper
self.globals["__loader__"] = self.loader = _ConsoleLoader()
self.more = False
self.buffer = []
_wrap_compiler(self)
Expand All @@ -177,7 +178,7 @@ def runsource(self, source):

def runcode(self, code):
try:
eval(code, self.globals, self.locals)
exec(code, self.locals)
except Exception:
self.showtraceback()

Expand Down
4 changes: 2 additions & 2 deletions src/werkzeug/debug/tbtools.py
Expand Up @@ -464,8 +464,8 @@ def __init__(self, exc_type, exc_value, tb):
if os.path.isfile(fn):
fn = os.path.realpath(fn)
self.filename = to_unicode(fn, get_filesystem_encoding())
self.module = self.globals.get("__name__")
self.loader = self.globals.get("__loader__")
self.module = self.globals.get("__name__", self.locals.get("__name__"))
self.loader = self.globals.get("__loader__", self.locals.get("__loader__"))
self.code = tb.tb_frame.f_code

# support for paste's traceback extensions
Expand Down
12 changes: 12 additions & 0 deletions tests/test_debug.py
Expand Up @@ -16,6 +16,7 @@
import requests

from werkzeug._compat import PY2
from werkzeug.debug import console
from werkzeug.debug import DebuggedApplication
from werkzeug.debug import get_machine_id
from werkzeug.debug.console import HTMLStringO
Expand Down Expand Up @@ -356,6 +357,17 @@ def app(environ, start_response):
assert r.text == "hello"


def test_console_closure_variables(monkeypatch):
# restore the original display hook
monkeypatch.setattr(sys, "displayhook", console._displayhook)
c = console.Console()
c.eval("y = 5")
c.eval("x = lambda: y")
ret = c.eval("x()")
expected = ">>> x()\n5" if PY2 else ">>> x()\n5\n"
assert ret == expected


@pytest.mark.skipif(PY2, reason="Python 2 doesn't have chained exceptions.")
@pytest.mark.timeout(2)
def test_chained_exception_cycle():
Expand Down