Re: [C++-sig] shared_ptr and register_ptr_to_python
On 11.10.2010 22:01, Jim Bosch wrote: A couple of notes: - I have not tested this. Likely there are some typos, and possibly even a bit of forgotten syntax - hopefully it's easy to correct. - Note that there's no need to re-wrap the methods that B inherits from A, but you do have create both PyA and PyB if you want both A and B to be instantiable from Python (after all, getSelf() is pure virtual). - If you have a C++ function that returns an A or B instance by reference or pointer, this code will still work. Of course, such an object will have to find some other way to implement getSelf() - only objects instantiated in Python will actually be PyA or PyB instances. I still don't think it solves my problem as in original case the objects A and B are created only under C++. I would like to pass the pointer of such object, insted of copying them into Queue.Queue in Python and you wrote that the objects (A,B) need to be instantiated under Python...This time it's more like bad Queue.Queue implementation, not boost.python (as CallPolicies don't work). Anyway, thank you for you help, I appreciate... -- pozdrawiam Marek Denis [[email protected]] ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Wrapping method that returns const int& (boost::python)
I have a simple test case:
class Foo
{
public:
const int& getX() const {return x;}
void setX(const int& x_) {x = x_;}
private:
int x;
};
BOOST_PYTHON_MODULE(module)
{
class_("Foo")
.def("getX", &Foo::getX)
;
}
The problem is that I cannot wrap getX and I get a compiler error. How
is it possible to wrap getX WITHOUT changing anything in Foo?
Thanks in advance!
Panos Christopoulos Charitos
www.ancient-ritual.com
www.anki3d.org
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Wrapping method that returns const int& (boost::python)
Panos wrote:
I have a simple test case:
class Foo
{
public:
const int& getX() const {return x;}
void setX(const int& x_) {x = x_;}
private:
int x;
};
BOOST_PYTHON_MODULE(module)
{
class_("Foo")
.def("getX", &Foo::getX)
;
}
The problem is that I cannot wrap getX and I get a compiler error. How
is it possible to wrap getX WITHOUT changing anything in Foo?
Thanks in advance!
Panos Christopoulos Charitos
www.ancient-ritual.com
www.anki3d.org
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Hi!
I totally affiliate myself to this problem.
The only difference is that i do not return a const reference but rather
a reference.
So maybe we can find a solution for this together :-)
Greetings!
--
Erik Tuerke
Department of Neurophysics
Max-Planck-Institute for Human Cognitive and Brain Sciences
Stephanstrasse 1A
04103 Leipzig
Germany
Tel: +49 341 99 40-2440
Email: [email protected]
www.cbs.mpg.de
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Delete objects, remove from dict
Hey and thanks for the answer! Unfortunately that doesn't seem to work. I'm
using boost.python for Python 3 ( boost python 1.44? )
error C2039: 'del' : is not a member of 'boost::python::api::object'
I've had a look on the reference manual but I don't understand if it's a
free function or an method of object() ? Neither work ..
The code i'm using:
*object obj = mMainNamespace[ name.c_str() ];
obj.del();*
// Simon
On Mon, Oct 25, 2010 at 1:26 AM, Ralf W. Grosse-Kunstleve wrote:
> > namespace.remove("test"); // <- how can I do something like this with
> boost
> >python?
>
> namespace["test"].del();
>
> See also:
>
> http://www.boost.org/doc/libs/1_44_0/libs/python/doc/v2/object.html
>
> Ralf
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Delete objects, remove from dict
I've only tried it with Python 2; I'm not setup for working with Python 3.
Try
boost::python::api::delitem(target, key);
and if that fails too
PyObject_DelItem(target.ptr(), key.ptr());
Ralf
>
>From: Simon W
>To: Development of Python/C++ integration
>Sent: Mon, October 25, 2010 8:48:27 AM
>Subject: Re: [C++-sig] Delete objects, remove from dict
>
>Hey and thanks for the answer! Unfortunately that doesn't seem to work. I'm
>using boost.python for Python 3 ( boost python 1.44? )
>
>error C2039: 'del' : is not a member of 'boost::python::api::object'
>
>I've had a look on the reference manual but I don't understand if it's a free
>function or an method of object() ? Neither work ..
>
>The code i'm using:
>object obj = mMainNamespace[ name.c_str() ];
>obj.del();
>
>// Simon
>
>
>On Mon, Oct 25, 2010 at 1:26 AM, Ralf W. Grosse-Kunstleve
>wrote:
>
>> namespace.remove("test"); // <- how can I do something like this with boost
>>>python?
>>
>>namespace["test"].del();
>>
>>See also:
>>
>>http://www.boost.org/doc/libs/1_44_0/libs/python/doc/v2/object.html
>>
>>Ralf
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Delete objects, remove from dict
Oh, at second glance... > > object obj = mMainNamespace[ name.c_str() ]; > >obj.del(); This cannot work! You need to do it the way I showed before. mMainNamespace[ name.c_str() ].del(); The [] operator returns a proxy object which supports the del since it still knows what the target object is. Once you've assigned the proxy object to boost::python::object the target information is lost. Ralf ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Delete objects, remove from dict
Thanks that did the trick! On Mon, Oct 25, 2010 at 7:07 PM, Ralf W. Grosse-Kunstleve wrote: > Oh, at second glance... > > > > > > object obj = mMainNamespace[ name.c_str() ]; > > >obj.del(); > > This cannot work! > You need to do it the way I showed before. > > mMainNamespace[ name.c_str() ].del(); > > The [] operator returns a proxy object which supports the del since > it still knows what the target object is. Once you've assigned the > proxy object to boost::python::object the target information is lost. > > Ralf > ___ > Cplusplus-sig mailing list > [email protected] > http://mail.python.org/mailman/listinfo/cplusplus-sig > ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Reference count on class object and import()
Hey again,
I'm trying to implement a load / unload module functionallity. When I delete
the module in the __main__ dict the module's reference count is still > 1.
I've been trying to find if I reference it anywhere else in my code but I
can't find anything! When I did a check in vc++ debugger found something.
When I run this code:
object obj = import(name.c_str());
*obj*'s *ob_refcnt* member is 2 when I check in the vc++ debugger. Shouldn't
it be 1?
After that line, I put it in the __main__ dict like:
mMainNamespace[ name.c_str() ] = obj;
The reference count shows 4, as expected.
When I'm entering my unload() function when I want to remove the module I do
like:
void unload()
{
object t = mMainNamespace[ name.c_str() ];
// reference count in t is now 4 ?
mMainNamespace[ name.c_str() ].del(); // I delete but it's not unloaded
properly because it's still referenced somewhere ..
}
What does import() do? It must save some reference somewhere?
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Mapping RAW memory data into Python
Hi, While I had some questions concerning accessing objects in Python, that were previously created in C++ now I'd like to know whether it's possible to get access to the raw memory from Python layer. Let's suppose I have the char* ptr pointer that points to the memory chunk with IPv4 packet. I would like to be able to read (and preferably write) data from Python layer (not via wrapped object that contains char* ptr and has some methods to write/read data), but without making any memory copies. I can use struct (even define format dynamically) in Python, but I don't know how do I pass the proper pointer from C++. If I use boost::python::object I had problems that script didn't know char type. I can use boost::python::str, return it and use in Python but I suppose it does copy data into str object, right? If not Boost, maybe basic Python C API would work? BTW. Suppose I had char* txt = "some string"; How do i create boost::python::object() that would containt the whole string? Passing *txt or txt into boost::python::object(*txt) didn't work. -- regards Marek Denis ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Wrapping method that returns const int& (boost::python)
Does this works for you??
class Foo
{
public:
const int& getX() const {return x;}
void setX(const int& x_) {x = x_;}
private:
int x;
};
BOOST_PYTHON_MODULE(module)
{
class_("Foo")
.def("getX", &Foo::getX, return_value_policy())
;
}
With Regards,
Abhishek Srivastava
-Original Message-
From: [email protected] on
behalf of Panos
Sent: Mon 10/25/2010 7:04 PM
To: [email protected]
Subject: [C++-sig] Wrapping method that returns const int& (boost::python)
I have a simple test case:
class Foo
{
public:
const int& getX() const {return x;}
void setX(const int& x_) {x = x_;}
private:
int x;
};
BOOST_PYTHON_MODULE(module)
{
class_("Foo")
.def("getX", &Foo::getX)
;
}
The problem is that I cannot wrap getX and I get a compiler error. How
is it possible to wrap getX WITHOUT changing anything in Foo?
Thanks in advance!
Panos Christopoulos Charitos
www.ancient-ritual.com
www.anki3d.org
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
DISCLAIMER:
---
The contents of this e-mail and any attachment(s) are confidential and
intended
for the named recipient(s) only.
It shall not attach any liability on the originator or NECHCL or its
affiliates. Any views or opinions presented in
this email are solely those of the author and may not necessarily reflect the
opinions of NECHCL or its affiliates.
Any form of reproduction, dissemination, copying, disclosure, modification,
distribution and / or publication of
this message without the prior written consent of the author of this e-mail is
strictly prohibited. If you have
received this email in error please delete it and notify the sender
immediately. .
---___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Mapping RAW memory data into Python
On 10/25/2010 10:51 AM, Marek Denis wrote: Hi, While I had some questions concerning accessing objects in Python, that were previously created in C++ now I'd like to know whether it's possible to get access to the raw memory from Python layer. Let's suppose I have the char* ptr pointer that points to the memory chunk with IPv4 packet. I would like to be able to read (and preferably write) data from Python layer (not via wrapped object that contains char* ptr and has some methods to write/read data), but without making any memory copies. I can use struct (even define format dynamically) in Python, but I don't know how do I pass the proper pointer from C++. If I use boost::python::object I had problems that script didn't know char type. I can use boost::python::str, return it and use in Python but I suppose it does copy data into str object, right? If not Boost, maybe basic Python C API would work? BTW. Suppose I had char* txt = "some string"; How do i create boost::python::object() that would containt the whole string? Passing *txt or txt into boost::python::object(*txt) didn't work. If it's okay for that data to be totally opaque in Python, then you probably want to look into PyCObject in the Python C API. The Python str type does not support pointing at memory it doesn't own, so it sounds like that isn't an option for you. If you want to interpret it in some way so that native Python code can understand it, you'll need to use Numpy. I've written a low-level Boost.Python interface to Numpy that you might find useful in that case here: http://svn.boost.org/svn/boost/sandbox/numpy/ ...but it will still be pretty complicated. Passing raw memory into Python is hard precisely because it's very hard to do the memory management in a safe way. Jim Bosch ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Wrapping method that returns const int& (boost::python)
Panos, Erik, the issue here is likely that Python's "int" objects are immutable. Therefore, you can't pass around ints by reference, only by-value. If the compiler is confused, perhaps you need to explicitly set a return-value policy that makes this clear ? Stefan -- ...ich hab' noch einen Koffer in Berlin... ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
