mirror of https://github.com/python/cpython
parent
0a37a30037
commit
14709144b5
|
@ -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()
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Improve frame repr() to mention filename, code name and current line number.
|
|
@ -547,6 +547,15 @@ frame_sizeof(PyFrameObject *f)
|
|||
PyDoc_STRVAR(sizeof__doc__,
|
||||
"F.__sizeof__() -> size of F in memory, in bytes");
|
||||
|
||||
static PyObject *
|
||||
frame_repr(PyFrameObject *f)
|
||||
{
|
||||
int lineno = PyFrame_GetLineNumber(f);
|
||||
return PyUnicode_FromFormat(
|
||||
"<frame at %p, file %R, line %d, code %S>",
|
||||
f, f->f_code->co_filename, lineno, f->f_code->co_name);
|
||||
}
|
||||
|
||||
static PyMethodDef frame_methods[] = {
|
||||
{"clear", (PyCFunction)frame_clear, METH_NOARGS,
|
||||
clear__doc__},
|
||||
|
@ -565,7 +574,7 @@ PyTypeObject PyFrame_Type = {
|
|||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
(reprfunc)frame_repr, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
|
|
Loading…
Reference in New Issue