Merge lp:~schmichael/python-signalfd/helpers into lp:python-signalfd

Proposed by Michael Schurter
Status: Superseded
Proposed branch: lp:~schmichael/python-signalfd/helpers
Merge into: lp:python-signalfd
Diff against target: 147 lines (+111/-3)
2 files modified
setup.py (+2/-1)
signalfd/__init__.py (+109/-2)
To merge this branch: bzr merge lp:~schmichael/python-signalfd/helpers
Reviewer Review Type Date Requested Status
Jean-Paul Calderone Pending
Review via email: mp+50551@code.launchpad.net

Description of the change

Without a helper to parse the 128 byte (80 meaningful bytes) siginfo_t, signalfds are pretty difficult to use, so I added the relevant struct and a couple helpers. I'm not picky about whether or not this is the final API; I just wanted to get something there to make signalfds usable.

Thanks for the great module! Sorry to see it didn't make it into 3.2. Hopefully 3.3?

To post a comment you must log in.
8. By Michael Schurter

Default to no flags in create_signalfd helper

9. By Michael Schurter

No need for copyright attribution

10. By Michael Schurter

Add basic bzrignore patterns

11. By Michael Schurter

Remove use of setuptools

12. By Michael Schurter

Match version in signalfd/__init__.py

13. By Michael Schurter

Switch to a simpler namedtuples like class for SigInfo

14. By Michael Schurter

Add some simple tests for {create_,read_}signalfd helpers

15. By Michael Schurter

Add a test for reading a nonblocking fd before it's ready

Unmerged revisions

15. By Michael Schurter

Add a test for reading a nonblocking fd before it's ready

14. By Michael Schurter

Add some simple tests for {create_,read_}signalfd helpers

13. By Michael Schurter

Switch to a simpler namedtuples like class for SigInfo

12. By Michael Schurter

Match version in signalfd/__init__.py

11. By Michael Schurter

Remove use of setuptools

10. By Michael Schurter

Add basic bzrignore patterns

9. By Michael Schurter

No need for copyright attribution

8. By Michael Schurter

Default to no flags in create_signalfd helper

7. By Michael Schurter

Add signalfd creation and reader helpers

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2010-06-21 22:16:05 +0000
3+++ setup.py 2011-02-21 06:26:50 +0000
4@@ -1,7 +1,8 @@
5 # Copyright (c) 2010 Jean-Paul Calderone
6 # See LICENSE file for details.
7
8-from distutils.core import Extension, setup
9+from setuptools import setup
10+from distutils.core import Extension
11
12 setup(
13 name="python-signalfd",
14
15=== modified file 'signalfd/__init__.py'
16--- signalfd/__init__.py 2010-06-21 22:11:44 +0000
17+++ signalfd/__init__.py 2011-02-21 06:26:50 +0000
18@@ -1,5 +1,8 @@
19 # Copyright (c) 2010 Jean-Paul Calderone.
20+# Copyright (c) 2011 Michael Schurter <michael@susens-schurter.com>
21 # See LICENSE file for details.
22+import os
23+import struct
24
25 try:
26 from signalfd._signalfd import \
27@@ -12,10 +15,12 @@
28 SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, \
29 SFD_CLOEXEC, SFD_NONBLOCK
30
31-__version__ = '0.1'
32-__version_info__ = (0, 1)
33+__version__ = '0.2'
34+__version_info__ = (0, 2)
35
36 __all__ = [
37+ 'created_signalfd', 'read_signalfd', 'siginfo_t', 'SigInfo',
38+
39 'sigprocmask', 'signalfd',
40
41 'SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK',
42@@ -23,3 +28,105 @@
43 'SFD_CLOEXEC', 'SFD_NONBLOCK',
44
45 '__version__', '__version_info__']
46+
47+_signo_to_str = {
48+ 1: 'SIGHUP',
49+ 2: 'SIGINT',
50+ 3: 'SIGQUIT',
51+ 4: 'SIGILL',
52+ 5: 'SIGTRAP',
53+ 6: 'SIGABRT',
54+ 7: 'SIGBUS',
55+ 8: 'SIGFPE',
56+ 9: 'SIGKILL',
57+ 10: 'SIGUSR1',
58+ 11: 'SIGSEGV',
59+ 12: 'SIGUSR2',
60+ 13: 'SIGPIPE',
61+ 14: 'SIGALRM',
62+ 15: 'SIGTERM',
63+ 16: 'SIGSTKFLT',
64+ 17: 'SIGCHLD',
65+ 18: 'SIGCONT',
66+ 19: 'SIGSTOP',
67+ 20: 'SIGTSTP',
68+ 21: 'SIGTTIN',
69+ 22: 'SIGTTOU',
70+ 23: 'SIGURG',
71+ 24: 'SIGXCPU',
72+ 25: 'SIGXFSZ',
73+ 26: 'SIGVTALRM',
74+ 27: 'SIGPROF',
75+ 28: 'SIGWINCH',
76+ 29: 'SIGIO',
77+ 30: 'SIGPWR',
78+ 31: 'SIGSYS',
79+ 34: 'SIGRTMIN',
80+ 64: 'SIGRTMAX',
81+ }
82+
83+def create_signalfd(signals, flags=0):
84+ """Creates a new signal file descriptor
85+
86+ Shortcut for:
87+ signalfd.sigprocmask(signalfd.SIG_BLOCK, signals)
88+ signalfd(-1, signals, flags)
89+ """
90+ sigprocmask(SIG_BLOCK, signals)
91+ return signalfd(-1, signals, flags)
92+
93+def read_signalfd(fd):
94+ """Given a signalfd, return a SigInfo instance"""
95+ return SigInfo(os.read(fd, siginfo_t.size))
96+
97+class SigInfo(object):
98+ def __init__(self, data):
99+ (self.ssi_signo,
100+ self.ssi_errno,
101+ self.ssi_code,
102+ self.ssi_pid,
103+ self.ssi_uid,
104+ self.ssi_fd,
105+ self.ssi_tid,
106+ self.ssi_band,
107+ self.ssi_overrun,
108+ self.ssi_trapno,
109+ self.ssi_status,
110+ self.ssi_int,
111+ self.ssi_ptr,
112+ self.ssi_utime,
113+ self.ssi_stime,
114+ self.ssi_addr,
115+ self.padding) = siginfo_t.unpack(data)
116+
117+ @property
118+ def signo(self):
119+ return self.ssi_signo
120+
121+ @property
122+ def signal(self):
123+ return _signo_to_str[self.ssi_signo]
124+
125+ def __str__(self):
126+ return self.signal
127+
128+siginfo_t = struct.Struct(
129+ 'I' # ssi_signo - Signal number
130+ 'i' # ssi_errno - Error number (unused)
131+ 'i' # ssi_code - Signal code
132+ 'I' # ssi_pid - PID of sender
133+ 'I' # ssi_uid - Real UID of sender
134+ 'i' # ssi_fd - File descriptor (SIGIO)
135+ 'I' # ssi_tid - Kernel timer ID (POSIX timers)
136+ 'I' # ssi_band - Band event (SIGIO)
137+ 'I' # ssi_overrun - POSIX timer overrun count
138+ 'I' # ssi_trapno - Trap number that caused signal
139+ 'i' # ssi_status - Exit status or signal (SIGCHLD)
140+ 'i' # ssi_int - Integer sent by sigqueue (2)
141+ 'Q' # ssi_ptr - Pointer sent by sigqueue (2)
142+ 'Q' # ssi_utime - User CPU time consumed (SIGCHLD)
143+ 'Q' # ssi_stime - System CPU time consumed (SIGCHLD)
144+ 'Q' # ssi_addr - Address that generated signal
145+ # (for hardware generated signals)
146+ '48s' # pad[X] - Pad size to 128 bytes for future use
147+ )

Subscribers

People subscribed via source and target branches