[Cython] errors in C++ tests

Stefan Behnel stefan_ml at behnel.de
Sun Jul 8 21:18:54 CEST 2012


Stefan Behnel, 08.07.2012 20:54:
> Lars Buitinck, 08.07.2012 20:38:
>> 2012/7/8 Stefan Behnel:
>>> """
>>> cpp_operators.cpp: In function ‘void __Pyx_CppExn2PyErr()’:
>>> cpp_operators.cpp:442: error: expected unqualified-id before ‘&’ token
>>> cpp_operators.cpp:442: error: expected `)' before ‘&’ token
>>> """
>>>
>>> The failing code line is this:
>>>
>>> """
>>>   } catch (const std::bad_alloc& exn) {
>>> """
>>
>> Could you check whether the header <new> is included? It should be to
>> get the definition of bad_alloc.
> 
> Ah, yes. I'm sure that's it.

... and there also were some more headers missing, so basically, this
feature never worked. Great. Here's the complete implementation with the
four header files that I had to add at the top:

"""
#include <new>
#include <typeinfo>
#include <stdexcept>
#include <iostream>

static void __Pyx_CppExn2PyErr() {
  // Catch a handful of different errors here and turn them into the
  // equivalent Python errors.
  try {
    if (PyErr_Occurred())
      ; // let the latest Python exn pass through and ignore the current one
    else
      throw;
  } catch (const std::bad_alloc& exn) {
    PyErr_SetString(PyExc_MemoryError, exn.what());
  } catch (const std::bad_cast& exn) {
    PyErr_SetString(PyExc_TypeError, exn.what());
  } catch (const std::domain_error& exn) {
    PyErr_SetString(PyExc_ValueError, exn.what());
  } catch (const std::invalid_argument& exn) {
    PyErr_SetString(PyExc_ValueError, exn.what());
  } catch (const std::ios_base::failure& exn) {
    PyErr_SetString(PyExc_IOError, exn.what());
  } catch (const std::out_of_range& exn) {
    // Change out_of_range to IndexError
    PyErr_SetString(PyExc_IndexError, exn.what());
  } catch (const std::overflow_error& exn) {
    PyErr_SetString(PyExc_OverflowError, exn.what());
  } catch (const std::range_error& exn) {
    PyErr_SetString(PyExc_ArithmeticError, exn.what());
  } catch (const std::underflow_error& exn) {
    PyErr_SetString(PyExc_ArithmeticError, exn.what());
  } catch (const std::exception& exn) {
    PyErr_SetString(PyExc_RuntimeError, exn.what());
  }
  catch (...)
  {
    PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
  }
}
"""

Back to this question then:

> Are there any side-effects in that header
> file, or would it be ok to always include it in C++ mode when the above
> exception conversion function is used?

Does it make sense to always include these four headers, or should we try
to handle the exceptions conditionally?

Stefan


More information about the cython-devel mailing list