Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
14709144
Commit
14709144
authored
Dec 31, 2017
by
Antoine Pitrou
Committed by
GitHub
Dec 31, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32468: Better frame repr() (#5067)
bpo-32468: Better frame repr()
parent
0a37a300
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
1 deletion
+52
-1
Lib/test/test_frame.py
Lib/test/test_frame.py
+41
-0
Misc/NEWS.d/next/Library/2017-12-31-20-32-58.bpo-32468.YBs__0.rst
...S.d/next/Library/2017-12-31-20-32-58.bpo-32468.YBs__0.rst
+1
-0
Objects/frameobject.c
Objects/frameobject.c
+10
-1
No files found.
Lib/test/test_frame.py
View file @
14709144
import
re
import
types
import
types
import
unittest
import
unittest
import
weakref
import
weakref
...
@@ -159,5 +160,45 @@ class FrameLocalsTest(unittest.TestCase):
...
@@ -159,5 +160,45 @@ class FrameLocalsTest(unittest.TestCase):
self
.
assertEqual
(
inner
.
f_locals
,
{})
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__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
Misc/NEWS.d/next/Library/2017-12-31-20-32-58.bpo-32468.YBs__0.rst
0 → 100644
View file @
14709144
Improve frame repr() to mention filename, code name and current line number.
Objects/frameobject.c
View file @
14709144
...
@@ -547,6 +547,15 @@ frame_sizeof(PyFrameObject *f)
...
@@ -547,6 +547,15 @@ frame_sizeof(PyFrameObject *f)
PyDoc_STRVAR
(
sizeof__doc__
,
PyDoc_STRVAR
(
sizeof__doc__
,
"F.__sizeof__() -> size of F in memory, in bytes"
);
"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
[]
=
{
static
PyMethodDef
frame_methods
[]
=
{
{
"clear"
,
(
PyCFunction
)
frame_clear
,
METH_NOARGS
,
{
"clear"
,
(
PyCFunction
)
frame_clear
,
METH_NOARGS
,
clear__doc__
},
clear__doc__
},
...
@@ -565,7 +574,7 @@ PyTypeObject PyFrame_Type = {
...
@@ -565,7 +574,7 @@ PyTypeObject PyFrame_Type = {
0
,
/* tp_getattr */
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_setattr */
0
,
/* tp_reserved */
0
,
/* tp_reserved */
0
,
/* tp_repr */
(
reprfunc
)
frame_repr
,
/* tp_repr */
0
,
/* tp_as_number */
0
,
/* tp_as_number */
0
,
/* tp_as_sequence */
0
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
0
,
/* tp_as_mapping */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment