classification
Title: Add support for arbitrary -X options
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, georg.brandl, haypo, loewis, merwok, ncoghlan, orsenthil, pitrou
Priority: normal Keywords: patch

Created on 2010-10-13 19:01 by pitrou, last changed 2010-10-21 13:42 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
xopts.patch pitrou, 2010-10-13 19:01
xopts2.patch pitrou, 2010-10-16 18:39
xopts3.patch pitrou, 2010-10-20 15:53
Messages (13)
msg118563 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-13 19:01
It can be useful to enable or disable global Python features based on command-line flags. The -X is supposed to allow implementation-specific options but it is currently disabled for CPython. This patch allows to use -X for arbitrary options in CPython. These options are not validated but minimally parsed and fed into a dictionary, sys.xoptions. An example is better than a thousand words:

$ ./python -c "import sys; print(sys.xoptions)"
{}
$ ./python -Xa,b=c,d -Xe,f=g=h -c "import sys; print(sys.xoptions)"
{'a': True, 'b': 'c', 'e': True, 'd': True, 'f': 'g=h'}


This could be useful for various debug enablers, such as Victor's SIGSEGV handler, warnings about unclosed files, etc.
msg118579 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-13 20:22
See issue10093 for a possible use case.
msg118729 - (view) Author: √Čric Araujo (merwok) * (Python committer) Date: 2010-10-14 22:53
Seems like a good idea to me.  Would other VMs have to implement sys.xoptions too?
msg118730 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-14 22:55
> Seems like a good idea to me.  Would other VMs have to implement sys.xoptions too?

I don't think we should mandate that, no.
msg118745 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-10-15 07:11
Why wouldn't they? A standard way to extend the command line options seems useful in all environments.
Now the interpretation of these options are subject to variations of course...
msg118746 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2010-10-15 07:18
On Wed, Oct 13, 2010 at 07:01:40PM +0000, Antoine Pitrou wrote:
> $ ./python -Xa,b=c,d -Xe,f=g=h -c "import sys; print(sys.xoptions)"
> {'a': True, 'b': 'c', 'e': True, 'd': True, 'f': 'g=h'}

Docs should be updated too. I see that the values could either be True
or a string? Would this be perceived as a limitation?
msg118748 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-10-15 07:29
Well, the syntax allows to pass either a string value (because it's a substring of the command line), or nothing.

When no value is passed, True seems better than None, because this allows the usage of the get() method::
    x = sys.xoptions.get('a')
or::
    if sys.xoptions.get('a'):
this returns None if the option was not set, True if set to a empty value, and a non-empty string if set to an actual value.
msg118752 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-10-15 09:04
I suggest using sys._xoptions to make it clearer that this is for CPython specific internal implementation runtime tweaks. We're putting it in sys so *we* can get at it, not applications.

(It also makes it clear that other implementations aren't obliged to implement *any* particular interface to the -X options. Requiring that would go against the whole spirit of -X)
msg118757 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-15 10:22
> (It also makes it clear that other implementations aren't obliged to
> implement *any* particular interface to the -X options. Requiring that 
> would go against the whole spirit of -X)

Agreed. I'll update the patch to use sys._xoptions.
msg118879 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-16 18:39
Here is a new patch renaming the attribute to sys._xoptions, and adding some docs.
msg118887 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-16 19:39
I'd prefer "-X key[=val]" without the possibility to mention multiple key=val pairs, so that val can contain anything (commata in particular).

Otherwise, the change looks fine to me.
msg119218 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-20 15:53
Here is a simpler version without comma-separated pairs.
msg119294 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-21 13:42
Committed in r85772.
History
Date User Action Args
2010-10-21 13:42:42pitrousetstatus: open -> closed
resolution: fixed
messages: + msg119294

stage: patch review -> resolved
2010-10-20 15:53:06pitrousetfiles: + xopts3.patch

messages: + msg119218
2010-10-16 19:39:21georg.brandlsetnosy: + georg.brandl
messages: + msg118887
2010-10-16 18:39:24pitrousetfiles: + xopts2.patch

messages: + msg118879
2010-10-15 10:22:00pitrousetmessages: + msg118757
2010-10-15 09:04:28ncoghlansetmessages: + msg118752
2010-10-15 07:29:28amaury.forgeotdarcsetmessages: + msg118748
2010-10-15 07:18:20orsenthilsetnosy: + orsenthil
messages: + msg118746
2010-10-15 07:11:50amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg118745
2010-10-14 22:55:01pitrousetmessages: + msg118730
2010-10-14 22:53:08merwoksetnosy: + merwok
messages: + msg118729
2010-10-13 20:22:43pitrousetmessages: + msg118579
2010-10-13 19:01:38pitroucreate