I am using ctypes to wrap a C-library (which I have control over) with Python. I want to wrap a C-function with declaration:
int fread_int( FILE * stream );
Tried this:
#if PY_MAJOR_VERSION >= 3
if (PyObject_HasAttrString(pyfile, "fileno")) {
int fd = (int)PyLong_AsLong(PyObject_CallMethod(pyfile, "fileno", NULL));
if (PyObject_HasAttrString(pyfile, "mode")) {
char *mode = PyUnicode_AsUTF8AndSize(
PyObject_CallMethod(pyfile, "mode", NULL), NULL);
fp = fdopen(fd, mode);
}
else {
return PyErr_Format(PyExc_ValueError,
"File doesn’t have mode attribute!");
}
}
else {
return PyErr_Format(PyExc_ValueError,
"File doesn’t have fileno method!");
}
#else
fp = PyFile_AsFile(pyfile);
#endif
It looks like it might be working.