[Python-checkins] r69346 - in python/branches/py3k: Doc/library/sys.rst Lib/test/test_sys.py Misc/NEWS Python/sysmodule.c

eric.smith python-checkins at python.org
Fri Feb 6 02:32:43 CET 2009


Author: eric.smith
Date: Fri Feb  6 02:32:42 2009
New Revision: 69346

Log:
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.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/library/sys.rst
   python/branches/py3k/Lib/test/test_sys.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Python/sysmodule.c

Modified: python/branches/py3k/Doc/library/sys.rst
==============================================================================
--- python/branches/py3k/Doc/library/sys.rst	(original)
+++ python/branches/py3k/Doc/library/sys.rst	Fri Feb  6 02:32:42 2009
@@ -769,8 +769,12 @@
    *micro*, *releaselevel*, and *serial*.  All values except *releaselevel* are
    integers; the release level is ``'alpha'``, ``'beta'``, ``'candidate'``, or
    ``'final'``.  The ``version_info`` value corresponding to the Python version 2.0
-   is ``(2, 0, 0, 'final', 0)``.
+   is ``(2, 0, 0, 'final', 0)``.  The components can also be accessed by name,
+   so ``sys.version_info[0]`` is equivalent to ``sys.version_info.major``
+   and so on.
 
+   .. versionchanged:: 2.7
+      Added named component attributes
 
 .. data:: warnoptions
 

Modified: python/branches/py3k/Lib/test/test_sys.py
==============================================================================
--- python/branches/py3k/Lib/test/test_sys.py	(original)
+++ python/branches/py3k/Lib/test/test_sys.py	Fri Feb  6 02:32:42 2009
@@ -298,13 +298,25 @@
         self.assert_(isinstance(sys.prefix, str))
         self.assert_(isinstance(sys.version, str))
         vi = sys.version_info
-        self.assert_(isinstance(vi, tuple))
+        self.assert_(isinstance(vi[:], tuple))
         self.assertEqual(len(vi), 5)
         self.assert_(isinstance(vi[0], int))
         self.assert_(isinstance(vi[1], int))
         self.assert_(isinstance(vi[2], int))
         self.assert_(vi[3] in ("alpha", "beta", "candidate", "final"))
         self.assert_(isinstance(vi[4], int))
+        self.assert_(isinstance(vi.major, int))
+        self.assert_(isinstance(vi.minor, int))
+        self.assert_(isinstance(vi.micro, int))
+        self.assert_(vi.releaselevel in
+                     ("alpha", "beta", "candidate", "final"))
+        self.assert_(isinstance(vi.serial, int))
+        self.assertEqual(vi[0], vi.major)
+        self.assertEqual(vi[1], vi.minor)
+        self.assertEqual(vi[2], vi.micro)
+        self.assertEqual(vi[3], vi.releaselevel)
+        self.assertEqual(vi[4], vi.serial)
+        self.assert_(vi > (1,0,0))
 
     def test_43581(self):
         # Can't use sys.stdout, as this is a StringIO object when

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Feb  6 02:32:42 2009
@@ -155,6 +155,9 @@
 Library
 -------
 
+- Issue #4285: Change sys.version_info to be a named tuple. Patch by
+  Ross Light.
+
 - Issue #1520877: Now distutils.sysconfig reads $AR from the 
   environment/Makefile. Patch by Douglas Greiman.
 

Modified: python/branches/py3k/Python/sysmodule.c
==============================================================================
--- python/branches/py3k/Python/sysmodule.c	(original)
+++ python/branches/py3k/Python/sysmodule.c	Fri Feb  6 02:32:42 2009
@@ -1017,7 +1017,7 @@
 builtin_module_names -- tuple of module names built into this interpreter\n\
 subversion -- subversion information of the build as tuple\n\
 version -- the version of this interpreter as a string\n\
-version_info -- version information as a tuple\n\
+version_info -- version information as a named tuple\n\
 hexversion -- version information encoded as a single integer\n\
 copyright -- copyright notice pertaining to this interpreter\n\
 platform -- platform identifier\n\
@@ -1227,6 +1227,75 @@
 	return seq;
 }
 
+PyDoc_STRVAR(version_info__doc__,
+"sys.version_info\n\
+\n\
+Version information as a named tuple.");
+
+static PyTypeObject VersionInfoType;
+
+static PyStructSequence_Field version_info_fields[] = {
+	{"major", "Major release number"},
+	{"minor", "Minor release number"},
+	{"micro", "Patch release number"},
+	{"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
+	{"serial", "Serial release number"},
+	{0}
+};
+
+static PyStructSequence_Desc version_info_desc = {
+	"sys.version_info",     /* name */
+	version_info__doc__,    /* doc */
+	version_info_fields,    /* fields */
+	5
+};
+
+static PyObject *
+make_version_info(void)
+{
+	PyObject *version_info;
+	char *s;
+	int pos = 0;
+
+	version_info = PyStructSequence_New(&VersionInfoType);
+	if (version_info == NULL) {
+		return NULL;
+	}
+
+	/*
+	 * These release level checks are mutually exclusive and cover
+	 * the field, so don't get too fancy with the pre-processor!
+	 */
+#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
+	s = "alpha";
+#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
+	s = "beta";
+#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
+	s = "candidate";
+#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
+	s = "final";
+#endif
+
+#define SetIntItem(flag) \
+	PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
+#define SetStrItem(flag) \
+	PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
+
+	SetIntItem(PY_MAJOR_VERSION);
+	SetIntItem(PY_MINOR_VERSION);
+	SetIntItem(PY_MICRO_VERSION);
+	SetStrItem(s);
+	SetIntItem(PY_RELEASE_SERIAL);
+#undef SetIntItem
+#undef SetStrItem
+
+	if (PyErr_Occurred()) {
+		Py_CLEAR(version_info);
+		return NULL;
+	}
+	return version_info;
+}
+
 static struct PyModuleDef sysmodule = {
 	PyModuleDef_HEAD_INIT,
 	"sys",
@@ -1239,8 +1308,6 @@
 	NULL
 };
 
-
-
 PyObject *
 _PySys_Init(void)
 {
@@ -1291,25 +1358,6 @@
 					  svn_revision));
 	SET_SYS_FROM_STRING("dont_write_bytecode",
 			     PyBool_FromLong(Py_DontWriteBytecodeFlag));
-	/*
-	 * These release level checks are mutually exclusive and cover
-	 * the field, so don't get too fancy with the pre-processor!
-	 */
-#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
-	s = "alpha";
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
-	s = "beta";
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
-	s = "candidate";
-#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
-	s = "final";
-#endif
-
-	SET_SYS_FROM_STRING("version_info",
-			    Py_BuildValue("iiiUi", PY_MAJOR_VERSION,
-					       PY_MINOR_VERSION,
-					       PY_MICRO_VERSION, s,
-					       PY_RELEASE_SERIAL));
 	SET_SYS_FROM_STRING("api_version",
 			    PyLong_FromLong(PYTHON_API_VERSION));
 	SET_SYS_FROM_STRING("copyright",
@@ -1361,6 +1409,15 @@
 		PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
 	}
 
+	/* version_info */
+	if (VersionInfoType.tp_name == 0)
+		PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
+	SET_SYS_FROM_STRING("version_info", make_version_info());
+	/* prevent user from creating new instances */
+	VersionInfoType.tp_init = NULL;
+	VersionInfoType.tp_new = NULL;
+
+	/* flags */
 	if (FlagsType.tp_name == 0)
 		PyStructSequence_InitType(&FlagsType, &flags_desc);
 	SET_SYS_FROM_STRING("flags", make_flags());


More information about the Python-checkins mailing list