[Tutor] signals and frame objects

Cwikla, Joe CwiklaJ at diebold.com
Wed Sep 15 23:56:19 CEST 2004


But if you wanted to use the stack frame there's a great example here:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=Y35XKKAZKTi%24Ewl3%4
0jessikat.fsnet.co.uk

No SIGALRM on Windows so substituting SIGBREAK and combining the above with
Kent's example:

######################################################################
breaks=0
action=None
 
def looper():
    print "Looping"
    while(True):
        pass

import signal
def handler(signum, frame):
    import sys
    c=frame.f_code
    print 'Signal handler called with signal', signum
    print 'called from ', c.co_filename, c.co_name, frame.f_lineno
    global breaks
    breaks+=1
    if breaks==7:
        signal.signal(signal.SIGBREAK, action)

# Set the signal handler
action=signal.signal(signal.SIGBREAK, handler)

looper()
#####################################################################

Yields (with 8 CTRL-BREAK):

Looping
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 7
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 7
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 6
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 7
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 6
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 6
Signal handler called with signal 21
called from  C:\test\SignalFrame.py looper 6
^C

- Joe

>Date: Wed, 15 Sep 2004 12:31:16 -0700 (PDT)
>From: Marilyn Davis <marilyn at deliberate.com>
>Subject: Re: [Tutor] signals and frame objects
>To: Kent Johnson <kent_johnson at skillsoft.com>
>Cc: tutor at python.org
>Message-ID: <Pine.LNX.4.44.0409151228180.8998-100000 at Kuna>
>Content-Type: TEXT/PLAIN; charset=US-ASCII
>
>Oh wow.  That is so easy.  I thought I had to manipulate the frame object
somehow to get back to where I was.  Thank you >so much Kent.
>
>Marilyn
>
>On Wed, 15 Sep 2004, Kent Johnson wrote:
>
>> If you return from the handler, processing will continue where it was 
>> interrupted. If you want to inspect the frame object in your handler, 
>> some information about it is available here:
>> http://docs.python.org/ref/types.html#l2h-142
>> 
>> For example, running this program in IDLE on MacOSX:
>> #############################
>> import signal
>> 
>> done = 0
>> def handler(signum, frame):
>>      print frame
>>      print dir(frame)
>>      print 'Signal handler called with signal', signum
>>      global done
>>      done = 1
>> 
>> # Set the signal handler and an alarm
>> signal.signal(signal.SIGALRM, handler)
>> signal.alarm(3)
>> 
>> print "Looping"
>> while not done:
>>      pass
>> 
>> print "Out of loop"
>> 
>> signal.alarm(0)          # Disable the alarm
>> 
>> print "Done"
>> ##############################3
>> 
>> gives this output:
>>  >>> ================================ RESTART 
>> ================================  >>> Looping <frame object at 
>> 0x4fd6c0> ['__class__', '__delattr__', '__doc__', '__getattribute__', 
>> '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', 
>> '__repr__', '__setattr__', '__str__', 'f_back', 'f_builtins', 
>> 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 
>> 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace'] Signal 
>> handler called with signal 14 Out of loop Done  >>>
>> 
>> Kent


More information about the Tutor mailing list