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
73f51a2c
Commit
73f51a2c
authored
Sep 22, 2001
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start of a test to make sure the profiler/tracer support in the core
interpreter is reporting what we expect to see.
parent
8363f48a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
110 additions
and
0 deletions
+110
-0
Lib/test/test_profilehooks.py
Lib/test/test_profilehooks.py
+110
-0
No files found.
Lib/test/test_profilehooks.py
0 → 100644
View file @
73f51a2c
import
pprint
import
sys
import
unittest
import
test_support
class
HookWatcher
:
def
__init__
(
self
):
self
.
frames
=
[]
self
.
events
=
[]
def
callback
(
self
,
frame
,
event
,
arg
):
self
.
add_event
(
event
,
frame
)
def
add_event
(
self
,
event
,
frame
=
None
):
"""Add an event to the log."""
if
frame
is
None
:
frame
=
sys
.
_getframe
(
1
)
try
:
frameno
=
self
.
frames
.
index
(
frame
)
except
ValueError
:
frameno
=
len
(
self
.
frames
)
self
.
frames
.
append
(
frame
)
self
.
events
.
append
((
frameno
,
event
,
ident
(
frame
)))
def
get_events
(
self
):
"""Remove calls to add_event()."""
add_event
=
self
.
add_event
.
im_func
.
func_code
disallowed
=
(
add_event
.
co_firstlineno
,
add_event
.
co_name
)
return
[
item
for
item
in
self
.
events
if
item
[
2
]
!=
disallowed
]
class
ProfileHookTestCase
(
unittest
.
TestCase
):
def
check_events
(
self
,
callable
,
expected
):
events
=
capture_events
(
callable
)
if
events
!=
expected
:
self
.
fail
(
"Expected events:
\
n
%s
\
n
Received events:
\
n
%s"
%
(
pprint
.
pformat
(
expected
),
pprint
.
pformat
(
events
)))
def
test_simple
(
self
):
def
f
(
p
):
pass
f_ident
=
ident
(
f
)
self
.
check_events
(
f
,
[(
0
,
'call'
,
f_ident
),
(
0
,
'return'
,
f_ident
),
])
def
test_exception
(
self
):
def
f
(
p
):
try
:
1
/
0
except
:
pass
f_ident
=
ident
(
f
)
self
.
check_events
(
f
,
[(
0
,
'call'
,
f_ident
),
(
0
,
'exception'
,
f_ident
),
(
0
,
'return'
,
f_ident
),
])
def
test_nested_exception
(
self
):
def
f
(
p
):
1
/
0
def
g
(
p
):
try
:
f
(
p
)
except
:
pass
f_ident
=
ident
(
f
)
g_ident
=
ident
(
g
)
self
.
check_events
(
g
,
[(
0
,
'call'
,
g_ident
),
(
1
,
'call'
,
f_ident
),
(
1
,
'exception'
,
f_ident
),
# This isn't what I expected:
(
0
,
'exception'
,
g_ident
),
(
0
,
'return'
,
g_ident
),
])
def
ident
(
function
):
if
hasattr
(
function
,
"f_code"
):
code
=
function
.
f_code
else
:
code
=
function
.
func_code
return
code
.
co_firstlineno
,
code
.
co_name
def
capture_events
(
callable
):
p
=
HookWatcher
()
sys
.
setprofile
(
p
.
callback
)
callable
(
p
)
sys
.
setprofile
(
None
)
return
p
.
get_events
()
def
show_events
(
callable
):
import
pprint
pprint
.
pprint
(
capture_events
(
callable
))
def
test_main
():
test_support
.
run_unittest
(
ProfileHookTestCase
)
if
__name__
==
"__main__"
:
test_main
()
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