@@ -2059,27 +2059,29 @@ static struct PyMethodDef winreg_methods[] = {
2059
2059
NULL ,
2060
2060
};
2061
2061
2062
- static void
2063
- insint (PyObject * d , char * name , long value )
2064
- {
2065
- PyObject * v = PyLong_FromLong (value );
2066
- if (!v || PyDict_SetItemString (d , name , v ))
2067
- PyErr_Clear ();
2068
- Py_XDECREF (v );
2069
- }
2070
-
2071
- #define ADD_INT (val ) insint(d, #val, val)
2062
+ #define ADD_INT (VAL ) do { \
2063
+ if (PyModule_AddIntConstant(m, #VAL, VAL) < 0) { \
2064
+ goto error; \
2065
+ } \
2066
+ } while (0)
2072
2067
2073
- static void
2074
- inskey (PyObject * d , char * name , HKEY key )
2068
+ static int
2069
+ inskey (PyObject * mod , char * name , HKEY key )
2075
2070
{
2076
2071
PyObject * v = PyLong_FromVoidPtr (key );
2077
- if (!v || PyDict_SetItemString (d , name , v ))
2078
- PyErr_Clear ();
2079
- Py_XDECREF (v );
2072
+ if (v == NULL ) {
2073
+ return -1 ;
2074
+ }
2075
+ int rc = PyModule_AddObjectRef (mod , name , v );
2076
+ Py_DECREF (v );
2077
+ return rc ;
2080
2078
}
2081
2079
2082
- #define ADD_KEY (val ) inskey(d, #val, val)
2080
+ #define ADD_KEY (VAL ) do { \
2081
+ if (inskey(m, #VAL, VAL) < 0) { \
2082
+ goto error; \
2083
+ } \
2084
+ } while (0)
2083
2085
2084
2086
2085
2087
static struct PyModuleDef winregmodule = {
@@ -2096,20 +2098,20 @@ static struct PyModuleDef winregmodule = {
2096
2098
2097
2099
PyMODINIT_FUNC PyInit_winreg (void )
2098
2100
{
2099
- PyObject * m , * d ;
2100
- m = PyModule_Create (& winregmodule );
2101
- if (m == NULL )
2101
+ PyObject * m = PyModule_Create (& winregmodule );
2102
+ if (m == NULL ) {
2102
2103
return NULL ;
2103
- d = PyModule_GetDict ( m );
2104
+ }
2104
2105
PyHKEY_Type .tp_doc = PyHKEY_doc ;
2105
- if (PyType_Ready (& PyHKEY_Type ) < 0 )
2106
- return NULL ;
2107
- if (PyDict_SetItemString (d , "HKEYType" ,
2108
- (PyObject * )& PyHKEY_Type ) != 0 )
2109
- return NULL ;
2110
- if (PyDict_SetItemString (d , "error" ,
2111
- PyExc_OSError ) != 0 )
2112
- return NULL ;
2106
+ if (PyType_Ready (& PyHKEY_Type ) < 0 ) {
2107
+ goto error ;
2108
+ }
2109
+ if (PyModule_AddObjectRef (m , "HKEYType" , (PyObject * )& PyHKEY_Type ) < 0 ) {
2110
+ goto error ;
2111
+ }
2112
+ if (PyModule_AddObjectRef (m , "error" , PyExc_OSError ) < 0 ) {
2113
+ goto error ;
2114
+ }
2113
2115
2114
2116
/* Add the relevant constants */
2115
2117
ADD_KEY (HKEY_CLASSES_ROOT );
@@ -2170,7 +2172,14 @@ PyMODINIT_FUNC PyInit_winreg(void)
2170
2172
ADD_INT (REG_RESOURCE_LIST );
2171
2173
ADD_INT (REG_FULL_RESOURCE_DESCRIPTOR );
2172
2174
ADD_INT (REG_RESOURCE_REQUIREMENTS_LIST );
2175
+
2176
+ #undef ADD_INT
2177
+
2173
2178
return m ;
2179
+
2180
+ error :
2181
+ Py_DECREF (m );
2182
+ return NULL ;
2174
2183
}
2175
2184
2176
2185
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM || MS_WINDOWS_GAMES */
0 commit comments