From: Bill S. <wf...@sa...> - 2006-12-18 03:52:08
|
I ran into the similar problem with ostreams. My solution (after some googling) was to define a hybrid FILE/streambuf class: class FILEstream : public std::streambuf { public: FILEstream(std::FILE* file): self_file(file) {} protected: std::streambuf::int_type overflow(std::streambuf::int_type c) { return std::fputc(c, self_file) == EOF? std::streambuf::traits_type::eof(): c; } FILE* self_file; }; I only had one method that used the ostream, so I used %ignore and % extend to apply it, but you could write a typemap to be more general. Use PyFile_Check() and PyFile_AsFile() to obtain a FILE*, which can then be used to construct a FILEstream. I haven't used it with istreams, so I can't make any guarantees, but hopefully this will at least provide a starting point. On Dec 17, 2006, at 2:08 PM, Noel O'Boyle wrote: > Dear SWIGers, > > I'm trying to create Python bindings for a C++ class with the > following constructor: > > OBConversion (std::istream * is = NULL) > > Is it possible to create a std::stream from Python, e.g. from a > file-like object? > > I've tried %include "std_iostream.i", but when I try to create an > istream object I get: > > mymodule.istream(myfileobject) > .... > TypeError: in method 'new_istream', argument 1 of type > 'std::basic_streambut<char,std::char_traits<char > > *' > > Any ideas where to go from here? > > Noel > > ---------------------------------------------------------------------- > --- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to > share your > opinions on IT & business topics through brief surveys - and earn cash > http://www.techsay.com/default.php? > page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Swig-user mailing list > Swi...@li... > https://lists.sourceforge.net/lists/listinfo/swig-user ** Bill Spotz ** ** Sandia National Laboratories Voice: (505)845-0170 ** ** P.O. Box 5800 Fax: (505)284-5451 ** ** Albuquerque, NM 87185-0370 Email: wf...@sa... ** |