logo       

Re: Re: using stdout/stderr with python: msg#00155

programming.swig

Subject: Re: Re: using stdout/stderr with python

Luigi:

Could you add the attached files to CVS?

file.i -> Lib/python
file_test.i, file_test_runme.py -> Examples/test-suite/python

I still don't know how to make the new CVS rep. work for me.

Anyway, the file.i should be used as usual


%module mymodule
...
%include file.i
...

int myfunc(FILE *file);
...

and it will provide the proper 'in' typemaps. Then you can call:

import sys
import mymodule
....
mymodule.myfunc(sys.stdout)



See the file_test_runme.py for more examples.

Marcelo


Luigi Ballabio wrote:

On 2004.05.25 17:48, Kevin Smith wrote:

Martin F Krafft wrote:

Some of my functions take a FILE*. I understand how one can
provide wrappers or a typemap to deal with that, but what if
I want to pass stdout and stderr into these functions as the FILE*
arguments? I could not find a way to do this.


One way would be to create C functions like GetStdin and GetStdout, and wrap them. This way, from Python, you could get access to FILE* versions of those streams. Then, you can pass those values back into your other functions.


This wouldn't help when next he wants to pass a Python file handle created with open(). From a quick look at the Python/C API (see
http://www.python.org/doc/2.3.3/api/fileObjects.html) the following might work:

%typemap(in) FILE* {
if (PyFile_Check($input)) {
$1 = PyFile_AsFile($input);
} else {
SWIG_exception(SWIG_TypeError, "file expected"),
}
}

but I haven't tested it, so I don't guarantee that it won't cause scripts to crash horribly.

Hope this helps,
Luigi
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig


/*
Typemaps for FILE*

From the ideas of Luigi
luigi.ballabio@xxxxxxxxxxxxx
*/

%types(FILE *);

/* defining basic methods */
%fragment("SWIG_AsValFilePtr","header") {
SWIGSTATICINLINE(int)
SWIG_AsValFilePtr(PyObject *obj, FILE **val) {
static swig_type_info* desc = 0;
FILE *ptr = 0;
if (!desc) desc = SWIG_TypeQuery("FILE *");
if ((SWIG_ConvertPtr(obj,(void **)(&ptr), desc, 0)) != -1) {
if (val) *val = ptr;
return 1;
}
if (PyFile_Check(obj)) {
if (val) *val = PyFile_AsFile(obj);
return 1;
}
if (val) PyErr_SetString(PyExc_TypeError, "a FILE* is expected");
return 0;
}
}

%fragment("SWIG_AsFilePtr","header",fragment="SWIG_AsValFilePtr") {
SWIGSTATICINLINE(FILE*)
SWIG_AsFilePtr(PyObject *obj) {
FILE *val = 0;
SWIG_AsValFilePtr(obj, &val);
return val;
}
}

%fragment("SWIG_CheckFilePtr","header",fragment="SWIG_AsValFilePtr") {
SWIGSTATICINLINE(int)
SWIG_CheckFilePtr(PyObject *obj) {
return SWIG_AsValFilePtr(obj, (FILE **)(0));
}
}

/* defining the typemaps */
%typemap_ascheck(SWIG_CCode(POINTER), SWIG_AsFilePtr, SWIG_CheckFilePtr,
"SWIG_AsFilePtr", "SWIG_CheckFilePtr", FILE*);
%module file_test

%include file.i


%inline
%{
int nfile(FILE *file) {
printf("hello %p\n", (void*)file);
if (file) {
// fprintf(file,"hello\n");
}
return 0;
}

int nfile(const char *filename) {
FILE *file = fopen(filename,"w");
nfile(file);
fclose(file);
return 0;
}

FILE* GetStdOut() {
return stdout;
}

%}

import sys
import file_test

file_test.nfile(sys.stdout)

cstdout = file_test.GetStdOut()

file_test.nfile(cstdout)
file_test.nfile("test.dat")
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise