PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Python | 130 lines | 84 code | 21 blank | 25 comment | 16 complexity | 2ad2400f6701c81d058f4f15f8134048 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. from iptest.assert_util import *
  16. skiptest("silverlight")
  17. import signal
  18. '''
  19. TODO: until we can send signals to other processes consistently (e.g., os.kill),
  20. signal is fairly difficult to test in an automated fashion. For now this
  21. will have to do.
  22. '''
  23. #--GLOBALS---------------------------------------------------------------------
  24. SUPPORTED_SIGNALS = [ signal.SIGBREAK,
  25. signal.SIGABRT,
  26. signal.SIGINT,
  27. signal.SIGTERM,
  28. signal.SIGFPE,
  29. signal.SIGILL,
  30. signal.SIGSEGV,
  31. ]
  32. #--RUN THIS FIRST--------------------------------------------------------------
  33. def run_me_first():
  34. for x in [x for x in SUPPORTED_SIGNALS if x!=signal.SIGINT]:
  35. AreEqual(signal.getsignal(x), 0)
  36. AreEqual(signal.getsignal(signal.SIGINT), signal.default_int_handler)
  37. for x in xrange(1, 23):
  38. if x in SUPPORTED_SIGNALS: continue
  39. AreEqual(signal.getsignal(x), None)
  40. run_me_first()
  41. #--TEST CASES------------------------------------------------------------------
  42. def test_signal_getsignal_negative():
  43. for x in [-2, -1, 0, 23, 24, 25]:
  44. AssertErrorWithMessage(ValueError, "signal number out of range",
  45. signal.getsignal, x)
  46. for x in [None, "abc", "14"]:
  47. AssertError(TypeError, signal.getsignal, x)
  48. def test_module_constants():
  49. AreEqual(signal.NSIG, 23)
  50. AreEqual(signal.SIGABRT, 22)
  51. AreEqual(signal.SIGBREAK, 21)
  52. AreEqual(signal.SIGFPE, 8)
  53. AreEqual(signal.SIGILL, 4)
  54. AreEqual(signal.SIGINT, 2)
  55. AreEqual(signal.SIGSEGV, 11)
  56. AreEqual(signal.SIGTERM, 15)
  57. AreEqual(signal.SIG_DFL, 0)
  58. AreEqual(signal.SIG_IGN, 1)
  59. def test_doc():
  60. Assert("get the signal action for a given signal" in signal.__doc__)
  61. Assert("The default handler for SIGINT installed by Python" in signal.default_int_handler.__doc__)
  62. Assert("Return the current action for the given signal" in signal.getsignal.__doc__)
  63. Assert("Set the action for the given signal" in signal.signal.__doc__)
  64. @skip("win32") #http://bugs.python.org/issue9324
  65. def test_signal_signal_neg():
  66. def a(b, c):
  67. pass
  68. WEIRD_WORKING_CASES = [6]
  69. NO_SUCH_DIR = [21]
  70. for x in xrange(1, 23):
  71. if x in SUPPORTED_SIGNALS: continue
  72. if x in WEIRD_WORKING_CASES: continue
  73. AssertError(RuntimeError,
  74. signal.signal, x, a)
  75. for x in [-2, -1, 0, 23, 24, 25]:
  76. AssertErrorWithMessage(ValueError, "signal number out of range",
  77. signal.signal, x, a)
  78. def bad_sig0(): pass
  79. def bad_sig1(a): pass
  80. def bad_sig3(a,b,c): pass
  81. for y in SUPPORTED_SIGNALS + WEIRD_WORKING_CASES:
  82. bad_handlers = [-2, -1, 2, 3, 4, 10, 22, 23, 24, None]
  83. for x in bad_handlers:
  84. AssertErrorWithMessage(TypeError, "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object",
  85. signal.signal, y, x)
  86. def test_signal_signal():
  87. WORKING_CASES = SUPPORTED_SIGNALS + [6]
  88. WEIRD_CASES = {
  89. 6: None,
  90. 2: signal.default_int_handler}
  91. for x in WORKING_CASES:
  92. #Ideal handler signature
  93. def a(signum, frame):
  94. return x
  95. ret_val = signal.signal(x, a)
  96. if x not in WEIRD_CASES.keys():
  97. AreEqual(ret_val, signal.SIG_DFL)
  98. else:
  99. AreEqual(ret_val, WEIRD_CASES[x])
  100. AreEqual(a, signal.getsignal(x))
  101. #Strange handler signatures
  102. class KNew(object):
  103. def __call__(self, *args, **kwargs):
  104. pass
  105. a = KNew()
  106. ret_val = signal.signal(signal.SIGBREAK, a)
  107. AreEqual(a, signal.getsignal(signal.SIGBREAK))
  108. #--MAIN------------------------------------------------------------------------
  109. run_test(__name__)