comparison Python/sysmodule.c @ 45059:9639a73afa47 legacy-trunk

Fixed some references leaks in sys.
author Christian Heimes <christian@cheimes.de>
date Wed, 30 Jan 2008 17:21:22 +0000
parents ea3fe302d392
children c05ee77b39f2
comparison
equal deleted inserted replaced
45054:53b90f31efd6 45059:9639a73afa47
1179 1179
1180 if (PyErr_Occurred()) { 1180 if (PyErr_Occurred()) {
1181 return NULL; 1181 return NULL;
1182 } 1182 }
1183 1183
1184 Py_INCREF(seq);
1185 return seq; 1184 return seq;
1186 } 1185 }
1187 1186
1188 PyObject * 1187 PyObject *
1189 _PySys_Init(void) 1188 _PySys_Init(void)
1197 1196
1198 m = Py_InitModule3("sys", sys_methods, sys_doc); 1197 m = Py_InitModule3("sys", sys_methods, sys_doc);
1199 if (m == NULL) 1198 if (m == NULL)
1200 return NULL; 1199 return NULL;
1201 sysdict = PyModule_GetDict(m); 1200 sysdict = PyModule_GetDict(m);
1201 #define SET_SYS_FROM_STRING(key, value) \
1202 v = value; \
1203 if (v != NULL) \
1204 PyDict_SetItemString(sysdict, key, v); \
1205 Py_XDECREF(v)
1202 1206
1203 { 1207 {
1204 /* XXX: does this work on Win/Win64? (see posix_fstat) */ 1208 /* XXX: does this work on Win/Win64? (see posix_fstat) */
1205 struct stat sb; 1209 struct stat sb;
1206 if (fstat(fileno(stdin), &sb) == 0 && 1210 if (fstat(fileno(stdin), &sb) == 0 &&
1256 PyDict_SetItemString(sysdict, "__excepthook__", 1260 PyDict_SetItemString(sysdict, "__excepthook__",
1257 PyDict_GetItemString(sysdict, "excepthook")); 1261 PyDict_GetItemString(sysdict, "excepthook"));
1258 Py_XDECREF(sysin); 1262 Py_XDECREF(sysin);
1259 Py_XDECREF(sysout); 1263 Py_XDECREF(sysout);
1260 Py_XDECREF(syserr); 1264 Py_XDECREF(syserr);
1261 PyDict_SetItemString(sysdict, "version", 1265
1262 v = PyString_FromString(Py_GetVersion())); 1266 SET_SYS_FROM_STRING("version",
1263 Py_XDECREF(v); 1267 PyString_FromString(Py_GetVersion()));
1264 PyDict_SetItemString(sysdict, "hexversion", 1268 SET_SYS_FROM_STRING("hexversion",
1265 v = PyInt_FromLong(PY_VERSION_HEX)); 1269 PyInt_FromLong(PY_VERSION_HEX));
1266 Py_XDECREF(v);
1267 svnversion_init(); 1270 svnversion_init();
1268 v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision); 1271 SET_SYS_FROM_STRING("subversion",
1269 PyDict_SetItemString(sysdict, "subversion", v); 1272 Py_BuildValue("(ssz)", "CPython", branch,
1270 Py_XDECREF(v); 1273 svn_revision));
1271 PyDict_SetItemString(sysdict, "dont_write_bytecode", 1274 SET_SYS_FROM_STRING("dont_write_bytecode",
1272 v = PyBool_FromLong(Py_DontWriteBytecodeFlag)); 1275 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1273 Py_XDECREF(v);
1274 /* 1276 /*
1275 * These release level checks are mutually exclusive and cover 1277 * These release level checks are mutually exclusive and cover
1276 * the field, so don't get too fancy with the pre-processor! 1278 * the field, so don't get too fancy with the pre-processor!
1277 */ 1279 */
1278 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA 1280 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
1282 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA 1284 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
1283 s = "candidate"; 1285 s = "candidate";
1284 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL 1286 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
1285 s = "final"; 1287 s = "final";
1286 #endif 1288 #endif
1287
1288 #define SET_SYS_FROM_STRING(key, value) \
1289 v = value; \
1290 if (v != NULL) \
1291 PyDict_SetItemString(sysdict, key, v); \
1292 Py_XDECREF(v)
1293 1289
1294 SET_SYS_FROM_STRING("version_info", 1290 SET_SYS_FROM_STRING("version_info",
1295 Py_BuildValue("iiisi", PY_MAJOR_VERSION, 1291 Py_BuildValue("iiisi", PY_MAJOR_VERSION,
1296 PY_MINOR_VERSION, 1292 PY_MINOR_VERSION,
1297 PY_MICRO_VERSION, s, 1293 PY_MICRO_VERSION, s,
1338 SET_SYS_FROM_STRING("dllhandle", 1334 SET_SYS_FROM_STRING("dllhandle",
1339 PyLong_FromVoidPtr(PyWin_DLLhModule)); 1335 PyLong_FromVoidPtr(PyWin_DLLhModule));
1340 SET_SYS_FROM_STRING("winver", 1336 SET_SYS_FROM_STRING("winver",
1341 PyString_FromString(PyWin_DLLVersionString)); 1337 PyString_FromString(PyWin_DLLVersionString));
1342 #endif 1338 #endif
1343 #undef SET_SYS_FROM_STRING
1344 if (warnoptions == NULL) { 1339 if (warnoptions == NULL) {
1345 warnoptions = PyList_New(0); 1340 warnoptions = PyList_New(0);
1346 } 1341 }
1347 else { 1342 else {
1348 Py_INCREF(warnoptions); 1343 Py_INCREF(warnoptions);
1350 if (warnoptions != NULL) { 1345 if (warnoptions != NULL) {
1351 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); 1346 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1352 } 1347 }
1353 1348
1354 PyStructSequence_InitType(&FlagsType, &flags_desc); 1349 PyStructSequence_InitType(&FlagsType, &flags_desc);
1355 PyDict_SetItemString(sysdict, "flags", make_flags()); 1350 SET_SYS_FROM_STRING("flags", make_flags());
1356 /* prevent user from creating new instances */ 1351 /* prevent user from creating new instances */
1357 FlagsType.tp_init = NULL; 1352 FlagsType.tp_init = NULL;
1358 FlagsType.tp_new = NULL; 1353 FlagsType.tp_new = NULL;
1359 1354
1355 #undef SET_SYS_FROM_STRING
1360 if (PyErr_Occurred()) 1356 if (PyErr_Occurred())
1361 return NULL; 1357 return NULL;
1362 return m; 1358 return m;
1363 } 1359 }
1364 1360