Re: [C++-sig] type convert: python file <-> C++ FILE*
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/
Liwei Peng <liwei_peng@yahoo.com> writes:
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.
Sorry. Try the following, which works for me: #include <boost/python.hpp> #include <stdio.h> namespace python = boost::python; struct FILE_to_pyfile { static PyObject* convert(FILE* x) { return PyFile_FromFile(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::return_by_value>()); }
Can anyone please help me out?
I hope this gets you past the problem. As I said before, these conversions should be built-in so you don't have to provide special call policies yourself. -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com
--- David Abrahams <dave@boost-consulting.com> wrote:
I hope this gets you past the problem. As I said before, these conversions should be built-in so you don't have to provide special call policies yourself.
Thanks, David! The code didn't work with my CVS branch from Oct 11, which I believe is just boost 1.29.0. But it works with gcc 3.2 and the current CVS. Ralf __________________________________________________ Do you Yahoo!? Y! Web Hosting - Let the expert host your web site http://webhosting.yahoo.com/
--- Liwei Peng <liwei_peng@yahoo.com> wrote:
FILE* getfile(const char* fname) { printf("openning file: %s\n", fname); FILE* f = fopen(fname, "w"); fprintf(f, "initial words\n"); return f; }
This doesn't work for me as well, using compiler options that work for everything else. I've verified that the custom converter works in general by changing the signature to FILE getfile(...) and return *f, but it does not work with the signature above or FILE& getfile(...). Ralf __________________________________________________ Do you Yahoo!? Y! Web Hosting - Let the expert host your web site http://webhosting.yahoo.com/
participants (3)
-
David Abrahams -
Liwei Peng -
Ralf W. Grosse-Kunstleve