Antonio Cuni avatar Antonio Cuni committed 5b3cde9

initial release

Comments (0)

Files changed (4)

+from setuptools import setup, find_packages
+
+setup(
+    name='signal2',
+    version='0.2',
+    author='Antonio Cuni',
+    author_email='anto.cuni@gmail.com',
+    py_modules=['signal2'],
+    url='http://bitbucket.org/antocuni/signal2',
+    license='BSD',
+    description='ctypes wrapper around sigaction() and sigqueue()',
+    long_description='ctypes wrapper around sigaction() and sigqueue()',
+    install_requires=["ctypes_configure",
+                      "py" # actually, it's required by ctypes_configure
+                      ],
+)
+import ctypes
+from ctypes_configure import configure
+
+libc = ctypes.CDLL(None)
+
+class siginfo_t(ctypes.Structure):
+    pass # forward reference
+
+class sigval_t(ctypes.Union):
+    _fields_ = [
+        ('sigval_int', ctypes.c_int),
+        ('sigval_ptr', ctypes.c_void_p)
+        ]
+
+sa_sigaction_t = ctypes.CFUNCTYPE(None,
+                                  ctypes.c_int,
+                                  ctypes.POINTER(siginfo_t),
+                                  ctypes.c_void_p)
+
+class _CConfigure:
+    _compilation_info_ = configure.ExternalCompilationInfo(
+        includes = ['signal.h', 'sys/types.h', 'unistd.h'],
+        )
+
+    pid_t = configure.SimpleType('pid_t', ctypes.c_int)
+
+    SA_SIGINFO = configure.ConstantInteger('SA_SIGINFO')
+
+    struct_sigaction = configure.Struct('struct sigaction', [
+            ('sa_flags', ctypes.c_int),
+            ('sa_sigaction', sa_sigaction_t)
+            ])
+
+    siginfo_t = configure.Struct('siginfo_t', [
+            ('si_signo', ctypes.c_int),
+            ('si_value', sigval_t)
+            ])
+
+_info = configure.configure(_CConfigure)
+
+pid_t = _info['pid_t']
+struct_sigaction = _info['struct_sigaction']
+siginfo_t._fields_ = _info['siginfo_t']._fields_
+SA_SIGINFO = _info['SA_SIGINFO']
+
+sigaction = libc.sigaction
+sigaction.argtypes = [ctypes.c_int,
+                      ctypes.POINTER(struct_sigaction),
+                      ctypes.POINTER(struct_sigaction)]
+sigaction.restype = ctypes.c_int
+
+sigqueue = libc.sigqueue
+sigqueue.argtypes = [pid_t, ctypes.c_int, sigval_t]
+sigqueue.restype = ctypes.c_int
+
+
+def install_sigaction(signo, sa_flags, handler):
+    """
+    Convenience function to install a sigaction() handler
+    """
+    action = struct_sigaction()
+    action.sa_flags = SA_SIGINFO
+    action.sa_sigaction = sa_sigaction_t(handler)
+    return sigaction(signo, ctypes.pointer(action), None)

Empty file added.

testing/test_signal2.py

+import os
+import ctypes
+import signal
+#
+from signal2 import SA_SIGINFO, install_sigaction, sigval_t, sigqueue
+
+def test_install_and_fire():
+    calls = []
+    def handler(signo, ptr_info, extra):
+        info = ptr_info[0]
+        val = info.si_value.sigval_int
+        calls.append((signo, info.si_signo, val))
+    install_sigaction(signal.SIGUSR2, SA_SIGINFO, handler)
+
+    pid = os.getpid()
+    val = sigval_t()
+    val.sigval_int = 42
+    sigqueue(pid, signal.SIGUSR2, val)
+    assert calls == [(signal.SIGUSR2, signal.SIGUSR2, 42)]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.