Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f30dbd

Browse files
committed
Fix SF # 626275, missing DECREF's in embedding example
Tested w/valgrind, all paths except the return on PyInt_AsLong() failure I think I got all of these right. Backport candidate.
1 parent 1bdca5e commit 0f30dbd

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

Doc/ext/run-func.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ main(int argc, char *argv[])
1717
/* Error checking of pName left out */
1818

1919
pModule = PyImport_Import(pName);
20+
Py_DECREF(pName);
21+
2022
if (pModule != NULL) {
2123
pDict = PyModule_GetDict(pModule);
2224
/* pDict is a borrowed reference */
@@ -29,27 +31,31 @@ main(int argc, char *argv[])
2931
for (i = 0; i < argc - 3; ++i) {
3032
pValue = PyInt_FromLong(atoi(argv[i + 3]));
3133
if (!pValue) {
34+
Py_DECREF(pArgs);
35+
Py_DECREF(pModule);
3236
fprintf(stderr, "Cannot convert argument\n");
3337
return 1;
3438
}
3539
/* pValue reference stolen here: */
3640
PyTuple_SetItem(pArgs, i, pValue);
3741
}
3842
pValue = PyObject_CallObject(pFunc, pArgs);
43+
Py_DECREF(pArgs);
3944
if (pValue != NULL) {
4045
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
4146
Py_DECREF(pValue);
4247
}
4348
else {
49+
Py_DECREF(pModule);
4450
PyErr_Print();
4551
fprintf(stderr,"Call failed\n");
4652
return 1;
4753
}
48-
Py_DECREF(pArgs);
4954
/* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
5055
}
5156
else {
52-
PyErr_Print();
57+
if (PyErr_Occurred())
58+
PyErr_Print();
5359
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
5460
}
5561
Py_DECREF(pModule);
@@ -59,7 +65,6 @@ main(int argc, char *argv[])
5965
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
6066
return 1;
6167
}
62-
Py_DECREF(pName);
6368
Py_Finalize();
6469
return 0;
6570
}

0 commit comments

Comments
 (0)