@@ -1584,12 +1584,13 @@ sys_getwindowsversion_impl(PyObject *module)
1584
1584
int pos = 0 ;
1585
1585
OSVERSIONINFOEXW ver ;
1586
1586
1587
- version = PyObject_GetAttrString (module , "_cached_windows_version" );
1587
+ if (PyObject_GetOptionalAttrString (module , "_cached_windows_version" , & version ) < 0 ) {
1588
+ return NULL ;
1589
+ };
1588
1590
if (version && PyObject_TypeCheck (version , & WindowsVersionType )) {
1589
1591
return version ;
1590
1592
}
1591
1593
Py_XDECREF (version );
1592
- PyErr_Clear ();
1593
1594
1594
1595
ver .dwOSVersionInfoSize = sizeof (ver );
1595
1596
if (!GetVersionExW ((OSVERSIONINFOW * ) & ver ))
@@ -1599,22 +1600,35 @@ sys_getwindowsversion_impl(PyObject *module)
1599
1600
if (version == NULL )
1600
1601
return NULL ;
1601
1602
1602
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .dwMajorVersion ));
1603
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .dwMinorVersion ));
1604
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .dwBuildNumber ));
1605
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .dwPlatformId ));
1606
- PyStructSequence_SET_ITEM (version , pos ++ , PyUnicode_FromWideChar (ver .szCSDVersion , -1 ));
1607
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .wServicePackMajor ));
1608
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .wServicePackMinor ));
1609
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .wSuiteMask ));
1610
- PyStructSequence_SET_ITEM (version , pos ++ , PyLong_FromLong (ver .wProductType ));
1603
+ #define SET_VERSION_INFO (CALL ) \
1604
+ do { \
1605
+ PyObject *item = (CALL); \
1606
+ if (item == NULL) { \
1607
+ goto error; \
1608
+ } \
1609
+ PyStructSequence_SET_ITEM(version, pos++, item); \
1610
+ } while(0)
1611
+
1612
+ SET_VERSION_INFO (PyLong_FromLong (ver .dwMajorVersion ));
1613
+ SET_VERSION_INFO (PyLong_FromLong (ver .dwMinorVersion ));
1614
+ SET_VERSION_INFO (PyLong_FromLong (ver .dwBuildNumber ));
1615
+ SET_VERSION_INFO (PyLong_FromLong (ver .dwPlatformId ));
1616
+ SET_VERSION_INFO (PyUnicode_FromWideChar (ver .szCSDVersion , -1 ));
1617
+ SET_VERSION_INFO (PyLong_FromLong (ver .wServicePackMajor ));
1618
+ SET_VERSION_INFO (PyLong_FromLong (ver .wServicePackMinor ));
1619
+ SET_VERSION_INFO (PyLong_FromLong (ver .wSuiteMask ));
1620
+ SET_VERSION_INFO (PyLong_FromLong (ver .wProductType ));
1611
1621
1612
1622
// GetVersion will lie if we are running in a compatibility mode.
1613
1623
// We need to read the version info from a system file resource
1614
1624
// to accurately identify the OS version. If we fail for any reason,
1615
1625
// just return whatever GetVersion said.
1616
1626
PyObject * realVersion = _sys_getwindowsversion_from_kernel32 ();
1617
1627
if (!realVersion ) {
1628
+ if (!PyErr_ExceptionMatches (PyExc_WindowsError )) {
1629
+ return NULL ;
1630
+ }
1631
+
1618
1632
PyErr_Clear ();
1619
1633
realVersion = Py_BuildValue ("(kkk)" ,
1620
1634
ver .dwMajorVersion ,
@@ -1623,21 +1637,19 @@ sys_getwindowsversion_impl(PyObject *module)
1623
1637
);
1624
1638
}
1625
1639
1626
- if (realVersion ) {
1627
- PyStructSequence_SET_ITEM (version , pos ++ , realVersion );
1628
- }
1640
+ SET_VERSION_INFO (realVersion );
1629
1641
1630
- if (PyErr_Occurred ()) {
1631
- Py_DECREF (version );
1632
- return NULL ;
1633
- }
1642
+ #undef SET_VERSION_INFO
1634
1643
1635
1644
if (PyObject_SetAttrString (module , "_cached_windows_version" , version ) < 0 ) {
1636
- Py_DECREF (version );
1637
- return NULL ;
1645
+ goto error ;
1638
1646
}
1639
1647
1640
1648
return version ;
1649
+
1650
+ error :
1651
+ Py_DECREF (version );
1652
+ return NULL ;
1641
1653
}
1642
1654
1643
1655
#pragma warning(pop)
0 commit comments