diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-21 13:42:28 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-21 13:42:28 (GMT) |
commit | 9583cac6337f9a5f2670fbe5e1f2e85aaad04522 (patch) | |
tree | 219c579de12fb04e6239ff11d548bfa454b6f135 /Python/sysmodule.c | |
parent | 6d61cb4d2fc1cd0b412bdf0cf15337751e56f0d2 (diff) | |
download | cpython-9583cac6337f9a5f2670fbe5e1f2e85aaad04522.zip cpython-9583cac6337f9a5f2670fbe5e1f2e85aaad04522.tar.gz cpython-9583cac6337f9a5f2670fbe5e1f2e85aaad04522.tar.bz2 |
Issue #10089: Add support for arbitrary -X options on the command-line.
They can be retrieved through a new attribute `sys._xoptions`.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 033c9d5..2530cc0 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1086,6 +1086,61 @@ PySys_HasWarnOptions(void) 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 @@ _PySys_Init(void) 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); |