[Boost.python] Converting python file object to std::istream.
I am exposing a C++ class to python that takes a reference to a std::istream object and operates on it. It looks something like this: class StreamReader { public: StreamReader(std::istream& strm); private: std::istream& strm_; } I would like to expose this class to python allowing a python client to pass in any python file type object (regular file object, StringIO object, stdin, etc.), as a C++ client would be able to pass any std::istream derived object. Now it seems to me that someone must have encountered this problem before, but I haven't seen any references as to how to make this work. Any ideas on the best way to proceed would be appreciated. thanks, Dan
Dan wrote:
I am exposing a C++ class to python that takes a reference to a std::istream object and operates on it. It looks something like this:
class StreamReader { public: StreamReader(std::istream& strm);
private: std::istream& strm_; }
I would like to expose this class to python allowing a python client to pass in any python file type object (regular file object, StringIO object, stdin, etc.), as a C++ client would be able to pass any std::istream derived object.
Now it seems to me that someone must have encountered this problem before, but I haven't seen any references as to how to make this work.
Any ideas on the best way to proceed would be appreciated.
It seems to me the correct way to do this is to implement the streambuf interface in terms of a boost::python::object, i.e. in which the xsputn() method is implemented as calling the python object's 'write' method. I haven't seen any such implementation yet. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...
Yes, that's what I figured - I'll give it a shot. Though I think you meant the read method in my particular case; since I want an input stream object. thanks, Dan Stefan Seefeld wrote:
Dan wrote:
I am exposing a C++ class to python that takes a reference to a std::istream object and operates on it. It looks something like this:
class StreamReader { public: StreamReader(std::istream& strm);
private: std::istream& strm_; }
I would like to expose this class to python allowing a python client to pass in any python file type object (regular file object, StringIO object, stdin, etc.), as a C++ client would be able to pass any std::istream derived object.
Now it seems to me that someone must have encountered this problem before, but I haven't seen any references as to how to make this work.
Any ideas on the best way to proceed would be appreciated.
It seems to me the correct way to do this is to implement the streambuf interface in terms of a boost::python::object, i.e. in which the xsputn() method is implemented as calling the python object's 'write' method.
I haven't seen any such implementation yet.
Regards, Stefan
participants (2)
-
Dan -
Stefan Seefeld