MCPcopy Create free account
hub / github.com/OSGeo/gdal / GetPyExceptionString

Function GetPyExceptionString

gcore/gdalpython.cpp:908–1001  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

906/************************************************************************/
907
908CPLString GetPyExceptionString()
909{
910 PyObject *poPyType = nullptr;
911 PyObject *poPyValue = nullptr;
912 PyObject *poPyTraceback = nullptr;
913
914 PyErr_Fetch(&poPyType, &poPyValue, &poPyTraceback);
915 if (poPyType)
916 Py_IncRef(poPyType);
917 if (poPyValue)
918 Py_IncRef(poPyValue);
919 if (poPyTraceback)
920 Py_IncRef(poPyTraceback);
921
922 // This is a mess. traceback.format_exception/format_exception_only
923 // sometimes throw exceptions themselves !
924 CPLString osPythonCode(
925 "import traceback\n"
926 "\n"
927 "def GDALFormatException2(etype, value):\n"
928 " try:\n"
929 " return ''.join(traceback.format_exception_only(etype, value))\n"
930 " except:\n"
931 " return (str(etype) + ', ' + str(value))\n"
932 "\n"
933 "def GDALFormatException3(etype, value, tb):\n"
934 //" print(etype, value, tb)\n"
935 " try:\n"
936 " return ''.join(traceback.format_exception(etype, value, tb))\n"
937 " except:\n"
938 " return (str(etype) + ', ' + str(value))\n");
939
940 CPLString osRet("An exception occurred in exception formatting code...");
941
942 static int nCounter = 0;
943 CPLString osModuleName(CPLSPrintf("gdal_exception_%d", nCounter));
944 PyObject *poCompiledString =
945 Py_CompileString(osPythonCode, osModuleName, Py_file_input);
946 if (poCompiledString == nullptr || PyErr_Occurred())
947 {
948 PyErr_Print();
949 }
950 else
951 {
952 PyObject *poModule =
953 PyImport_ExecCodeModule(osModuleName, poCompiledString);
954 CPLAssert(poModule);
955
956 Py_DecRef(poCompiledString);
957
958 PyObject *poPyGDALFormatException2 =
959 PyObject_GetAttrString(poModule, "GDALFormatException2");
960 CPLAssert(poPyGDALFormatException2);
961
962 PyObject *poPyGDALFormatException3 =
963 PyObject_GetAttrString(poModule, "GDALFormatException3");
964 CPLAssert(poPyGDALFormatException3);
965

Callers 13

RunImplMethod · 0.85
GetStringFunction · 0.85
ErrOccurredEmitCPLErrorFunction · 0.85
GetIntResFunction · 0.85
GetStringResFunction · 0.85
GetFieldsMethod · 0.85
GetGeomFieldsMethod · 0.85
GetLayerMethod · 0.85
LoadPluginMethod · 0.85
IdentifyMethod · 0.85
OpenMethod · 0.85
RunImplMethod · 0.85

Calls 2

CPLSPrintfFunction · 0.85
GetStringFunction · 0.70

Tested by

no test coverage detected