comparison Python/sysmodule.c @ 51378:4cc49d978ec2

Merged revisions 69331 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r69331 | eric.smith | 2009-02-05 19:48:26 -0500 (Thu, 05 Feb 2009) | 2 lines Implement issue #4285, convert sys.version_info to a named tuple. Patch by Ross Light. ........
author Eric Smith <eric@trueblade.com>
date Fri, 06 Feb 2009 01:32:42 +0000
parents e8154bd322cb
children bb5de24a343f
comparison
equal deleted inserted replaced
51376:207097691972 51378:4cc49d978ec2
1015 maxsize -- the largest supported length of containers.\n\ 1015 maxsize -- the largest supported length of containers.\n\
1016 maxunicode -- the largest supported character\n\ 1016 maxunicode -- the largest supported character\n\
1017 builtin_module_names -- tuple of module names built into this interpreter\n\ 1017 builtin_module_names -- tuple of module names built into this interpreter\n\
1018 subversion -- subversion information of the build as tuple\n\ 1018 subversion -- subversion information of the build as tuple\n\
1019 version -- the version of this interpreter as a string\n\ 1019 version -- the version of this interpreter as a string\n\
1020 version_info -- version information as a tuple\n\ 1020 version_info -- version information as a named tuple\n\
1021 hexversion -- version information encoded as a single integer\n\ 1021 hexversion -- version information encoded as a single integer\n\
1022 copyright -- copyright notice pertaining to this interpreter\n\ 1022 copyright -- copyright notice pertaining to this interpreter\n\
1023 platform -- platform identifier\n\ 1023 platform -- platform identifier\n\
1024 executable -- pathname of this Python interpreter\n\ 1024 executable -- pathname of this Python interpreter\n\
1025 prefix -- prefix used to find the Python library\n\ 1025 prefix -- prefix used to find the Python library\n\
1225 return NULL; 1225 return NULL;
1226 } 1226 }
1227 return seq; 1227 return seq;
1228 } 1228 }
1229 1229
1230 PyDoc_STRVAR(version_info__doc__,
1231 "sys.version_info\n\
1232 \n\
1233 Version information as a named tuple.");
1234
1235 static PyTypeObject VersionInfoType;
1236
1237 static PyStructSequence_Field version_info_fields[] = {
1238 {"major", "Major release number"},
1239 {"minor", "Minor release number"},
1240 {"micro", "Patch release number"},
1241 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1242 {"serial", "Serial release number"},
1243 {0}
1244 };
1245
1246 static PyStructSequence_Desc version_info_desc = {
1247 "sys.version_info", /* name */
1248 version_info__doc__, /* doc */
1249 version_info_fields, /* fields */
1250 5
1251 };
1252
1253 static PyObject *
1254 make_version_info(void)
1255 {
1256 PyObject *version_info;
1257 char *s;
1258 int pos = 0;
1259
1260 version_info = PyStructSequence_New(&VersionInfoType);
1261 if (version_info == NULL) {
1262 return NULL;
1263 }
1264
1265 /*
1266 * These release level checks are mutually exclusive and cover
1267 * the field, so don't get too fancy with the pre-processor!
1268 */
1269 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
1270 s = "alpha";
1271 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
1272 s = "beta";
1273 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
1274 s = "candidate";
1275 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
1276 s = "final";
1277 #endif
1278
1279 #define SetIntItem(flag) \
1280 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
1281 #define SetStrItem(flag) \
1282 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
1283
1284 SetIntItem(PY_MAJOR_VERSION);
1285 SetIntItem(PY_MINOR_VERSION);
1286 SetIntItem(PY_MICRO_VERSION);
1287 SetStrItem(s);
1288 SetIntItem(PY_RELEASE_SERIAL);
1289 #undef SetIntItem
1290 #undef SetStrItem
1291
1292 if (PyErr_Occurred()) {
1293 Py_CLEAR(version_info);
1294 return NULL;
1295 }
1296 return version_info;
1297 }
1298
1230 static struct PyModuleDef sysmodule = { 1299 static struct PyModuleDef sysmodule = {
1231 PyModuleDef_HEAD_INIT, 1300 PyModuleDef_HEAD_INIT,
1232 "sys", 1301 "sys",
1233 sys_doc, 1302 sys_doc,
1234 -1, /* multiple "initialization" just copies the module dict. */ 1303 -1, /* multiple "initialization" just copies the module dict. */
1236 NULL, 1305 NULL,
1237 NULL, 1306 NULL,
1238 NULL, 1307 NULL,
1239 NULL 1308 NULL
1240 }; 1309 };
1241
1242
1243 1310
1244 PyObject * 1311 PyObject *
1245 _PySys_Init(void) 1312 _PySys_Init(void)
1246 { 1313 {
1247 PyObject *m, *v, *sysdict; 1314 PyObject *m, *v, *sysdict;
1289 SET_SYS_FROM_STRING("subversion", 1356 SET_SYS_FROM_STRING("subversion",
1290 Py_BuildValue("(UUU)", "CPython", branch, 1357 Py_BuildValue("(UUU)", "CPython", branch,
1291 svn_revision)); 1358 svn_revision));
1292 SET_SYS_FROM_STRING("dont_write_bytecode", 1359 SET_SYS_FROM_STRING("dont_write_bytecode",
1293 PyBool_FromLong(Py_DontWriteBytecodeFlag)); 1360 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1294 /*
1295 * These release level checks are mutually exclusive and cover
1296 * the field, so don't get too fancy with the pre-processor!
1297 */
1298 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
1299 s = "alpha";
1300 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
1301 s = "beta";
1302 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
1303 s = "candidate";
1304 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
1305 s = "final";
1306 #endif
1307
1308 SET_SYS_FROM_STRING("version_info",
1309 Py_BuildValue("iiiUi", PY_MAJOR_VERSION,
1310 PY_MINOR_VERSION,
1311 PY_MICRO_VERSION, s,
1312 PY_RELEASE_SERIAL));
1313 SET_SYS_FROM_STRING("api_version", 1361 SET_SYS_FROM_STRING("api_version",
1314 PyLong_FromLong(PYTHON_API_VERSION)); 1362 PyLong_FromLong(PYTHON_API_VERSION));
1315 SET_SYS_FROM_STRING("copyright", 1363 SET_SYS_FROM_STRING("copyright",
1316 PyUnicode_FromString(Py_GetCopyright())); 1364 PyUnicode_FromString(Py_GetCopyright()));
1317 SET_SYS_FROM_STRING("platform", 1365 SET_SYS_FROM_STRING("platform",
1359 } 1407 }
1360 if (warnoptions != NULL) { 1408 if (warnoptions != NULL) {
1361 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); 1409 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1362 } 1410 }
1363 1411
1412 /* version_info */
1413 if (VersionInfoType.tp_name == 0)
1414 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1415 SET_SYS_FROM_STRING("version_info", make_version_info());
1416 /* prevent user from creating new instances */
1417 VersionInfoType.tp_init = NULL;
1418 VersionInfoType.tp_new = NULL;
1419
1420 /* flags */
1364 if (FlagsType.tp_name == 0) 1421 if (FlagsType.tp_name == 0)
1365 PyStructSequence_InitType(&FlagsType, &flags_desc); 1422 PyStructSequence_InitType(&FlagsType, &flags_desc);
1366 SET_SYS_FROM_STRING("flags", make_flags()); 1423 SET_SYS_FROM_STRING("flags", make_flags());
1367 /* prevent user from creating new instances */ 1424 /* prevent user from creating new instances */
1368 FlagsType.tp_init = NULL; 1425 FlagsType.tp_init = NULL;