File: sigtest.py

package info (click to toggle)
pydb 1.19-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,952 kB
  • ctags: 1,412
  • sloc: python: 3,065; perl: 2,479; sh: 586; makefile: 555; lisp: 265; ansic: 16
file content (152 lines) | stat: -rwxr-xr-x 5,459 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python -t
# -*- Python -*-
# $Id: sigtest.py.in,v 1.17 2006/10/09 20:11:56 rockyb Exp $
"Unit test for Extended Python debugger's signal handling commands "
import os, sys, unittest, signal

top_builddir = "../"
if top_builddir[-1] != os.path.sep:
    top_builddir += os.path.sep
sys.path.insert(0, os.path.join(top_builddir, 'pydb'))
top_srcdir = ".."
if top_srcdir[-1] != os.path.sep:
    top_srcdir += os.path.sep
sys.path.insert(0, os.path.join(top_srcdir, 'pydb'))

import sighandler

builddir     = "."
if builddir[-1] != os.path.sep:
    builddir += os.path.sep

top_builddir = "../"
if top_builddir[-1] != os.path.sep:
    top_builddir += os.path.sep

srcdir = "."
if srcdir[-1] != os.path.sep:
    srcdir += os.path.sep

pydir        = os.path.join(top_builddir, "pydb")
pydb_short   = "pydb.py"
pydb_path    = os.path.join(pydir, pydb_short)
outfile      = 'sighandler.out'
program      = 'sigtestexample.py'

class SigTests(unittest.TestCase):
    def tearDown(self):
        try:
            os.unlink(outfile)
        except OSError:
            pass
        try:
            os.unlink('log')
        except OSError:
            pass

    def create_pydb_proc(self, cmds,arg=None):
        """Run pydb on program with command string 'cmds', and pass arg if
        that is given. Debugger output is stored in outfile. Note that
        there is also output produced by the debugged program.
        """
        if arg:
            pid = os.spawnlp(os.P_WAIT, pydb_path, pydb_path, '-o',
                             outfile, '-e', '%s' % cmds, program, arg)
        else:
            pid = os.spawnlp(os.P_NOWAIT, pydb_path, pydb_path, '-o',
                             outfile, '-e', '%s' % cmds, program)
        return pid

    def test_signum_name(self):
        """Test that signal name and number lookup work """
        for signum in range(signal.NSIG):
            signame = sighandler.lookup_signame(signum)
            if signame is not None:
                self.assertEqual(signum, sighandler.lookup_signum(signame))
                # Try without the SIG prefix
                self.assertEqual(signum, sighandler.lookup_signum(signame[3:]))

    def test_noprint_nostop_pass(self):
        """Check to see the debugger does not intercepts a USR1 signal,
        doesn't print intercepting it but passes it along to the program."""

        self.tearDown()
        cmds = 'handle SIGUSR1 noprint nostop pass;;next 10;;quit'
        pid = self.create_pydb_proc(cmds, 'signal')
        f = open(outfile, 'r')
        lines = f.readlines()
        f.close()
        self.assertEqual(False,'Program received signal SIGUSR1\n' in lines) 
        f = open('log', 'r')
        line = f.readline()
        f.close()
        self.assertEqual(line, 'signal received\n')
        os.unlink('log')

    def test_noprint_nostop_nopass(self):
        """Check to see the debugger does not intercepts a USR1 signal,
        doesn't print intercepting and doesn't pass it along to the program."""

        self.tearDown()
        cmds = 'handle SIGUSR1 noprint nostop nopass;;set sigcheck on;;continue;;quit'
        pid = self.create_pydb_proc(cmds, 'signal')
        f = open(outfile, 'r')
        lines = f.readlines()
        f.close()
        self.assertEqual(False,'Program received signal SIGUSR1\n' in lines) 
        self.assertRaises(IOError, open, 'log', 'r')

    def test_print_nostop_nopass(self):
        """Check to see the debugger intercepts a USR1 signal,
        prints it and doesn't pass it along to the program.
        """
        self.tearDown()
        cmds = "handle SIGUSR1 print nostop nopass;;set sigcheck on;;continue;;quit"
        pid = self.create_pydb_proc(cmds, 'signal')
        f = open(outfile, 'r')
        lines = f.readlines()
        f.close()

        self.assertEqual(True,'Program received signal SIGUSR1\n' in lines)
        self.assertRaises(IOError, open, 'log', 'r')

    def test_print_nostop_pass(self):
        """Check to see the debugger intercepts a USR1 signal,
        prints it and passes it along to the program.
        """
        self.tearDown()
        cmds = "handle SIGUSR1 print nostop pass;;next 10;;quit"
        pid = self.create_pydb_proc(cmds, 'signal')
        f = open(outfile, 'r')
        lines = f.readlines()
        f.close()

        self.assertEqual(True,'Program received signal SIGUSR1\n' in lines)
        f = open('log', 'r')
        line = f.readline()
        f.close()
        self.assertEqual(line, 'signal received\n')
        os.unlink('log')

    def test_print_stop(self):
        """Check to see the debugger intercepts a USR1 signal and stops."""

        self.tearDown()
        cmds = 'set basename on;;handle SIGUSR1 print stop;;continue;;where;;quit'
        pid = self.create_pydb_proc(cmds, 'signal')
        f = open(outfile, 'r')
        lines = f.readlines()
        f.close()
        self.assertEqual(True, 'Program received signal SIGUSR1\n' in lines)
        if sys.version_info[0:2] != (2, 5):
            self.assertEqual(True,
                             "-> 0 in file 'sigtestexample.py' at line 20\n"
                             in lines)
        else:
            self.assertEqual(True,
                             "-> 0 <module>() called from file 'sigtestexample.py' at line 20\n"
                             in lines)
        self.assertRaises(IOError, open, 'log', 'r')

if __name__ == '__main__':
    unittest.main()