Ticket #6831: runner-cffi.patch

File runner-cffi.patch, 3.5 KB (added by Jean-Paul Calderone, 7 years ago)

a port of twisted.runner.portmap to cffi

  • twisted/runner/portmap.c

     
    1 /*
    2  * Copyright (c) 2001-2004 Twisted Matrix Laboratories.
    3  * See LICENSE for details.
    4 
    5  *
    6  */
    7 
    8 /* portmap.c: A simple Python wrapper for pmap_set(3) and pmap_unset(3) */
    9 
    10 #include <Python.h>
    11 #include <rpc/rpc.h>
    12 #include <rpc/pmap_clnt.h>
    13 
    14 static PyObject * portmap_set(PyObject *self, PyObject *args)
    15 {
    16         unsigned long program, version;
    17         int protocol;
    18         unsigned short port;
    19        
    20         if (!PyArg_ParseTuple(args, "llih:set",
    21                               &program, &version, &protocol, &port))
    22                 return NULL;
    23 
    24         pmap_unset(program, version);
    25         pmap_set(program, version, protocol, port);
    26        
    27         Py_INCREF(Py_None);
    28         return Py_None;
    29 }
    30 
    31 static PyObject * portmap_unset(PyObject *self, PyObject *args)
    32 {
    33         unsigned long program, version;
    34        
    35         if (!PyArg_ParseTuple(args, "ll:unset",
    36                               &program, &version))
    37                 return NULL;
    38 
    39         pmap_unset(program, version);
    40        
    41         Py_INCREF(Py_None);
    42         return Py_None;
    43 }
    44 
    45 static PyMethodDef PortmapMethods[] = {
    46         {"set", portmap_set, METH_VARARGS,
    47          "Set an entry in the portmapper."},
    48         {"unset", portmap_unset, METH_VARARGS,
    49          "Unset an entry in the portmapper."},
    50         {NULL, NULL, 0, NULL}
    51 };
    52 
    53 void initportmap(void)
    54 {
    55         (void) Py_InitModule("portmap", PortmapMethods);
    56 }
    57 
  • twisted/runner/topfiles/setup.py

     
    22# See LICENSE for details.
    33
    44try:
    5     from twisted.python.dist import setup, ConditionalExtension as Extension
     5    from twisted.python.dist import setup
    66except ImportError:
    77    raise SystemExit("twisted.python.dist module not found.  Make sure you "
    88                     "have installed the Twisted core package before "
    99                     "attempting to install any other Twisted projects.")
    1010
    11 extensions = [
    12     Extension("twisted.runner.portmap",
    13               ["twisted/runner/portmap.c"],
    14               condition=lambda builder: builder._check_header("rpc/rpc.h")),
    15 ]
    16 
    1711if __name__ == '__main__':
    1812    setup(
    1913        twisted_subproject="runner",
     
    3024Twisted Runner contains code useful for persistent process management
    3125with Python and Twisted, and has an almost full replacement for inetd.
    3226""",
    33         # build stuff
    34         conditionalExtensions=extensions,
    3527    )
  • twisted/runner/portmap.py

     
     1# Copyright (c) Twisted Matrix Laboratories.
     2# See LICENSE for details.
     3
     4"""
     5A simple Python wrapper for pmap_set(3) and pmap_unset(3).
     6"""
     7
     8__all__ = ["set", "unset"]
     9
     10import cffi
     11
     12source = """
     13    typedef int bool_t;
     14    bool_t pmap_set(unsigned long prognum, unsigned long versnum, unsigned int protocol, unsigned short port);
     15    bool_t pmap_unset(unsigned long prognum, unsigned long versnum);
     16"""
     17
     18_ffi = cffi.FFI()
     19_ffi.cdef(source)
     20_lib = _ffi.verify(source="#include <rpc/rpc.h>\n" + source)
     21
     22
     23def set(program, version, protocol, port):
     24    """
     25    Set an entry in the portmapper.
     26    """
     27    return _lib.pmap_set(program, version, protocol, port)
     28
     29
     30
     31def unset(program, version):
     32    """
     33    Unset an entry in the portmapper.
     34    """
     35    return _lib.pmap_unset(program, version)