-
-
Notifications
You must be signed in to change notification settings - Fork 32k
cProfile does not work in pdb #102249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Apparently the problem is with settrace: Python 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def fun(frame, event, arg):
... import cProfile
... cProfile.run("print(0)")
... sys.settrace(None)
...
>>> import sys
>>> sys.settrace(fun)
>>> print(1)
0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in fun
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/cProfile.py", line 16, in run
return _pyprofile._Utils(Profile).run(statement, filename, sort)
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/profile.py", line 57, in run
self._show(prof, filename, sort)
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/profile.py", line 72, in _show
prof.print_stats(sort)
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/cProfile.py", line 42, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pstats.py", line 114, in __init__
self.init(arg)
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pstats.py", line 128, in init
self.load_stats(arg)
File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pstats.py", line 154, in load_stats
raise TypeError("Cannot create or construct a %r object from %r"
TypeError: Cannot create or construct a <class 'pstats.Stats'> object from <cProfile.Profile object at 0x1029d6980>
>>> |
The problem is that the profiling function (set by Python 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> def fun_prof(frame, event, arg):
... print("profiling")
...
>>> def fun_trace(frame, event, arg):
... print("tracing")
... sys.setprofile(fun_prof)
... print("Call func")
... sys.setprofile(None)
... sys.settrace(None)
...
>>> sys.settrace(fun_trace)
>>> print(0)
tracing
Call func
0
>>> |
I can use
|
@iritkatriel @gvanrossum as more-than-once committers into pdb and cProfile correspondingly still active. |
I'm sorry, I no longer have a working knowledge of these APIs. Historically, it never occurred to me that these APIs would be combined. |
FWIW, I can repro this on 3.11.0 on Windows: Python 3.11.0 | packaged by conda-forge | (main, Oct 25 2022, 06:12:32) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> db = pdb.Pdb()
>>> db.run("aaa")
> <string>(1)<module>()
(Pdb) import profile, cProfile
(Pdb) profile.run("print(0)")
0
1 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 profile:0(print(0))
0 0.000 0.000 profile:0(profiler)
(Pdb) cProfile.run("print(0)")
0
*** TypeError: Cannot create or construct a <class 'pstats.Stats'> object from <cProfile.Profile object at 0x0000000003814C20>
(Pdb) |
I guess the best way to address this would be to update the doc so it is clear that If I can add a small feature request: |
I think the rule is - you can't do |
This would probably best as a separate issue, IMO, since it doesn't appear to be directly
Is this documented anywhere? It might be worth adding a small note in the docs of the respective functions. |
From what I can see, the rule is that if you need to call |
In that case, perhaps we should add a note to that effect. Are you interested in submitting a PR? Happy to help guide you in that since I'm one of the docs team members here. |
I opened #102806 |
Co-authored-by: C.A.M. Gerlach <[email protected]>
(cherry picked from commit 2445673) Co-authored-by: Quentin Peter <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]>
(cherry picked from commit 2445673) Co-authored-by: Quentin Peter <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]>
…111557) Co-authored-by: Quentin Peter <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]>
…111558) Co-authored-by: Quentin Peter <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]>
Thanks! |
Co-authored-by: C.A.M. Gerlach <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]>
Bug report
While debugging, cProfile fails to work, while profile does work, see session below:
Your environment
Linked PRs
The text was updated successfully, but these errors were encountered: