Nick Coghlan <ncogh...@gmail.com> added the comment:

As Ned notes, to cover *implicit* creation of Python subprocesses an 
environment based solution would be needed to ensure the subprocesses adopt the 
desired settings. The advantage that has over the current workarounds is that 
it can be scoped to only affect the parent process when it is executed rather 
than affecting an entire Python installation.

As Serhiy notes, for *direct* invocation, you could use a custom module, but 
that wouldn't help with the subprocess case. In terms of implementation 
strategy, as with the -m switch, I'd probably bootstrap into runpy as soon as 
"-C" is encountered anyway to avoid the need for additional complexity in the C 
code.

One thing that would be useful with this is that it would eliminate some of the 
demand for -X options, since anything with a Python level API could just use -C 
instead. To use faulthandler as an example:

Current: "python -Xfaulthandler"
Alternate: "python -C 'import faulthandler; faulthandler.enable()'"

While the second is longer, the advantage is that it's the same as the way you 
enable faulthandler from Python, so there's no need to learn a special command 
(and, since its a -X option, the current way doesn't show up in the output of 
"python --help").

This would also cleanly resolve the request in issue 6958 to allow 
configuration of the logging module from the command line by allowing you to 
write things like:

   python -C "import logging; logging.basicConfig()" script.py

The interaction with other logging configuration is obvious (it follows the 
normal rules for multiple configuration attempts) and doesn't require the 
invention of new complex rules.

Warnings ends up in a similar boat. Simple options like -Wall, -Wdefault or 
-Werror are easy to use, but more complex configuration options are tricky. 
With "-C" you can do whatever you like using the Python API.

Other tricks also become possible without the need for launch scripts, like 
testing import logic and fallbacks by messing with sys.path and sys.modules, or 
patching builtins or other modules.

----------
title: Add -C option to run code at Python startup -> Enhanced command line 
features for the runpy module

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue14803>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to