Issue 14803: Add feature to allow code execution prior to main invocation (original) (raw)

Messages (27)

msg160597 - (view)

Author: Alyssa Coghlan (ncoghlan) * (Python committer)

Date: 2012-05-14 07:43

Reading http://nedbatchelder.com/code/coverage/subprocess.html, it occurred to me that there are various tracing and profiling operations that could be cleanly handled with significantly less work on the part of the tracing/profiling tool authors if the interpreter supported a "-C" operation that was like the existing "-c" option, but didn't terminate the options list.

The interpreter would invoke such commands after the interpreter is fully initialised, but before it begins the processing to find and execute main.

Then, to use subprocess coverage with coverage.py as an example, you could just run a command like:

"python -C 'import coverage; coverage.process_startup()' worker.py"

Other things you could usefully do in such an invocation is reconfigure sys.std(in|out|err) to match the settings used on the invoking side (e.g. to ensure Unicode data is tunnelled correctly), configure the logging module with a custom configuration, configure the warnings module programmatically, enable a memory profiler, etc.

Providing a function that could be called from -C and then uses an atexit() handler to do any necessary post-processing may be significantly simpler than trying to use runpy.run_(path|module) to achieve a similar effect.

msg160642 - (view)

Author: Antoine Pitrou (pitrou) * (Python committer)

Date: 2012-05-14 17:22

It would be nice to have a comparison of the available alternatives. It's not obvious that asking people to type some "-C ..." boilerplate to get code coverage is very user-friendly. Or am I misunderstanding the request?

msg160647 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2012-05-14 17:49

This can be achieved without intoducing a new interpreter option, using special module.

python -m prerun