From 0d5298475de40c36f591b2a57d8843082485ef1c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 23 Nov 2018 15:01:35 -0800 Subject: [PATCH] Fix `raises(..., "code(string)")` frame filename. --- changelog/4435.bugfix.rst | 1 + src/_pytest/python_api.py | 2 +- testing/python/raises.py | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 changelog/4435.bugfix.rst diff --git a/changelog/4435.bugfix.rst b/changelog/4435.bugfix.rst new file mode 100644 index 000000000..de60b5e62 --- /dev/null +++ b/changelog/4435.bugfix.rst @@ -0,0 +1 @@ +Fix ``raises(..., 'code(string)')`` frame filename. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 805cd85ad..7de8e154c 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -679,7 +679,7 @@ def raises(expected_exception, *args, **kwargs): loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: - code = _pytest._code.Source(code).compile() + code = _pytest._code.Source(code).compile(_genframe=frame) six.exec_(code, frame.f_globals, loc) # XXX didn't mean f_globals == f_locals something special? # this is destroyed here ... diff --git a/testing/python/raises.py b/testing/python/raises.py index 130b196ac..6ca19c677 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -17,6 +17,10 @@ class TestRaises(object): def test_raises_exec(self): pytest.raises(ValueError, "a,x = []") + def test_raises_exec_correct_filename(self): + excinfo = pytest.raises(ValueError, 'int("s")') + assert __file__ in excinfo.traceback[-1].path + def test_raises_syntax_error(self): pytest.raises(SyntaxError, "qwe qwe qwe")