Issue 21862: cProfile command-line should accept "-m module_name" as an alternative to script path (original) (raw)

Created on 2014-06-24 20:06 by pitrou, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
cProfile-add-new-option-module.patch rock,2014-06-24 21:30 patch review
21862.patch gennad,2014-07-25 22:08 review
cProfile_module_option.patch oquanox,2015-01-17 20:00 patch review
cProfile_module_option_2.patch oquanox,2015-01-27 23:21 patch review
Pull Requests
URL Status Linked Edit
PR 4297 merged CuriousLearner,2017-11-06 19:42
Messages (20)
msg221488 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-24 20:06
As the title says. You should be able to type: $ python -m cProfile -m my.module.name to profile execution of my.module.name.
msg221502 - (view) Author: Rock Lee (rock) Date: 2014-06-24 21:30
I tweaked the Lib/cProfile.py a little bit to get the feature done, please review the patch attached.
msg221546 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-25 13:13
That's not how -m should work. It should use the runpy facilities (see the runpy module). Otherwise it probably won't work properly with qualified module names ("-m pkg.subpkg.mod").
msg224008 - (view) Author: Gennadiy Zlobin (gennad) * Date: 2014-07-25 22:08
I added runpy import mechanism
msg232932 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2014-12-19 14:07
The patch does not seem to allow parameters after the -m option. I'm sure this restriction can be lifted.
msg234195 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-01-17 20:00
Now allows parameters after the -m option.
msg234560 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2015-01-23 15:44
This change will cause the module to be imported twice: progname = runpy.run_module(args[0])['__file__'] ... and then the runctx() call. What about something like: code = "runpy.run_module(modname, run_name='__main__')" globs = { 'runpy': runpy, 'modname': args[0] }
msg234614 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2015-01-24 13:22
I haven't looked into the details of how cProfile handles command line arguments, but potentially relevant is issue #19982, which proposes adding a "target" parameter that lets runpy run in the context of an existing module object, rather than always creating its own. However, if this can be implemented without that, go ahead - it can always be refactored later.
msg234863 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-01-27 23:21
Updated the patch and added docs and tests.
msg238195 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-03-16 11:41
Could someone please review the patch. Thanks.
msg305666 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-06 18:13
Hey, seems like this bug is not updated for a while. Can I work on this?
msg305667 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-06 18:17
Sanyam, you definitely can. Step #1 would probably be to port the patch to git master and turn it into a GitHub PR.
msg305668 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-06 18:22
Thanks for the heads up! I'll do that ;)
msg305714 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 07:48
Interesting - I'd never looked at how cProfile works before, and the fact it already doesn't support the "-i" (interactive) switch makes it much simpler to handle than the pdb case. So from a runpy perspective, this approach gets a +1 from me, but I'd prefer if there was someone more familiar with the cProfile module that was willing to handle the merge.
msg305720 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-07 08:29
Le 07/11/2017 à 08:48, Nick Coghlan a écrit : > > So from a runpy perspective, this approach gets a +1 from me, but I'd prefer if there was someone more familiar with the cProfile module that was willing to handle the merge. Thanks Nick. Can you confirm the runpy invocation in the PR is sane?
msg305741 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 12:07
I added an inline comment on the PR - I think what's there now would work fine, but I also suggested a slightly shorter and clearer (at least to me) alternative.
msg305742 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 12:08
Also, based on reviewing this, I suspect the same approach would also work for the pure Python profile module.
msg305819 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-08 10:51
New changeset 7973e279a21999f134aff92dd6d344ec4591fae9 by Antoine Pitrou (Sanyam Khurana) in branch 'master': bpo-21862: Add -m option to cProfile for profiling modules (#4297) https://github.com/python/cpython/commit/7973e279a21999f134aff92dd6d344ec4591fae9
msg305820 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-08 10:51
This is done. Thank you Sanyam!
msg305822 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-08 11:21
Thanks a lot Antoine and Nick :)
History
Date User Action Args
2022-04-11 14:58:05 admin set github: 66061
2018-01-05 07:34:30 ncoghlan link issue9325 dependencies
2017-11-08 11:21:16 CuriousLearner set messages: +
2017-11-08 10:51:30 pitrou set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2017-11-08 10:51:02 pitrou set messages: +
2017-11-07 12:08:20 ncoghlan set messages: +
2017-11-07 12:07:22 ncoghlan set messages: +
2017-11-07 08:29:07 pitrou set messages: +
2017-11-07 07:48:46 ncoghlan set messages: +
2017-11-06 19:42:34 CuriousLearner set pull_requests: + <pull%5Frequest4259>
2017-11-06 18:22:10 CuriousLearner set messages: +
2017-11-06 18:17:19 pitrou set messages: +
2017-11-06 18:13:47 CuriousLearner set nosy: + CuriousLearnermessages: +
2017-11-06 14:41:02 pitrou set versions: + Python 3.7, - Python 3.5
2015-03-16 11:41:08 oquanox set messages: +
2015-01-27 23:21:11 oquanox set files: + cProfile_module_option_2.patchmessages: +
2015-01-24 13:22:28 ncoghlan set messages: +
2015-01-23 15:44:48 amaury.forgeotdarc set messages: +
2015-01-22 22:21:33 berker.peksag set nosy: + berker.peksagstage: needs patch -> patch review
2015-01-17 20:00:29 oquanox set files: + cProfile_module_option.patchnosy: + oquanoxmessages: +
2014-12-19 14:07:54 amaury.forgeotdarc set nosy: + amaury.forgeotdarcmessages: +
2014-07-25 22:08:08 gennad set files: + 21862.patchnosy: + gennadmessages: +
2014-06-25 13:13:21 pitrou set messages: +
2014-06-24 21:30:21 rock set files: + cProfile-add-new-option-module.patchnosy: + rockmessages: + keywords: + patch
2014-06-24 20:06:54 pitrou create