Passing FILE * types using ctypes

  • Thread starter Zeeshan Quireshi
  • Start date
Z

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. 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
 
G

geremy condra

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
 
F

Francesco Bochicchio

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
 
G

Gregory Ewing

Francesco said:
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.
 
N

Neil Hodgson

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
 
L

Lawrence D'Oliveiro

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 *?
 
L

Lawrence D'Oliveiro

In message <a83319d7-c199-4532-9816-
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.
 
R

Robert Kern

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top