[C++-sig] type convert: python file <-> C++ FILE*
Liwei Peng
liwei_peng at yahoo.com
Mon Oct 28 02:59:34 CET 2002
Hi,
For converting python file to/from C++ FILE*,
I tried the suggested code. It didn't work.
I got the same error message like before.
Here is my c++ code:
-----------------------------------------
#include <boost/python.hpp>
namespace python = boost::python;
struct FILE_to_pyfile
{
static PyObject* convert(FILE const& x)
{
return PyFile_FromFile((FILE*)(&x), "", "", NULL);
}
};
FILE* getfile(const char* fname)
{
printf("openning file: %s\n", fname);
FILE* f = fopen(fname, "w");
fprintf(f, "initial words\n");
return f;
}
BOOST_PYTHON_MODULE(numbermod)
{
python::to_python_converter<FILE,
FILE_to_pyfile>();
python::def("getfile", getfile,
python::return_value_policy<python::reference_existing_object>());
}
--------------------------------
The only change I made to the suggested code is
to cast "&x" (whose type is const FILE*) to 'FILE*".
Without this, the code won't compile.
Here is how I compile the code:
---------------------------------
g++ -I/usr/include/python2.2 -fPIC -c number_wrap1.C
g++ -shared number_wrap1.o -o numbermod.so
-lboost_python
--------------------------
I am using g++ 3.2, python 2.2.1.
The error message from python code:
-------------------------------------------
Python 2.2.1 (#1, Sep 9 2002, 09:26:21)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for
more information.
>>>
>>> from numbermod import *
>>> f = getfile('hello.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: bad argument type for built-in operation
>>>
Can anyone please help me out?
Thanks in advance,
liwei
__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/
More information about the Cplusplus-sig
mailing list