[C++-sig] Catching Boost.Python.ArgumentError

2010-04-26 Thread Austin Bingham
I feel like I'm missing something simple, but how do I catch
Boost.Python.ArgumentException? As far as I can tell, the Boost.Python
module is not something I can import explicitly, so I can't write
"catch Boost.Python.ArgumentException:". I can do something like
comparing type and module name strings, but that just feels inelegant
and a trouble for maintenance. Any ideas would be very welcome.

Austin
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Catching Boost.Python.ArgumentError

2010-04-26 Thread Ralf W. Grosse-Kunstleve
The best you can do is:


try:
  foo("abc")
except Exception, e:
  if (not str(e).startswith("Python argument types in")):
raise
print "continue"



- Original Message 
From: Austin Bingham 
To: [email protected]
Sent: Mon, April 26, 2010 1:22:41 AM
Subject: [C++-sig] Catching Boost.Python.ArgumentError

I feel like I'm missing something simple, but how do I catch
Boost.Python.ArgumentException? As far as I can tell, the Boost.Python
module is not something I can import explicitly, so I can't write
"catch Boost.Python.ArgumentException:". I can do something like
comparing type and module name strings, but that just feels inelegant
and a trouble for maintenance. Any ideas would be very welcome.

Austin
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Catching Boost.Python.ArgumentError

2010-04-26 Thread Alex Mohr

On 4/26/2010 1:22 AM, Austin Bingham wrote:

I feel like I'm missing something simple, but how do I catch
Boost.Python.ArgumentException? As far as I can tell, the Boost.Python
module is not something I can import explicitly, so I can't write
"catch Boost.Python.ArgumentException:". I can do something like
comparing type and module name strings, but that just feels inelegant
and a trouble for maintenance. Any ideas would be very welcome.


I haven't looked at the code in awhile but I think I do it by intentionally 
generating an ArgumentError, catching it, and in the handler where I have the 
exception object, I store its class away for later use.


Alex
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] boost::python::len( list_object) throws "Access violation" (MSDev v9/Boost 1.41)

2010-04-26 Thread John McLaughlin
I believe I have found my problem.  

I was allowing the GIL to be released (essentially letting multiple
threads run concurrently) but the " const boost::python::list & disks"
argument was not protected at this point.

I can either move the Py_BEGIN_ALLOW_THREADS call to after I am finished
accessing the "disks" arg, or use Py_BLOCK_THREADS and
Py_UNBLOCK_THREADS to protect access calls.

John
 
John McLaughlin
iStor Networks, Inc.
-Original Message-
From: John McLaughlin 
Sent: Friday, April 23, 2010 8:01 PM
To: '[email protected].'
Subject: boost::python::len( list_object) throws "Access violation"
(MSDev v9/Boost 1.41)

I have a strange crash in my C++ method when called from Python.  I have
a method that has one of the arguments a Python List.  The symptoms are
the same with an empty list and a non-empty one.

In one method, early in the code I call;
   boost::python::len( var )
and it returns properly.

HOWEVER, in another method, I am passing the *same value* from Python,
but the "boost::python::len( var )" aborts when "PyErr_Occurred()" is
called.  It causes an access violation (like a bad pointer dereference).

What is strange is that the line before (calling
PyObject_Length(obj.ptr() ) returns a proper size.

Any ideas?

--

namespace boost { namespace python {

inline ssize_t len(object const& obj)
{
ssize_t result = PyObject_Length(obj.ptr());
if (PyErr_Occurred()) throw_error_already_set();
return result;
}

}} // namespace boost::python

--

void PyService_Volume::Reconfigure(
const PyStorageSize & sizeInBytes,
const
iStor::UserAgent::UAEnums::VolumeCompositionName & compositionName,
const boost::python::list & disks,
int stripeWidth,
const iStor::UserAgent::UAEnums::StripeDepth &
stripeDepth)
{
Py_BEGIN_ALLOW_THREADS
.
.
.

if (boost::python::len(disks) > 0)
{
...

--

John McLaughlin
iStor Networks, Inc.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig