Hi guys,

I have a problem building a python c++ extension and, in particular, the
problem occurs when I try to map C++ exceptions to python exceptions.
Here is a simple example:

========================================================================
### Cpp_Rectangle.pxd

cdef extern from "Rectangle.h":
  ctypedef struct cpp_Rectangle "Rectangle":

    int raiseException() except +

  cpp_Rectangle* new_Rectangle "new Rectangle"( int x0, int y0, int x1,
    int y1 )
  void delete_Rectangle "delete"( cpp_Rectangle* rect )

========================================================================
### PyRectangle.pyx

cimport Cpp_Rectangle

cdef class Rectangle:
  cdef Cpp_Rectangle.cpp_Rectangle* thisptr

  def __init__( self, int x0, int y0, int x1, int y1 ):
    self.thisptr = Cpp_Rectangle.new_Rectangle( x0, y0, x1, y1 )

  def __del__( self ):
    Cpp_Rectangle.delete_Rectangle( self.thisptr )

  def raiseException( self ):
    self.thisptr.raiseException()

=========================================================================

How you can see in the Cpp_Rectangle.pyd, I am trying to use the
"except +" feature to map the exceptions generated by the raiseException
 function into a Python RuntimeError.

The cython --cplus runs fine, but when I try to compile the
PyRectangle.cpp then I got the following errors:

=========================================================================

[sim...@spider Cython_Test]$ g++ -shared -fPIC -I/usr/include/python2.5
-o PyRectangle.so PyRectangle.cpp -L /usr/lib64 -L. -lRectangle -lpython2.5
PyRectangle.cpp: In function 'void __Pyx_CppExn2PyErr()':
PyRectangle.cpp:277: error: expected unqualified-id before '&' token
PyRectangle.cpp:277: error: ISO C++ forbids declaration of 'type name'
with no type
PyRectangle.cpp:277: error: expected `)' before '&' token
PyRectangle.cpp:277: error: expected `{' before '&' token
PyRectangle.cpp:277: error: 'exn' was not declared in this scope
PyRectangle.cpp:277: error: expected `;' before ')' token
PyRectangle.cpp:280: error: expected primary-expression before 'catch'
PyRectangle.cpp:280: error: expected `;' before 'catch'
PyRectangle.cpp:283: error: expected primary-expression before 'catch'
PyRectangle.cpp:283: error: expected `;' before 'catch'

==========================================================================

The errors occur because the the inclusion of the stdexcept header is
missing.

Is it a cython bug or it is up to the user to add the inclusion?

Actually, I have solved the problem adding the following rows to my
Cpp_Rectangle.pxd file, but I don't know if it is the right way to procede:

==========================================================================

cdef extern from "stdexcept":
  pass

==========================================================================

Another error happens when I change my Cpp_Rectangle.pxd like this:

==========================================================================

### Cpp_Rectangle.pxd

cdef extern from "stdexcept":
  pass

cdef extern from "Rectangle.h":
  ctypedef struct cpp_Rectangle "Rectangle":

    int raiseException() except +MemoryError

  cpp_Rectangle* new_Rectangle "new Rectangle"( int x0, int y0, int x1,
    int y1 )
  void delete_Rectangle "delete"( cpp_Rectangle* rect )

=========================================================================

Basically I am trying now to map each exception thrown by the
raiseException to a python MemoryError exception.

The cython compilation runs fine again, but when I try to compile the
.cpp file I got the errors:

=========================================================================

[sim...@spider Cython_Test]$ g++ -shared -fPIC -I/usr/include/python2.5
-o PyRectangle.so PyRectangle.cpp -L /usr/lib64 -L. -lRectangle -lpython2.5
PyRectangle.cpp: In function 'PyObject*
__pyx_pf_11PyRectangle_9Rectangle_raiseException(PyObject*, PyObject*)':
PyRectangle.cpp:454: error: '__pyx_builtin_MemoryError' was not declared
in this scope

==========================================================================

The accused piece of code is the following:

==========================================================================

/* __pyx_t_1 allocated */
try {
  __pyx_t_1 =
((struct__pyx_obj_11PyRectangle_Rectangle*)__pyx_v_self)->thisptr->raiseException();}


catch(...) {PyErr_SetString(__pyx_builtin_MemoryError, "");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno =
__LINE__; goto __pyx_L1_error;}}

=============================================================================

The variable __pyx_builtin_MemoryError is not declared anywhere.
Should the __pyx_builtin_MemoryError be substituted with PyExc_MemoryError?

The version of cython I am using is the 0.11

Thanks in advance and sorry for my long email.

Ciao,
Simone





begin:vcard
fn:Simone Bisogni
n:Bisogni;Simone
org:Tekno-Soft
adr:;;Via Marconi, 1;Oristano;OR;09170;Italy
email;internet:[email protected]
title:Dott. Ing.
tel;work:(+39) 0783 71339 202
tel;fax:(+39) 0783 71339 502
note;quoted-printable:=0D=0A=
	-----BEGIN PGP PUBLIC KEY BLOCK-----=0D=0A=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oxddtxPzZvCA6fPyGQ6OIygdQRgrnSrF66kTFZHU/IGWZT6bjC2s/2Mnt=0D=0A=
	QMmQWefKefWwORwDJd1Qr+N73tG4VYw2Kt2iHxRhIJ0TXNto6xrzw4wHhl7FY81b=0D=0A=
	Jk4xlja0LvfBDzvPOFwTq3EkyDkMkagxiRxyuGkbiir6m8BAoII0aRF2bvYjFyJD=0D=0A=
	u+P08Ft7YgrY5lu4J775IfuxsoYersUN8fpmpI8AAwUH/ReiArgqMfFTudbPLPIr=0D=0A=
	l1ULClrIzvO3G0brkoc/MK9ndORnFqdMEea6WhnnM+H+URB0X+H42wWxCS9s3qkQ=0D=0A=
	y7h2fdTytV6ns0Tw4EcQHy/K1gM6G78ynI1uGJ2XoOtJW/nx52ncvW7ztSAKYel5=0D=0A=
	bB5oF9LNWc5w9aSPt+ixg+Rc93hWztTS6pH9bS2c0fKAyaFUF03tNg3vsfmjORo+=0D=0A=
	LKAZPu8C+mcCrcz47ZfG974K8xB69BrJ7lNYzVmRY6n7JS5vwr/X8Jip/e4Xeu0n=0D=0A=
	Vi1wlE7kaVIF0vZOcMbdJaI1tN1hx9kavZsgDNkn4qurLCdmSiKxYP0rokxJuOkG=0D=0A=
	DsmISQQYEQIACQUCSVit7wIbDAAKCRCOKENRYGw4N0DOAKCLmS0I/z3gejeA1Rmf=0D=0A=
	Ak93hxJV6ACfSAqHR3CvarjUOrJtVkKsTUBwXck=3D=0D=0A=
	=3D+mqh=0D=0A=
	-----END PGP PUBLIC KEY BLOCK-----
x-mozilla-html:TRUE
url:http://www.tekno-soft.it
version:2.1
end:vcard

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to