bpo-32468: Better frame repr() (#5067)

bpo-32468: Better frame repr()
This commit is contained in:
Antoine Pitrou 2017-12-31 22:35:22 +01:00 committed by GitHub
parent 0a37a30037
commit 14709144b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 1 deletions

View file

@ -1,3 +1,4 @@
import re
import types
import unittest
import weakref
@ -159,5 +160,45 @@ class FrameLocalsTest(unittest.TestCase):
self.assertEqual(inner.f_locals, {})
class ReprTest(unittest.TestCase):
"""
Tests for repr(frame).
"""
def test_repr(self):
def outer():
x = 5
y = 6
def inner():
z = x + 2
1/0
t = 9
return inner()
offset = outer.__code__.co_firstlineno
try:
outer()
except ZeroDivisionError as e:
tb = e.__traceback__
frames = []
while tb:
frames.append(tb.tb_frame)
tb = tb.tb_next
else:
self.fail("should have raised")
f_this, f_outer, f_inner = frames
file_repr = re.escape(repr(__file__))
self.assertRegex(repr(f_this),
r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code test_repr>$"
% (file_repr, offset + 23))
self.assertRegex(repr(f_outer),
r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code outer>$"
% (file_repr, offset + 7))
self.assertRegex(repr(f_inner),
r"^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code inner>$"
% (file_repr, offset + 5))
if __name__ == "__main__":
unittest.main()