diff --git a/Core/Tools/PyEmbeddedUtils.cpp b/Core/Tools/PyEmbeddedUtils.cpp
index 13266b63aefdbd05c52d42664400ef29ae4a4c18..e268e00d6a486418c31957b7df13e9e631415374 100644
--- a/Core/Tools/PyEmbeddedUtils.cpp
+++ b/Core/Tools/PyEmbeddedUtils.cpp
@@ -167,14 +167,13 @@ std::string PyEmbeddedUtils::pythonStackTrace()
             result << "\n";
             PyObject* pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
             if (pyth_func && PyCallable_Check(pyth_func)) {
-                PyObject* pyth_val;
-
-                pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
-
-                pystr = PyObject_Str(pyth_val);
-                if (char* str = PyString_AsString(pystr))
-                    result << std::string(str);
-                Py_DECREF(pyth_val);
+                PyObject* pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
+                if (pyth_val) {
+                    pystr = PyObject_Str(pyth_val);
+                    if (char* str = PyString_AsString(pystr))
+                        result << std::string(str);
+                    Py_DECREF(pyth_val);
+                }
             }
             result << "\n";
         }