[C++-sig] python file object and std::istream / std::ifstream

François Duranleau duranlef at iro.umontreal.ca
Tue Jul 10 21:07:55 CEST 2007


On Mon, 9 Jul 2007, Nat Goodspeed wrote:

>> -----Original Message-----
>> From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org]
>> On Behalf Of Dr. Johannes Zellner
>>
>> I'd like to make a c++ method
>>
>> void read(std::istream& in)
>> {
>> }
>>
>> available for in python so that I can pass python file objects.
>
> [Nat] It sounds as though you want Python code to be able to manipulate
> C++ stream objects. Is that correct?

[snip]

As I understand, it's more like the opposite: the OP wants to manipulate 
Python file objects on the C++ side with a istream interface. In that 
case, I would create a source device class (Source concept from 
Boost.Iostreams) wrapping calls to PyFile_*, and then wrap the func like 
this:

void read_wrap( boost::python::object in_file )
{
     boost::iostreams::stream< pyfile_device_wrapper >
         in( pyfile_device_wrapper( in_file ) ) ;
     read( in ) ;
}

You can read the documentation on Python file objects and Boost.Iostreams 
to write the pyfile_device_wrapper. It should fairly simple.

-- 
François Duranleau
LIGUM, Université de Montréal


More information about the Cplusplus-sig mailing list