ctypes shared object FILE*

D

Dog Walker

I need to call a function in a shared object with this signature:
init_dialog(FILE *input, FILE *output)
The FILE*'s are to stdin and stdout.

The call from python is libdialog.init_dialog( x, y)
I need to define x and y so that they will have the structure of
sys.stdin and sys.stdout; the called function (init_dialog) is using a
(std?) function fileno to extract the fileno from the FILE* describing
stdin and stdout.
How can I do this?
 
A

Aaron Brady

I need to call a function in a shared object with this signature:
init_dialog(FILE *input, FILE *output)
The FILE*'s are to stdin and stdout.

The call from python is libdialog.init_dialog( x, y)
I need to define x and y so that they will have the structure of
sys.stdin and sys.stdout; the called function (init_dialog) is using a
(std?) function fileno to extract the fileno from the FILE* describing
stdin and stdout.
How can I do this?

sys.stdin and sys.stdout have a 'fileno' method, which returns an
integer.

FILE* PyFile_AsFile(PyObject *p)
Return the file object associated with p as a FILE*.

This might be what you want. You need to inform the function of what
types to expect and return.
import sys
import ctypes
ctypes.pythonapi.PyFile_AsFile.argtypes= [ ctypes.py_object ]
ctypes.pythonapi.PyFile_AsFile.restype= ctypes.c_void_p
ctypes.pythonapi.PyFile_AsFile( sys.stdin ) 2019259304
ctypes.pythonapi.PyFile_AsFile( sys.stdout )
2019259336

But I'm confused why PyFile_AsFile didn't return a c_void_p as I
asked.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top