From: Zeeshan Quireshi on
Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
it a FILE *pointer, how do i open a file in Python and convert it to a
FILE *pointer. Or do i have to call the C library using ctypes first,
get the pointer and then pass it to my function.

Also, is there any automated way to convert c struct and enum
definitions to ctypes data types.

Zeeshan
From: geremy condra on
On Wed, Mar 3, 2010 at 6:50 PM, Zeeshan Quireshi
<zeeshan.quireshi(a)gmail.com> wrote:
> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer, how do i open a file in Python and convert it to a
> FILE *pointer. Or do i have to call the C library using ctypes first,
> get the pointer and then pass it to my function.

Something along these lines should work:

class FILE(ctypes.structure):
pass

FILE_p = ctypes.POINTER(FILE)

....but I haven't tested it.

You can also use a c_void_p.

> Also, is there any automated way to convert c struct and enum
> definitions to ctypes data types.

for structures:
http://code.activestate.com/recipes/576734-c-struct-decorator/?in=user-4170000

for functions:
http://code.activestate.com/recipes/576731-c-function-decorator/?in=user-4170000

Geremy Condra
From: Francesco Bochicchio on
On Mar 4, 12:50 am, Zeeshan Quireshi <zeeshan.quire...(a)gmail.com>
wrote:
> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer, how do i open a file in Python and convert it to a
> FILE *pointer. Or do i have to call the C library using ctypes first,
> get the pointer and then pass it to my function.
>
> Also, is there any automated way to convert c struct and enum
> definitions to ctypes data types.
>
> Zeeshan

Python file objects have a method fileno() whic returns the 'C file
descriptor', i.e. the number used by low level IO in python as well as
in C.
I would use this as interface between python and C and then in the C
function using fdopen to get a FILE * for an already open file for
which you have a file descriptor.

If you don't want change the C interface, you could try using fdopen
in python by loading the standard C library ang using ctypes
to call the function. (I tried briefly but always get 0 from fdopen ).

But if you can change the C code, why not to pass the file name? The
idea of opening the file in python and manage it in C feels a bit
icky ...

Ciao
----
FB

From: Gregory Ewing on
Francesco Bochicchio wrote:

> Python file objects have a method fileno() whic returns the 'C file
> descriptor', i.e. the number used by low level IO in python as well as
> in C.
> I would use this as interface between python and C and then in the C
> function using fdopen to get a FILE * for an already open file for
> which you have a file descriptor.

But note that this will be a *new* stdio file buffer
attached to the same file descriptor, not the same one
that the Python file object is using. This may or may
not be a problem depending on what you're trying to
do.

If you need the same FILE * that Python is using, you
may need to use ctypes to extract it out of the file
object.

--
Greg
From: Neil Hodgson on
Zeeshan Quireshi:

> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer, how do i open a file in Python and convert it to a
> FILE *pointer.

For this to work, your library should have been compiled with the
same compiler as Python and possibly the same compiler options such as
choice of runtime library. Otherwise, they may differ in the content and
layout of FILE and also in behaviour. On Unix, this may not be a problem
because of the shared runtime but on Windows it can cause crashes.

Neil