Commit 6ab7c6ed8cdec29d0c62829f6c399eff79e8ca14
1 parent
f8591ef9
Fix exception handling SWIG interface for Python 3
Showing
5 changed files
with
54 additions
and
10 deletions
ChangeLog
| 1 | -2016-07-19 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | 1 | +2016-07-22 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> |
| 2 | 2 | ||
| 3 | * Version 1.1.0 released | 3 | * Version 1.1.0 released |
| 4 | ========================= | 4 | ========================= |
| 5 | 5 | ||
| 6 | + Fix exception handling SWIG interface for Python 3 | ||
| 6 | Write same XML element names than those that were read (#1820) | 7 | Write same XML element names than those that were read (#1820) |
| 7 | Rename spatial model components (#1818) | 8 | Rename spatial model components (#1818) |
| 8 | Fix consistency problem in GCTABackground3D (#1815) | 9 | Fix consistency problem in GCTABackground3D (#1815) |
NEWS
| @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 | @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 | ||
| 2 | 2 | ||
| 3 | Author(s) : Juergen Knoedlseder | 3 | Author(s) : Juergen Knoedlseder |
| 4 | 4 | ||
| 5 | -21 July 2016 | 5 | +22 July 2016 |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | 1. Introduction | 8 | 1. Introduction |
| @@ -358,6 +358,11 @@ be needed for response computation. | @@ -358,6 +358,11 @@ be needed for response computation. | ||
| 358 | A GTestSuite::test_value() method has been added for strings to simplify the | 358 | A GTestSuite::test_value() method has been added for strings to simplify the |
| 359 | writing of test cases for string comparison (#1772). | 359 | writing of test cases for string comparison (#1772). |
| 360 | 360 | ||
| 361 | +The Python interface for handling of SWIG exceptions is now also compatible | ||
| 362 | +with Python 3, hence there should be no longer "basic_string ::_S_construct | ||
| 363 | +null not valid" messages when errors occur in the SWIG interface under | ||
| 364 | +Python 3. | ||
| 365 | + | ||
| 361 | 366 | ||
| 362 | 18. Multi-wavelength interface | 367 | 18. Multi-wavelength interface |
| 363 | ------------------------------ | 368 | ------------------------------ |
README.md
| 1 | GammaLib information | 1 | GammaLib information |
| 2 | ==================== | 2 | ==================== |
| 3 | -* Version: 1.1.0.dev1 (19 July 2016) | 3 | +* Version: 1.1.0.dev1 (22 July 2016) |
| 4 | * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) | 4 | * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) |
| 5 | 5 | ||
| 6 | [](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/) | 6 | [](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/) |
pyext/GException.i
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * GException.i - exception handler * | 2 | * GException.i - exception handler * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2012 by Juergen Knoedlseder * | 4 | + * copyright (C) 2008-2016 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -36,10 +36,22 @@ | @@ -36,10 +36,22 @@ | ||
| 36 | catch (const GException::out_of_range& e) { | 36 | catch (const GException::out_of_range& e) { |
| 37 | SWIG_exception(SWIG_IndexError, e.what()); | 37 | SWIG_exception(SWIG_IndexError, e.what()); |
| 38 | } | 38 | } |
| 39 | - catch (const GExceptionHandler& e) { | 39 | + catch (const GException::invalid_value& e) { |
| 40 | + SWIG_exception(SWIG_ValueError, e.what()); | ||
| 41 | + } | ||
| 42 | + catch (const GException::invalid_argument& e) { | ||
| 43 | + SWIG_exception(SWIG_ValueError, e.what()); | ||
| 44 | + } | ||
| 45 | + catch (const GException::fits_error& e) { | ||
| 46 | + SWIG_exception(SWIG_IOError, e.what()); | ||
| 47 | + } | ||
| 48 | + catch (const GException& e) { | ||
| 40 | SWIG_exception(SWIG_RuntimeError, e.what()); | 49 | SWIG_exception(SWIG_RuntimeError, e.what()); |
| 41 | } | 50 | } |
| 42 | catch (const std::exception& e) { | 51 | catch (const std::exception& e) { |
| 43 | SWIG_exception(SWIG_RuntimeError, e.what()); | 52 | SWIG_exception(SWIG_RuntimeError, e.what()); |
| 44 | } | 53 | } |
| 54 | + catch (...) { | ||
| 55 | + SWIG_exception(SWIG_RuntimeError, "unknown exception"); | ||
| 56 | + } | ||
| 45 | } | 57 | } |
pyext/GTestSuite.i
| @@ -62,11 +62,37 @@ public: | @@ -62,11 +62,37 @@ public: | ||
| 62 | // Extract message strings | 62 | // Extract message strings |
| 63 | PyObject *py_type = PyObject_Str(type); | 63 | PyObject *py_type = PyObject_Str(type); |
| 64 | PyObject *py_value = PyObject_Str(value); | 64 | PyObject *py_value = PyObject_Str(value); |
| 65 | - char *c_type = PyString_AsString(py_type); | ||
| 66 | - char *c_value = PyString_AsString(py_value); | ||
| 67 | - m_message += std::string(c_type); | ||
| 68 | - m_message += "\n"; | ||
| 69 | - m_message += std::string(c_value); | 65 | + char *c_type = NULL; |
| 66 | + char *c_value = NULL; | ||
| 67 | + if (PyUnicode_Check(py_type)) { | ||
| 68 | + PyObject* temp_bytes = PyUnicode_AsEncodedString(py_type, | ||
| 69 | + "utf-8", | ||
| 70 | + "Error ~"); | ||
| 71 | + if (temp_bytes != NULL) { | ||
| 72 | + c_type = PyBytes_AS_STRING(temp_bytes); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + else if (py_type != NULL) { | ||
| 76 | + c_type = PyBytes_AS_STRING(py_type); | ||
| 77 | + } | ||
| 78 | + if (c_type != NULL) { | ||
| 79 | + m_message += std::string(c_type); | ||
| 80 | + m_message += "\n"; | ||
| 81 | + } | ||
| 82 | + if (PyUnicode_Check(py_value)) { | ||
| 83 | + PyObject* temp_bytes = PyUnicode_AsEncodedString(py_value, | ||
| 84 | + "utf-8", | ||
| 85 | + "Error ~"); | ||
| 86 | + if (temp_bytes != NULL) { | ||
| 87 | + c_value = PyBytes_AS_STRING(temp_bytes); | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + else if (py_value != NULL) { | ||
| 91 | + c_value = PyBytes_AS_STRING(py_value); | ||
| 92 | + } | ||
| 93 | + if (c_value != NULL) { | ||
| 94 | + m_message += std::string(c_value); | ||
| 95 | + } | ||
| 70 | Py_DECREF(py_type); | 96 | Py_DECREF(py_type); |
| 71 | Py_DECREF(py_value); | 97 | Py_DECREF(py_value); |
| 72 | } | 98 | } |