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

Pearu Peterson pearu at cens.ioc.ee
Sat Jun 1 22:19:49 CEST 2002


Hi,

Say, a class A has a method
  
  void A::output(std::ostream & os);

that sends lots of junk to `os'. My question is how to wrap such a method
with BPL V2 that would accept a Python file instance as an argument?
 
For simplicity, let's assume that the file is already opened in Python
side. Now one can write the following wrapper for this method but I could
not figure out how to make the connection between 
  FILE* f;
and 
  std::ostream os;
when using gcc 3.x compiler.

  void A_output(const A & a, PyObject* fp) {
    if (!PyFile_Check(fp)) {
      ...
      throw boost::python::error_already_set();
    }
    FILE* f = PyFile_AsFile(fp);
    // ??? Need something like std::ostream os(f); here. 
    a.output(os);
  }

I tried google but there I got very contradictionary results. E.g. in
gcc-2.95.3 there is ostdiostream to make this connection but in gcc-3.x
ostdiostream does not exist anymore.

Any hints how to solve this issue are very much appeciated.

Pearu






More information about the Cplusplus-sig mailing list