[C++-sig] V2: python file object as an ostream argument

David Abrahams david.abrahams at rcn.com
Sun Jun 2 00:44:29 CEST 2002


From: "Pearu Peterson" <pearu at cens.ioc.ee>

> Thanks for the references.
>
> Following (a rather buggy) example in
>
>
http://groups.google.com/groups?hl=en&lr=&safe=off&selm=86mtsc%248sh%241%40
nnrp1.deja.com&rnum=4
>
> I solved this issue as follows:
> /////////////////////////////
> #include <streambuf>
>
>   class std_obuf: public std::streambuf {
>   public:
>     std_obuf(std::FILE* file): m_file(file) {}
>   protected:
>     std::streambuf::int_type overflow(std::streambuf::int_type c) {
>       return std::fputc(c, m_file) ==EOF?
std::streambuf::traits_type::eof(): c;
>     }
>     FILE* m_file;
>   };
>
>   void A_output(const A & a, PyObject* fp) {
>     if (!PyFile_Check(fp)) {
>       ...
>       throw boost::python::error_already_set();
>     }
>     std::FILE* f = PyFile_AsFile(fp);
>     std_obuf buf(f);
>     std::ostream os(&buf);
>     a.output(os);
>   }
> ///////////////////////////////

That's great! Are you planning to submit this, enchanced to handle input as
well, to boost? Or would you please bug Dietmar about it until he submits
something as promised in his post from more than 2 years ago?

-Dave







More information about the Cplusplus-sig mailing list