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
e1bd7329
Commit
e1bd7329
authored
Sep 26, 2001
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More test cases, including something that simulates what the profiler
probably *should* be doing.
parent
696b740f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
5 deletions
+66
-5
Lib/test/test_profilehooks.py
Lib/test/test_profilehooks.py
+66
-5
No files found.
Lib/test/test_profilehooks.py
View file @
e1bd7329
...
...
@@ -34,14 +34,48 @@ class HookWatcher:
return
[
item
for
item
in
self
.
events
if
item
[
2
]
not
in
disallowed
]
class
ProfileHookTestCase
(
unittest
.
TestCase
):
class
ProfileSimulator
(
HookWatcher
):
def
__init__
(
self
):
self
.
stack
=
[]
HookWatcher
.
__init__
(
self
)
def
callback
(
self
,
frame
,
event
,
arg
):
self
.
dispatch
[
event
](
self
,
frame
)
def
trace_call
(
self
,
frame
):
self
.
add_event
(
'call'
,
frame
)
self
.
stack
.
append
(
frame
)
def
trace_return
(
self
,
frame
):
self
.
add_event
(
'return'
,
frame
)
self
.
stack
.
pop
()
def
trace_exception
(
self
,
frame
):
if
len
(
self
.
stack
)
>=
2
and
frame
is
self
.
stack
[
-
2
]:
self
.
add_event
(
'propogate-from'
,
self
.
stack
[
-
1
])
self
.
stack
.
pop
()
else
:
self
.
add_event
(
'ignore'
,
frame
)
dispatch
=
{
'call'
:
trace_call
,
'exception'
:
trace_exception
,
'return'
:
trace_return
,
}
class
TestCaseBase
(
unittest
.
TestCase
):
def
check_events
(
self
,
callable
,
expected
):
events
=
capture_events
(
callable
)
events
=
capture_events
(
callable
,
self
.
new_watcher
()
)
if
events
!=
expected
:
self
.
fail
(
"Expected events:
\
n
%s
\
n
Received events:
\
n
%s"
%
(
pprint
.
pformat
(
expected
),
pprint
.
pformat
(
events
)))
class
ProfileHookTestCase
(
TestCaseBase
):
def
new_watcher
(
self
):
return
HookWatcher
()
def
test_simple
(
self
):
def
f
(
p
):
pass
...
...
@@ -159,6 +193,28 @@ class ProfileHookTestCase(unittest.TestCase):
])
class
ProfileSimulatorTestCase
(
TestCaseBase
):
def
new_watcher
(
self
):
return
ProfileSimulator
()
def
test_simple
(
self
):
def
f
(
p
):
pass
f_ident
=
ident
(
f
)
self
.
check_events
(
f
,
[(
1
,
'call'
,
f_ident
),
(
1
,
'return'
,
f_ident
),
])
def
test_basic_exception
(
self
):
def
f
(
p
):
1
/
0
f_ident
=
ident
(
f
)
self
.
check_events
(
f
,
[(
1
,
'call'
,
f_ident
),
(
1
,
'ignore'
,
f_ident
),
(
1
,
'propogate-from'
,
f_ident
),
])
def
ident
(
function
):
if
hasattr
(
function
,
"f_code"
):
code
=
function
.
f_code
...
...
@@ -174,8 +230,9 @@ def protect(f, p):
protect_ident
=
ident
(
protect
)
def
capture_events
(
callable
):
p
=
HookWatcher
()
def
capture_events
(
callable
,
p
=
None
):
if
p
is
None
:
p
=
HookWatcher
()
sys
.
setprofile
(
p
.
callback
)
protect
(
callable
,
p
)
sys
.
setprofile
(
None
)
...
...
@@ -188,7 +245,11 @@ def show_events(callable):
def
test_main
():
test_support
.
run_unittest
(
ProfileHookTestCase
)
loader
=
unittest
.
TestLoader
()
suite
=
unittest
.
TestSuite
()
suite
.
addTest
(
loader
.
loadTestsFromTestCase
(
ProfileHookTestCase
))
suite
.
addTest
(
loader
.
loadTestsFromTestCase
(
ProfileSimulatorTestCase
))
test_support
.
run_suite
(
suite
)
if
__name__
==
"__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