| 906 | /************************************************************************/ |
| 907 | |
| 908 | CPLString 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 |
no test coverage detected