-
Notifications
You must be signed in to change notification settings - Fork 0
/
flingfdmodule.c
58 lines (46 loc) · 1.38 KB
/
flingfdmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <Python.h>
#include "libflingfd/src/flingfd.h"
static PyObject * pyflingfd_simple_send( PyObject *self, PyObject *args ) {
const char *path;
PyObject *fp;
int fd;
if ( !PyArg_ParseTuple( args, "sO", &path, &fp ) )
return NULL;
#if PY_MAJOR_VERSION >= 3
fd = PyObject_AsFileDescriptor( fp );
if ( fd == -1 )
return PyErr_SetFromErrno( PyExc_IOError );
#else
if ( !PyFile_Check( fp ) ) {
PyErr_SetString( PyExc_IOError, "Not a file object" );
return NULL;
}
fd = fileno( PyFile_AsFile( fp ) );
if ( fd == -1 )
return PyErr_SetFromErrno( PyExc_IOError );
#endif
if ( flingfd_simple_send( path, fd ) )
Py_RETURN_TRUE;
PyErr_SetFromErrno( PyExc_IOError );
return NULL;
}
static PyObject * pyflingfd_simple_recv( PyObject *self, PyObject *args ) {
const char *path;
int fd;
if ( !PyArg_ParseTuple( args, "s", &path ) )
return NULL;
if ( ( fd = flingfd_simple_recv( path ) ) != -1 )
return Py_BuildValue( "i", fd );
PyErr_SetFromErrno( PyExc_IOError );
return NULL;
}
static PyMethodDef FlingFDMethods[] = {
{ "simple_send", pyflingfd_simple_send, METH_VARARGS,
"Sends a single file descriptor to a receiver." },
{ "simple_recv", pyflingfd_simple_recv, METH_VARARGS,
"Receives a single file descriptor from a sender." },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initflingfd( void ) {
(void)Py_InitModule( "flingfd", FlingFDMethods );
}