Re: [C++-sig] export custom C++ exception to Python so it can beraisedby python

2012-11-07 Thread Martin Hellmich

Hi Giuseppe,

thank you for the detailed response!

On 11/07/2012 08:56 AM, Giuseppe Corbelli wrote:

On 06/11/2012 15:35, Martin Hellmich wrote:

I've based my code on.
http://stackoverflow.c  om/questions/9620268/boost-python-custom-exception-class



I have used the same reference and also this suggestion for including the
other attributes shown here:
http://stackoverflow.com/questions/11448735/boostpython-export-custom-exception-and-inherit-from-pythons-exception


However, this doesn't solve my problem.
It works fine when I throw the MyException in C++, but when I raise the
MyCPPException in Python, the translator is not called (which makes
sense), so
the attribute 'cause' does not change.

Actually, before I throw the MyException for the first time in C++,
MyCPPException does not have the attribute 'cause' at all.


Of course.


This issue should be the same, if I set the exception with
PyErr_SetObject,
since the translator is not called, when I 'raise' it.


Very likely the SetString uses SetObject under the hood, but I never
used SetObject.
I'd try to:

*) As you were writing before, declare a C++ class MyException: public
std::exception {
  MyException();
  MyException(int code, const std::string &string);

  int code();
  const char* what();

  int errorCode;
  std::string errorMessage;
  };
*) Wrap it as a bpy::class_ so you can use it like a standard exception
with all the attributes you need.


This is the part that I miss, I guess. How can I wrap a class so that it 
works like an exception in python? In the solutions on stackoverflow the 
translation to an exception only happens in the translator, meaning that 
I cannot call 'raise MyException' in python, as it doesn't inherit from 
(the python) Exception.


PyErr_NewException has to be based on a type of python Exception, so I 
cannot base it on my c++ class, right?

Have you ever used the 3rd parameter to specify a dict?

Cheers
Martin


*) Register an exception translator as defined before. Maybe the
to-python conversion performed in the exception translator does work
even if you have wrapped MyException as a standard class.



--
Martin HellmichInformation Technology Department
mhell...@cern.chCERN
+41 22 76 765 26   CH-1211 Geneva 23
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] export custom C++ exception to Python so it can beraisedbypython

2012-11-07 Thread Giuseppe Corbelli

On 07/11/2012 09:24, Martin Hellmich wrote:

*) As you were writing before, declare a C++ class MyException: public
std::exception {
MyException();
MyException(int code, const std::string &string);

int code();
const char* what();

int errorCode;
std::string errorMessage;
};
*) Wrap it as a bpy::class_ so you can use it like a standard exception
with all the attributes you need.


This is the part that I miss, I guess. How can I wrap a class so that it works
like an exception in python? In the solutions on stackoverflow the translation
to an exception only happens in the translator, meaning that I cannot call
'raise MyException' in python, as it doesn't inherit from (the python) 
Exception.


I've had a closer look. What if we take WindowsError as an example?
*) Define a new python exception CustomError with python/C APIs based on 
WindowsError

*) Define a new C++ exception CustomException based on std::exception
*) Register a translator from C++ to python using CustomError as a target

Seems a reasonable approach.

--
Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbe...@copanitalia.com
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] buildin converter for long double

2012-11-07 Thread Holger Brandsmeier
Dear Jiang,

thanks I got it to work by using my own version of
`builtin_converters.hpp` locally together with the code that you
recommended.

-Holger

On Tue, Nov 6, 2012 at 3:25 AM, jiangzuo...@gmail.com
 wrote:
> how about return a wrap type of long double to cheating boost/python, and
> register youself converter as
>
>   PyObject * o = PyArrayScalar_New(LongDouble);
>   PyLongDoubleScalarObject * s = (PyLongDoubleScalarObject*)o;
>   s->obval = value;
>
> Don't forget to call import_array() in your model init function.
>
> BTW, I think float128 is not useful, it's slow. and in most case, change
> algorithm to avoid precision problem is another *better* approach.
>
>
>   Changsheng Jiang
>
>
> On Mon, Nov 5, 2012 at 6:27 PM, Holger Brandsmeier 
> wrote:
>>
>> Dear list,
>>
>> the boost python buildin converter for long double:
>>   BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double,
>> ::PyFloat_FromDouble(x), &PyFloat_Type)
>>   converter/builtin_converters.hpp
>> identifies long double, which on my system is a 128bit float, with a
>> 64bit float in python. I would like to identify it with numpy`s
>> float128. Can you give me any hings how I would be able to do that?
>>
>> -Holger
>> ___
>> Cplusplus-sig mailing list
>> Cplusplus-sig@python.org
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
>
> ___
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig