Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 65575:0b5bfba7a907
Issue #10089: Add support for arbitrary -X options on the command-line.
They can be retrieved through a new attribute `sys._xoptions`.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 21 Oct 2010 13:42:28 +0000 |
| parents | e8969fb822fc |
| children | be8ae6b17539 |
comparison
equal
deleted
inserted
replaced
| 65574:2f94d68c2731 | 65575:0b5bfba7a907 |
|---|---|
| 1082 | 1082 |
| 1083 int | 1083 int |
| 1084 PySys_HasWarnOptions(void) | 1084 PySys_HasWarnOptions(void) |
| 1085 { | 1085 { |
| 1086 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; | 1086 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; |
| 1087 } | |
| 1088 | |
| 1089 static PyObject *xoptions = NULL; | |
| 1090 | |
| 1091 static PyObject * | |
| 1092 get_xoptions(void) | |
| 1093 { | |
| 1094 if (xoptions == NULL || !PyDict_Check(xoptions)) { | |
| 1095 Py_XDECREF(xoptions); | |
| 1096 xoptions = PyDict_New(); | |
| 1097 } | |
| 1098 return xoptions; | |
| 1099 } | |
| 1100 | |
| 1101 void | |
| 1102 PySys_AddXOption(const wchar_t *s) | |
| 1103 { | |
| 1104 PyObject *opts; | |
| 1105 PyObject *name = NULL, *value = NULL; | |
| 1106 const wchar_t *name_end; | |
| 1107 int r; | |
| 1108 | |
| 1109 opts = get_xoptions(); | |
| 1110 if (opts == NULL) | |
| 1111 goto error; | |
| 1112 | |
| 1113 name_end = wcschr(s, L'='); | |
| 1114 if (!name_end) { | |
| 1115 name = PyUnicode_FromWideChar(s, -1); | |
| 1116 value = Py_True; | |
| 1117 Py_INCREF(value); | |
| 1118 } | |
| 1119 else { | |
| 1120 name = PyUnicode_FromWideChar(s, name_end - s); | |
| 1121 value = PyUnicode_FromWideChar(name_end + 1, -1); | |
| 1122 } | |
| 1123 if (name == NULL || value == NULL) | |
| 1124 goto error; | |
| 1125 r = PyDict_SetItem(opts, name, value); | |
| 1126 Py_DECREF(name); | |
| 1127 Py_DECREF(value); | |
| 1128 return; | |
| 1129 | |
| 1130 error: | |
| 1131 Py_XDECREF(name); | |
| 1132 Py_XDECREF(value); | |
| 1133 /* No return value, therefore clear error state if possible */ | |
| 1134 if (_Py_atomic_load_relaxed(&_PyThreadState_Current)) | |
| 1135 PyErr_Clear(); | |
| 1136 } | |
| 1137 | |
| 1138 PyObject * | |
| 1139 PySys_GetXOptions(void) | |
| 1140 { | |
| 1141 return get_xoptions(); | |
| 1087 } | 1142 } |
| 1088 | 1143 |
| 1089 /* XXX This doc string is too long to be a single string literal in VC++ 5.0. | 1144 /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
| 1090 Two literals concatenated works just fine. If you have a K&R compiler | 1145 Two literals concatenated works just fine. If you have a K&R compiler |
| 1091 or other abomination that however *does* understand longer strings, | 1146 or other abomination that however *does* understand longer strings, |
| 1533 } | 1588 } |
| 1534 if (warnoptions != NULL) { | 1589 if (warnoptions != NULL) { |
| 1535 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); | 1590 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); |
| 1536 } | 1591 } |
| 1537 | 1592 |
| 1593 v = get_xoptions(); | |
| 1594 if (v != NULL) { | |
| 1595 PyDict_SetItemString(sysdict, "_xoptions", v); | |
| 1596 } | |
| 1597 | |
| 1538 /* version_info */ | 1598 /* version_info */ |
| 1539 if (VersionInfoType.tp_name == 0) | 1599 if (VersionInfoType.tp_name == 0) |
| 1540 PyStructSequence_InitType(&VersionInfoType, &version_info_desc); | 1600 PyStructSequence_InitType(&VersionInfoType, &version_info_desc); |
| 1541 SET_SYS_FROM_STRING("version_info", make_version_info()); | 1601 SET_SYS_FROM_STRING("version_info", make_version_info()); |
| 1542 /* prevent user from creating new instances */ | 1602 /* prevent user from creating new instances */ |
