comparison Python/sysmodule.c @ 58507:7379cf70fd60

Merged revisions 77763 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77763 | eric.smith | 2010-01-26 19:28:29 -0500 (Tue, 26 Jan 2010) | 1 line Issue #7766: Change sys.getwindowsversion() return value to a named tuple and add the additional members returned in an OSVERSIONINFOEX structure. The new members are service_pack_major, service_pack_minor, suite_mask, and product_type. ........
author Eric Smith <eric@trueblade.com>
date Wed, 27 Jan 2010 00:44:57 +0000
parents b1a775aa0710
children 29fac4b1a51e
comparison
equal deleted inserted replaced
58494:77238950c6dd 58507:7379cf70fd60
595 595
596 #ifdef MS_WINDOWS 596 #ifdef MS_WINDOWS
597 PyDoc_STRVAR(getwindowsversion_doc, 597 PyDoc_STRVAR(getwindowsversion_doc,
598 "getwindowsversion()\n\ 598 "getwindowsversion()\n\
599 \n\ 599 \n\
600 Return information about the running version of Windows.\n\ 600 Return information about the running version of Windows as a named tuple.\n\
601 The result is a tuple of (major, minor, build, platform, text)\n\ 601 The members are named: major, minor, build, platform, service_pack,\n\
602 All elements are numbers, except text which is a string.\n\ 602 service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\
603 Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\ 603 backward compatibiliy, only the first 5 items are available by indexing.\n\
604 " 604 All elements are numbers, except service_pack which is a string. Platform\n\
605 ); 605 may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\
606 3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\
607 controller, 3 for a server."
608 );
609
610 static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
611
612 static PyStructSequence_Field windows_version_fields[] = {
613 {"major", "Major version number"},
614 {"minor", "Minor version number"},
615 {"build", "Build number"},
616 {"platform", "Operating system platform"},
617 {"service_pack", "Latest Service Pack installed on the system"},
618 {"service_pack_major", "Service Pack major version number"},
619 {"service_pack_minor", "Service Pack minor version number"},
620 {"suite_mask", "Bit mask identifying available product suites"},
621 {"product_type", "System product type"},
622 {0}
623 };
624
625 static PyStructSequence_Desc windows_version_desc = {
626 "sys.getwindowsversion", /* name */
627 getwindowsversion_doc, /* doc */
628 windows_version_fields, /* fields */
629 5 /* For backward compatibility,
630 only the first 5 items are accessible
631 via indexing, the rest are name only */
632 };
606 633
607 static PyObject * 634 static PyObject *
608 sys_getwindowsversion(PyObject *self) 635 sys_getwindowsversion(PyObject *self)
609 { 636 {
610 OSVERSIONINFO ver; 637 PyObject *version;
638 int pos = 0;
639 OSVERSIONINFOEX ver;
611 ver.dwOSVersionInfoSize = sizeof(ver); 640 ver.dwOSVersionInfoSize = sizeof(ver);
612 if (!GetVersionEx(&ver)) 641 if (!GetVersionEx((OSVERSIONINFO*) &ver))
613 return PyErr_SetFromWindowsErr(0); 642 return PyErr_SetFromWindowsErr(0);
614 return Py_BuildValue("HHHHs", 643
615 ver.dwMajorVersion, 644 version = PyStructSequence_New(&WindowsVersionType);
616 ver.dwMinorVersion, 645 if (version == NULL)
617 ver.dwBuildNumber, 646 return NULL;
618 ver.dwPlatformId, 647
619 ver.szCSDVersion); 648 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
649 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
650 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
651 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
652 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
653 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
654 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
655 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
656 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
657
658 return version;
620 } 659 }
621 660
622 #endif /* MS_WINDOWS */ 661 #endif /* MS_WINDOWS */
623 662
624 #ifdef HAVE_DLOPEN 663 #ifdef HAVE_DLOPEN
1485 PyStructSequence_InitType(&FlagsType, &flags_desc); 1524 PyStructSequence_InitType(&FlagsType, &flags_desc);
1486 SET_SYS_FROM_STRING("flags", make_flags()); 1525 SET_SYS_FROM_STRING("flags", make_flags());
1487 /* prevent user from creating new instances */ 1526 /* prevent user from creating new instances */
1488 FlagsType.tp_init = NULL; 1527 FlagsType.tp_init = NULL;
1489 FlagsType.tp_new = NULL; 1528 FlagsType.tp_new = NULL;
1529
1530
1531 #if defined(MS_WINDOWS)
1532 /* getwindowsversion */
1533 if (WindowsVersionType.tp_name == 0)
1534 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1535 /* prevent user from creating new instances */
1536 WindowsVersionType.tp_init = NULL;
1537 WindowsVersionType.tp_new = NULL;
1538 #endif
1490 1539
1491 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ 1540 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
1492 #ifndef PY_NO_SHORT_FLOAT_REPR 1541 #ifndef PY_NO_SHORT_FLOAT_REPR
1493 SET_SYS_FROM_STRING("float_repr_style", 1542 SET_SYS_FROM_STRING("float_repr_style",
1494 PyUnicode_FromString("short")); 1543 PyUnicode_FromString("short"));