from __future__ import absolute_import

import signal
import sys

from time import time

from .exceptions import RestartFreqExceeded

TERMSIGS = frozenset([
    'HUP',
    'QUIT',
    'ILL',
    'TRAP',
    'ABRT',
    'EMT',
    'FPE',
    'BUS',
    'SEGV',
    'SYS',
    'PIPE',
    'ALRM',
    'TERM',
    'XCPU',
    'XFSZ',
    'VTALRM',
    'PROF',
    'USR1',
    'USR2',
])


def _shutdown_cleanup(signum, frame):
    sys.exit(-(256 - signum))


def reset_signals(handler=_shutdown_cleanup):
    for sig in TERMSIGS:
        try:
            signum = getattr(signal, 'SIG%s' % (sig, ))
            if signal.getsignal(signum) != signal.SIG_IGN:
                signal.signal(signum, handler)
        except (OSError, AttributeError, ValueError, RuntimeError):
            pass


class restart_state(object):
    RestartFreqExceeded = RestartFreqExceeded

    def __init__(self, maxR, maxT):
        self.maxR, self.maxT = maxR, maxT
        self.R, self.T = 0, None

    def step(self):
        now = time()
        R = self.R
        if self.T and now - self.T >= self.maxT:
            self.R = 0
        elif self.maxR and R >= self.maxR:
            # verify that R has a value as it may have been reset
            # by another thread, and we want to avoid locking.
            if self.R:
                raise self.RestartFreqExceeded(
                    "%r in %rs" % (self.R, self.maxT),
                )
        self.R += 1
        self.T = now

@@ -142,25 +142,18 @@
 S = 
-frozenset(%5B
+(
 %0A    '
+SIG
 HUP'
@@ -159,16 +159,19 @@
 ',%0A    '
+SIG
 QUIT',%0A 
@@ -174,16 +174,19 @@
 ',%0A    '
+SIG
 ILL',%0A  
@@ -188,16 +188,19 @@
 ',%0A    '
+SIG
 TRAP',%0A 
@@ -203,16 +203,19 @@
 ',%0A    '
+SIG
 ABRT',%0A 
@@ -218,16 +218,19 @@
 ',%0A    '
+SIG
 EMT',%0A  
@@ -232,16 +232,19 @@
 ',%0A    '
+SIG
 FPE',%0A  
@@ -246,16 +246,19 @@
 ',%0A    '
+SIG
 BUS',%0A  
@@ -260,16 +260,19 @@
 ',%0A    '
+SIG
 SEGV',%0A 
@@ -275,16 +275,19 @@
 ',%0A    '
+SIG
 SYS',%0A  
@@ -289,16 +289,19 @@
 ',%0A    '
+SIG
 PIPE',%0A 
@@ -304,16 +304,19 @@
 ',%0A    '
+SIG
 ALRM',%0A 
@@ -319,16 +319,19 @@
 ',%0A    '
+SIG
 TERM',%0A 
@@ -334,16 +334,19 @@
 ',%0A    '
+SIG
 XCPU',%0A 
@@ -349,16 +349,19 @@
 ',%0A    '
+SIG
 XFSZ',%0A 
@@ -364,16 +364,19 @@
 ',%0A    '
+SIG
 VTALRM',
@@ -381,16 +381,19 @@
 ',%0A    '
+SIG
 PROF',%0A 
@@ -396,16 +396,19 @@
 ',%0A    '
+SIG
 USR1',%0A 
@@ -415,16 +415,18 @@
    '
+SIG
 USR2',%0A
-%5D
 )%0A%0A%0A
@@ -620,25 +620,11 @@
 al, 
-'SIG%25s' %25 (sig, )
+sig
 )%0A  
@@ -633,18 +633,25 @@
         
-if
+current =
  signal.
@@ -667,16 +667,51 @@
 (signum)
+%0A            if current and current
  != sign

Fix invalid argument bug when running in Python3.3