PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/modules/system_related/signal_test.py

#
Python | 130 lines | 84 code | 21 blank | 25 comment | 34 complexity | 2ad2400f6701c81d058f4f15f8134048 MD5 | raw file
  1#####################################################################################
  2#
  3#  Copyright (c) Microsoft Corporation. All rights reserved.
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6# copy of the license can be found in the License.html file at the root of this distribution. If
  7# you cannot locate the  Apache License, Version 2.0, please send an email to
  8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14#####################################################################################
 15
 16from iptest.assert_util import *
 17skiptest("silverlight")
 18import signal
 19
 20'''
 21TODO: until we can send signals to other processes consistently (e.g., os.kill),
 22      signal is fairly difficult to test in an automated fashion. For now this
 23      will have to do.
 24'''
 25
 26#--GLOBALS---------------------------------------------------------------------
 27SUPPORTED_SIGNALS = [   signal.SIGBREAK,
 28                        signal.SIGABRT,
 29                        signal.SIGINT, 
 30                        signal.SIGTERM,
 31                        signal.SIGFPE, 
 32                        signal.SIGILL, 
 33                        signal.SIGSEGV,
 34                        ]
 35#--RUN THIS FIRST--------------------------------------------------------------
 36def run_me_first():
 37    for x in [x for x in SUPPORTED_SIGNALS if x!=signal.SIGINT]:
 38        AreEqual(signal.getsignal(x), 0)
 39    AreEqual(signal.getsignal(signal.SIGINT), signal.default_int_handler)
 40
 41    for x in xrange(1, 23):
 42        if x in SUPPORTED_SIGNALS: continue
 43        AreEqual(signal.getsignal(x), None)
 44
 45run_me_first()
 46
 47#--TEST CASES------------------------------------------------------------------
 48def test_signal_getsignal_negative():
 49    for x in [-2, -1, 0, 23, 24, 25]:
 50        AssertErrorWithMessage(ValueError, "signal number out of range",
 51                               signal.getsignal, x)
 52    for x in [None, "abc", "14"]:
 53        AssertError(TypeError, signal.getsignal, x)
 54
 55def test_module_constants():
 56    AreEqual(signal.NSIG, 23)
 57    AreEqual(signal.SIGABRT, 22)
 58    AreEqual(signal.SIGBREAK, 21)
 59    AreEqual(signal.SIGFPE, 8)
 60    AreEqual(signal.SIGILL, 4)
 61    AreEqual(signal.SIGINT, 2)
 62    AreEqual(signal.SIGSEGV, 11)
 63    AreEqual(signal.SIGTERM, 15)
 64    AreEqual(signal.SIG_DFL, 0)
 65    AreEqual(signal.SIG_IGN, 1)
 66
 67def test_doc():
 68    Assert("get the signal action for a given signal" in signal.__doc__)
 69    Assert("The default handler for SIGINT installed by Python" in signal.default_int_handler.__doc__)
 70    Assert("Return the current action for the given signal" in signal.getsignal.__doc__)
 71    Assert("Set the action for the given signal" in signal.signal.__doc__)
 72
 73@skip("win32") #http://bugs.python.org/issue9324
 74def test_signal_signal_neg():
 75    def a(b, c):
 76        pass
 77    
 78    WEIRD_WORKING_CASES = [6]
 79    NO_SUCH_DIR = [21]
 80    
 81    for x in xrange(1, 23):
 82        if x in SUPPORTED_SIGNALS: continue
 83        if x in WEIRD_WORKING_CASES: continue
 84        AssertError(RuntimeError,
 85                    signal.signal, x, a)
 86
 87    for x in [-2, -1, 0, 23, 24, 25]:
 88        AssertErrorWithMessage(ValueError, "signal number out of range",
 89                               signal.signal, x, a)
 90
 91    def bad_sig0(): pass
 92    def bad_sig1(a): pass
 93    def bad_sig3(a,b,c): pass
 94
 95    for y in SUPPORTED_SIGNALS + WEIRD_WORKING_CASES:
 96        bad_handlers = [-2, -1, 2, 3, 4, 10, 22, 23, 24, None] 
 97        for x in bad_handlers:
 98            AssertErrorWithMessage(TypeError, "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object",
 99                                   signal.signal, y, x)
100
101
102def test_signal_signal():
103    WORKING_CASES = SUPPORTED_SIGNALS + [6]
104    WEIRD_CASES = {
105                   6: None,
106                   2: signal.default_int_handler}
107    for x in WORKING_CASES:
108        #Ideal handler signature
109        def a(signum, frame):
110            return x
111        
112        ret_val = signal.signal(x, a)
113        if x not in WEIRD_CASES.keys():
114            AreEqual(ret_val, signal.SIG_DFL)
115        else:
116            AreEqual(ret_val, WEIRD_CASES[x])
117        AreEqual(a, signal.getsignal(x))
118        
119    #Strange handler signatures
120    class KNew(object):
121        def __call__(self, *args, **kwargs):
122            pass
123    
124    a = KNew()
125    ret_val = signal.signal(signal.SIGBREAK, a)
126    AreEqual(a, signal.getsignal(signal.SIGBREAK))
127                 
128
129#--MAIN------------------------------------------------------------------------   
130run_test(__name__)