[Python-checkins] r85772 - in python/branches/py3k: Doc/c-api/sys.rst Doc/data/refcounts.dat Doc/library/sys.rst Doc/using/cmdline.rst Include/sysmodule.h Lib/test/test_cmd_line.py Misc/NEWS Modules/main.c Python/getopt.c Python/sysmodule.c

antoine.pitrou python-checkins at python.org
Thu Oct 21 15:42:28 CEST 2010


Author: antoine.pitrou
Date: Thu Oct 21 15:42:28 2010
New Revision: 85772
Log:
Issue #10089: Add support for arbitrary -X options on the command-line.
They can be retrieved through a new attribute `sys._xoptions`.
Modified:
 python/branches/py3k/Doc/c-api/sys.rst
 python/branches/py3k/Doc/data/refcounts.dat
 python/branches/py3k/Doc/library/sys.rst
 python/branches/py3k/Doc/using/cmdline.rst
 python/branches/py3k/Include/sysmodule.h
 python/branches/py3k/Lib/test/test_cmd_line.py
 python/branches/py3k/Misc/NEWS
 python/branches/py3k/Modules/main.c
 python/branches/py3k/Python/getopt.c
 python/branches/py3k/Python/sysmodule.c
Modified: python/branches/py3k/Doc/c-api/sys.rst
==============================================================================
--- python/branches/py3k/Doc/c-api/sys.rst	(original)
+++ python/branches/py3k/Doc/c-api/sys.rst	Thu Oct 21 15:42:28 2010
@@ -127,6 +127,21 @@
 
 .. versionadded:: 3.2
 
+.. c:function:: void PySys_AddXOption(const wchar_t *s)
+
+ Parse *s* as a set of :option:`-X` options and add them to the current
+ options mapping as returned by :c:func:`PySys_GetXOptions`.
+
+ .. versionadded:: 3.2
+
+.. c:function:: PyObject *PySys_GetXOptions()
+
+ Return the current dictionary of :option:`-X` options, similarly to
+ :data:`sys._xoptions`. On error, *NULL* is returned and an exception is
+ set.
+
+ .. versionadded:: 3.2
+
 
 .. _processcontrol:
 
Modified: python/branches/py3k/Doc/data/refcounts.dat
==============================================================================
--- python/branches/py3k/Doc/data/refcounts.dat	(original)
+++ python/branches/py3k/Doc/data/refcounts.dat	Thu Oct 21 15:42:28 2010
@@ -1305,6 +1305,9 @@
 PySys_AddWarnOption:void:::
 PySys_AddWarnOption:char*:s::
 
+PySys_AddXOption:void:::
+PySys_AddXOption:const wchar_t*:s::
+
 PySys_GetFile:FILE*:::
 PySys_GetFile:char*:name::
 PySys_GetFile:FILE*:def::
@@ -1312,6 +1315,8 @@
 PySys_GetObject:PyObject*::0:
 PySys_GetObject:char*:name::
 
+PySys_GetXOptions:PyObject*::0:
+
 PySys_SetArgv:int:::
 PySys_SetArgv:int:argc::
 PySys_SetArgv:char**:argv::
Modified: python/branches/py3k/Doc/library/sys.rst
==============================================================================
--- python/branches/py3k/Doc/library/sys.rst	(original)
+++ python/branches/py3k/Doc/library/sys.rst	Thu Oct 21 15:42:28 2010
@@ -975,6 +975,30 @@
 module for informational purposes; modifying this value has no effect on the
 registry keys used by Python. Availability: Windows.
 
+
+.. data:: _xoptions
+
+ A dictionary of the various implementation-specific flags passed through
+ the :option:`-X` command-line option. Option names are either mapped to
+ their values, if given explicitly, or to :const:`True`. Example::
+
+ $ ./python -Xa=b -Xc
+ Python 3.2a3+ (py3k, Oct 16 2010, 20:14:50)
+ [GCC 4.4.3] on linux2
+ Type "help", "copyright", "credits" or "license" for more information.
+ >>> import sys
+ >>> sys._xoptions
+ {'a': 'b', 'c': True}
+
+ .. impl-detail::
+
+ This is a CPython-specific way of accessing options passed through
+ :option:`-X`. Other implementations may export them through other
+ means, or not at all.
+
+ .. versionadded:: 3.2
+
+
 .. rubric:: Citations
 
 .. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf .
Modified: python/branches/py3k/Doc/using/cmdline.rst
==============================================================================
--- python/branches/py3k/Doc/using/cmdline.rst	(original)
+++ python/branches/py3k/Doc/using/cmdline.rst	Thu Oct 21 15:42:28 2010
@@ -321,6 +321,17 @@
 
 .. note:: The line numbers in error messages will be off by one.
 
+
+.. cmdoption:: -X
+
+ Reserved for various implementation-specific options. CPython currently
+ defines none of them, but allows to pass arbitrary values and retrieve
+ them through the :data:`sys._xoptions` dictionary.
+
+ .. versionchanged:: 3.2
+ It is now allowed to pass :option:`-X` with CPython.
+
+
 Options you shouldn't use
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -330,11 +341,6 @@
 
 .. _Jython: http://jython.org
 
-.. cmdoption:: -X
-
- Reserved for alternative implementations of Python to use for their own
- purposes.
-
 .. _using-on-envvars:
 
 Environment variables
Modified: python/branches/py3k/Include/sysmodule.h
==============================================================================
--- python/branches/py3k/Include/sysmodule.h	(original)
+++ python/branches/py3k/Include/sysmodule.h	Thu Oct 21 15:42:28 2010
@@ -27,6 +27,9 @@
 PyAPI_FUNC(void) PySys_AddWarnOptionUnicode(PyObject *);
 PyAPI_FUNC(int) PySys_HasWarnOptions(void);
 
+PyAPI_FUNC(void) PySys_AddXOption(const wchar_t *);
+PyAPI_FUNC(PyObject *) PySys_GetXOptions(void);
+
 #ifdef __cplusplus
 }
 #endif
Modified: python/branches/py3k/Lib/test/test_cmd_line.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cmd_line.py	(original)
+++ python/branches/py3k/Lib/test/test_cmd_line.py	Thu Oct 21 15:42:28 2010
@@ -67,6 +67,15 @@
 rc, out, err = assert_python_ok('-vv')
 self.assertNotIn(b'stack overflow', err)
 
+ def test_xoptions(self):
+ rc, out, err = assert_python_ok('-c', 'import sys; print(sys._xoptions)')
+ opts = eval(out.splitlines()[0])
+ self.assertEqual(opts, {})
+ rc, out, err = assert_python_ok(
+ '-Xa', '-Xb=c,d=e', '-c', 'import sys; print(sys._xoptions)')
+ opts = eval(out.splitlines()[0])
+ self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})
+
 def test_run_module(self):
 # Test expected operation of the '-m' switch
 # Switch needs an argument
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Oct 21 15:42:28 2010
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #10089: Add support for arbitrary -X options on the command-line.
+ They can be retrieved through a new attribute ``sys._xoptions``.
+
 - Issue #4388: On Mac OS X, decode command line arguments from UTF-8, instead
 of the locale encoding. If the LANG (and LC_ALL and LC_CTYPE) environment
 variable is not set, the locale encoding is ISO-8859-1, whereas most programs
Modified: python/branches/py3k/Modules/main.c
==============================================================================
--- python/branches/py3k/Modules/main.c	(original)
+++ python/branches/py3k/Modules/main.c	Thu Oct 21 15:42:28 2010
@@ -47,7 +47,7 @@
 static int orig_argc;
 
 /* command line options */
-#define BASE_OPTS L"bBc:dEhiJm:OsStuvVW:xX?"
+#define BASE_OPTS L"bBc:dEhiJm:OsStuvVW:xX:?"
 
 #define PROGRAM_OPTS BASE_OPTS
 
@@ -84,6 +84,7 @@
 -W arg : warning control; arg is action:message:category:module:lineno\n\
 also PYTHONWARNINGS=arg\n\
 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
+-X opt : set implementation-specific option\n\
 ";
 static char *usage_4 = "\
 file : program read from script file\n\
@@ -407,8 +408,6 @@
 skipfirstline = 1;
 break;
 
- /* case 'X': reserved for implementation-specific arguments */
-
 case 'h':
 case '?':
 help++;
@@ -422,6 +421,10 @@
 PySys_AddWarnOption(_PyOS_optarg);
 break;
 
+ case 'X':
+ PySys_AddXOption(_PyOS_optarg);
+ break;
+
 /* This space reserved for other options */
 
 default:
Modified: python/branches/py3k/Python/getopt.c
==============================================================================
--- python/branches/py3k/Python/getopt.c	(original)
+++ python/branches/py3k/Python/getopt.c	Thu Oct 21 15:42:28 2010
@@ -89,12 +89,6 @@
 return '_';
 }
 
- if (option == 'X') {
- fprintf(stderr,
- "-X is reserved for implementation-specific arguments\n");
- return '_';
- }
-
 if ((ptr = wcschr(optstring, option)) == NULL) {
 if (_PyOS_opterr)
 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Modified: python/branches/py3k/Python/sysmodule.c
==============================================================================
--- python/branches/py3k/Python/sysmodule.c	(original)
+++ python/branches/py3k/Python/sysmodule.c	Thu Oct 21 15:42:28 2010
@@ -1086,6 +1086,61 @@
 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
 }
 
+static PyObject *xoptions = NULL;
+
+static PyObject *
+get_xoptions(void)
+{
+ if (xoptions == NULL || !PyDict_Check(xoptions)) {
+ Py_XDECREF(xoptions);
+ xoptions = PyDict_New();
+ }
+ return xoptions;
+}
+
+void
+PySys_AddXOption(const wchar_t *s)
+{
+ PyObject *opts;
+ PyObject *name = NULL, *value = NULL;
+ const wchar_t *name_end;
+ int r;
+
+ opts = get_xoptions();
+ if (opts == NULL)
+ goto error;
+
+ name_end = wcschr(s, L'=');
+ if (!name_end) {
+ name = PyUnicode_FromWideChar(s, -1);
+ value = Py_True;
+ Py_INCREF(value);
+ }
+ else {
+ name = PyUnicode_FromWideChar(s, name_end - s);
+ value = PyUnicode_FromWideChar(name_end + 1, -1);
+ }
+ if (name == NULL || value == NULL)
+ goto error;
+ r = PyDict_SetItem(opts, name, value);
+ Py_DECREF(name);
+ Py_DECREF(value);
+ return;
+
+error:
+ Py_XDECREF(name);
+ Py_XDECREF(value);
+ /* No return value, therefore clear error state if possible */
+ if (_Py_atomic_load_relaxed(&_PyThreadState_Current))
+ PyErr_Clear();
+}
+
+PyObject *
+PySys_GetXOptions(void)
+{
+ return get_xoptions();
+}
+
 /* XXX This doc string is too long to be a single string literal in VC++ 5.0.
 Two literals concatenated works just fine. If you have a K&R compiler
 or other abomination that however *does* understand longer strings,
@@ -1535,6 +1590,11 @@
 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
 }
 
+ v = get_xoptions();
+ if (v != NULL) {
+ PyDict_SetItemString(sysdict, "_xoptions", v);
+ }
+
 /* version_info */
 if (VersionInfoType.tp_name == 0)
 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /