boost python & context manager/with statement

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

boost python & context manager/with statement

Avi Bahra
Does boost python support context management using the with
statement?  In my case I need to manage __enter__/__exit__ on
the c++ side. Could not find any examples for this in the
documentation.

Any help appreciated.

Best regards,
Ta,
    Avi
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: boost python & context manager/with statement

Jim Bosch-2
On 02/12/2012 01:25 PM, Avi Bahra wrote:
> Does boost python support context management using the with
> statement?  In my case I need to manage __enter__/__exit__ on
> the c++ side. Could not find any examples for this in the
> documentation.
>

Do you mean that you want to create an object that *has* __enter__ and
__exit__ methods in C++, or that you want to write something like a
"with" statement in C++?

If it's the former, you can do that pretty easily - just call your
methods "__enter__" and "__exit__" and give them the right signatures,
and Python will use them quite happily.

If it's the latter, I'm afraid there's no syntactic sugar for it.  It's
probably easiest to fake it by calling __enter__ and __exit__ and
wrapping it all in a C++ try/catch block (if you raise a Python
exception, it gets thrown in C++ as boost::python::error_already_set).
But C++ itself doesn't really have a context management syntax, so there
isn't really a good way to simulate the Python one.

HTH

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: boost python & context manager/with statement

Dave Abrahams
In reply to this post by Avi Bahra

on Sun Feb 12 2012, Avi Bahra <avibahra-AT-googlemail.com> wrote:

> Does boost python support context management using the with
> statement?  In my case I need to manage __enter__/__exit__ on
> the c++ side. Could not find any examples for this in the
> documentation.
>
> Any help appreciated.
>
> Best regards,
> Ta,
>     Avi

There's no explicit support for it, no.

Sorry,

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: [Boost-users] boost python & context manager/with statement

Trigve Siver
> From: Dave Abrahams <[hidden email]>

> To: [hidden email]
> Cc: [hidden email]
> Sent: Tuesday, February 28, 2012 10:06 PM
> Subject: Re: [Boost-users] boost python & context manager/with statement
>
>
> on Sun Feb 12 2012, Avi Bahra <avibahra-AT-googlemail.com> wrote:
>
>>  Does boost python support context management using the with
>>  statement?  In my case I need to manage __enter__/__exit__ on
>>  the c++ side. Could not find any examples for this in the
>>  documentation.
>>
>>  Any help appreciated.
>>
>>  Best regards,
>>  Ta,
>>      Avi
>
> There's no explicit support for it, no.
>
> Sorry,

But I think you could implement it like this:


 class_<SomeClass>(...) .def("__enter__", &somefunc)
.def("__exit__", &someotherfunc)
;
Trigve
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig