[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Passing FILE * types using ctypes

Zeeshan Quireshi

3/3/2010 11:50:00 PM

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
7 Answers

geremy condra

3/4/2010 2:05:00 AM

0

On Wed, Mar 3, 2010 at 6:50 PM, Zeeshan Quireshi
<zeeshan.quireshi@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=us...

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

Geremy Condra

Francesco Bochicchio

3/4/2010 1:46:00 PM

0

On Mar 4, 12:50 am, Zeeshan Quireshi <zeeshan.quire...@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

Gregory Ewing

3/5/2010 8:10:00 AM

0

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

Neil Hodgson

3/5/2010 9:46:00 PM

0

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

Lawrence D'Oliveiro

3/10/2010 9:57:00 PM

0

In message <7vbrveFegeU1@mid.individual.net>, Gregory Ewing wrote:

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

Why would Python be using a FILE *?

Lawrence D'Oliveiro

3/10/2010 9:59:00 PM

0

In message <a83319d7-c199-4532-9816-
d002f7fd7914@q16g2000yqq.googlegroups.com>, Zeeshan Quireshi wrote:

> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer ...

Another option is to fix your library not to use stdio directly.

Robert Kern

3/10/2010 10:09:00 PM

0

On 2010-03-10 15:57 PM, Lawrence D'Oliveiro wrote:
> In message<7vbrveFegeU1@mid.individual.net>, Gregory Ewing wrote:
>
>> If you need the same FILE * that Python is using, you
>> may need to use ctypes to extract it out of the file
>> object.
>
> Why would Python be using a FILE *?

In Python 2.x, Python's file objects use a stdio FILE* pointer underneath. The
I/O subsystem was rewritten in Python 3.x to use only file descriptors.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco