Re: [C++-sig] TypeError: No to_python (by-value) converter found for C++ type: class std::basic_ostream >
> I get this error:
> *TypeError: No to_python (by-value) converter found for C++ type: class
> std::basic_ostream >*
>
> This is the python function
> *def serialise(self, gameobj, file):
> if(gameobj in self.components):
> file.write("HEY", 3)*
>
> What's the problem here? Is is because the ostream argument is an reference?
The problem is you are trying to copy a noncopyable object:
return class_,
boost::noncopyable>("ostream", no_init)
ostream is noncopyable
.def("write", write ,
return_value_policy())
but you define write to return copy (of ostream)
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Read and write byte streams python<-->c++
Hey,
I'm trying to serialise some data. I have a c++ class:
class ostream
{
std::ostream& stream;
public:
ostream(std::ostream& s) : stream(s) {}
void write(const char* bytes, Uint32 count)
{
stream.write(bytes, count);
}
};
As you can see, it only wraps std::ostream.
I call the python function like this:
ostream stream(file);
GameObject* obj = new GameObject(...);
if(boost::python::override f = get_override("serialise"))
return f(boost::python::object(boost::python::ptr(obj)),
boost::python::object(boost::python::ptr(&stream)));
Then in the python script:
def serialise(self, gameobj, file):
if(gameobj in self.components):
b = pickle.dumps(self.components[gameobj])
# this is some testing code and dont really make sense because I
don't know what to do...
file.write(str(len(str(b))), 10) # try to serialise an int that
tells the size of the byte-object
print(len(str(b)))
file.write(str(b), len(str(b))) # write the byte object
return len(str(b)) + 10 # return amount of bytes we wrote
First i "pickle" a python object. I convert that byte-object to a string
since my write() function in c++ takes a const char*. But i'd like to pass
the byte-object directly since const char* is basically a byte, right?
Also, the other problem, when im about to read the serialised data I need to
know the size of the previously serialised data to unpickle it. I thoght
that I could to the c++ way and add a integer before the byte stream that
reveal how big it is. But How can i serialise an int from python that takes
up constant "byte-size" for an integer between lets say 0-1!?
Any help on this one? Thanks!
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] TypeError: No to_python (by-value) converter found for C++ type: class std::basic_ostream >
Thank you for the answer. Is there any way I can prevent it from copying the
stream object even though std::ostream::write() returns a std::ostream& ?
On Wed, Sep 29, 2010 at 11:38 AM, Jakub Zytka wrote:
>
> > I get this error:
> > *TypeError: No to_python (by-value) converter found for C++ type: class
> > std::basic_ostream >*
> >
> > This is the python function
> > *def serialise(self, gameobj, file):
> > if(gameobj in self.components):
> > file.write("HEY", 3)*
> >
> > What's the problem here? Is is because the ostream argument is an
> reference?
> The problem is you are trying to copy a noncopyable object:
>
>
> return class_,
>> boost::noncopyable>("ostream", no_init)
>>
> ostream is noncopyable
>
>
> .def("write", write ,
>> return_value_policy())
>>
> but you define write to return copy (of ostream)
>
> ___
> 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] TypeError: No to_python (by-value) converter found for C++ type: class std::basic_ostream >
On 09/29/10 14:11, Simon W wrote:
Thank you for the answer. Is there any way I can prevent it from copying the
stream object even though std::ostream::write() returns a std::ostream& ?
You can use other return policy, or define one on your own.
http://www.boost.org/doc/libs/1_44_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies
But honestly, I do not think it is the correct way to go. Read below.
> I'm trying to serialise some data. I have a c++ class:
> class ostream
> {
> ...
> could to the c++ way and add a integer before the byte stream that reveal how
> big it is. But How can i serialise an int from python that takes up constant
> "byte-size" for an integer between lets say 0-1!?
Everything can be done, but I have a different question:
I haven't followed all your previous emails, but do you *really* need to
implement serialization code in python?
Somehow I feel it is very weird that on one hand you want to have serialization
code in python, and yet use ostream.
What would be wrong with coding serialization purely in C++, and then providing
appropriate pickle suite to python:
struct MyPickleSuite
{
// ...
boost::python::tuple MyPickleSuite::getstate(MyClass & object)
{
std::ostringstream oStream(ios_base::binary);
serialize(oStream, object);
return boost::python::make_tuple(oStream.str());
}
void MyPickleSuite::setstate(MyClass & object, boost::python::tuple state)
{
// verify tuple correctness etc.
std::string serializedData = extract(state[0]);
std::istringstream iStream(serializedData, ios_base::binary);
serialize(iStream, object); // NOTE: you can use the same code for serialization
and deserialization. stream type should determine actual behavior
}
}
That way you do not have to expose ostream to python at all. In fact if you
wanted to switch to another type of stream (imagine that you have a custom
stream implementation, which provides eg. type checking) it is cheap. You only
change a few functions in your pickle suite, nothing more.
Even if you do really need to define serialization in python I see no reason to
play with streams. I'd rather had a wrapper which hides underlying
implementation (eg. std::streams) and operates on strings.
Please reconsider your design.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Derivation from Python str
I'd like to derive from either Python's built-in 'str' or
boost::python::str, resulting in a class that provides additional
methods / member functions / attributes. The definitions of these
additional methods are implemented in C++. Is there any way to achieve
that? Please note that implicit conversions don't serve this goal, as
methods of 'str' should be part of the derived classes set of attributes
- e.g. the following Python code should be ok ('Derived' being the class
derived from Python's 'str'):
>>> Derived d
>>> d.startswith('prefix')
I see that Python's 'str' is immutable, which is acceptable for my purpose.
What I tried:
Including e.g. boost::python::str in the list of base classes yields a
run-time error in python:
boost::python::class_ >("Derived")
.def("foo",&Derived::foo)
;
Including std::string (which is built-in-converted to Python's 'str') in
the list of base classes yields a run-time assertion complaining about
the base class not having been wrapped. What I'd like to achieve anyway,
is derivation from Python's 'str', not std::string.
Thanks for any help!
Cheers, Willi.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Derivation from Python str
Am Mittwoch 29 September 2010, 17:02:57 schrieb Willi Karel: > I'd like to derive from either Python's built-in 'str' or > boost::python::str, resulting in a class that provides additional > methods / member functions / attributes. [...] AFAIK this is just an unsupported feature of the BPL. I was (and would still be) interested in the same thing, only with list/dict being the base classes, but this does not seem to be possible. >From the outside, I would guess/hope that it's not too hard to fix, but having looked at various parts of boost::python in the past, I don't think that I will try it myself in the near future. > Including e.g. boost::python::str in the list of base classes yields a > run-time error in python: [...] If it was supported, I would say that this would be the right approach/API. Have a nice day, Hans ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Derivation from Python str
On 09/29/2010 09:01 AM, Hans Meine wrote: Am Mittwoch 29 September 2010, 17:02:57 schrieb Willi Karel: I'd like to derive from either Python's built-in 'str' or boost::python::str, resulting in a class that provides additional methods / member functions / attributes. [...] AFAIK this is just an unsupported feature of the BPL. I was (and would still be) interested in the same thing, only with list/dict being the base classes, but this does not seem to be possible. From the outside, I would guess/hope that it's not too hard to fix, but having looked at various parts of boost::python in the past, I don't think that I will try it myself in the near future. Including e.g. boost::python::str in the list of base classes yields a run-time error in python: [...] If it was supported, I would say that this would be the right approach/API. If you *really* need to subclass Python's str type (so your objects pass an "isinstance(obj, str)" check, it is possible to do that in a way that is interoperable with Boost.Python (you'd be able to hold such an object in boost::python::str, for instance), but you'd have to use a lot of the raw Python C API to do it. It would certainly be harder than just adding str-like methods to a regular Boost.Python class. Jim Bosch ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Derivation from Python str
Dear Hans and Jim, thanks a lot for your replies! I tried to wrap/def boost::python::str's methods for my derived class, which is possible in a quite compact way using boost::function_types and some templates. However, I quit my attempt when it came to operators - too much work for now. Cheers, Willi. Jim Bosch wrote: > On 09/29/2010 09:01 AM, Hans Meine wrote: >> Am Mittwoch 29 September 2010, 17:02:57 schrieb Willi Karel: >>> I'd like to derive from either Python's built-in 'str' or >>> boost::python::str, resulting in a class that provides additional >>> methods / member functions / attributes. [...] >> >> AFAIK this is just an unsupported feature of the BPL. I was (and >> would still >> be) interested in the same thing, only with list/dict being the base >> classes, >> but this does not seem to be possible. >> >>> From the outside, I would guess/hope that it's not too hard to fix, >>> but having >> looked at various parts of boost::python in the past, I don't think >> that I >> will try it myself in the near future. >> >>> Including e.g. boost::python::str in the list of base classes yields a >>> run-time error in python: [...] >> >> If it was supported, I would say that this would be the right >> approach/API. >> > > If you *really* need to subclass Python's str type (so your objects pass > an "isinstance(obj, str)" check, it is possible to do that in a way that > is interoperable with Boost.Python (you'd be able to hold such an object > in boost::python::str, for instance), but you'd have to use a lot of the > raw Python C API to do it. It would certainly be harder than just > adding str-like methods to a regular Boost.Python class. > > Jim Bosch > ___ > Cplusplus-sig mailing list > [email protected] > http://mail.python.org/mailman/listinfo/cplusplus-sig -- __ Technische Universität Wien DI Wilfried Karel /_// // __/ Institut für Photogrammetrie / // / // /_ und Fernerkundung (E122) [email protected] / // __// __/ Gußhausstraße 27-29 +43 1 58801 12244 /_//_/ /_/A-1040 Wien ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
