Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 1 | # Verify that gdb can pretty-print the various PyObject* types |
| 2 | # |
| 3 | # The code for testing gdb was adapted from similar work in Unladen Swallow's |
| 4 | # Lib/test/test_jit_gdb.py |
| 5 | |
| 6 | import os |
| 7 | import re |
Antoine Pitrou | d0f3e07 | 2013-09-21 23:56:17 +0200 | [diff] [blame] | 8 | import pprint |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 9 | import subprocess |
| 10 | import sys |
Vinay Sajip | f1b34ee | 2012-05-06 12:03:05 +0100 | [diff] [blame] | 11 | import sysconfig |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 12 | import unittest |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 13 | import locale |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 14 | |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 15 | # Is this Python configured to support threads? |
| 16 | try: |
| 17 | import _thread |
| 18 | except ImportError: |
| 19 | _thread = None |
| 20 | |
Antoine Pitrou | d0f3e07 | 2013-09-21 23:56:17 +0200 | [diff] [blame] | 21 | from test import support |
Benjamin Peterson | 65c66ab | 2010-10-29 21:31:35 +0000 | [diff] [blame] | 22 | from test.support import run_unittest, findfile, python_is_optimized |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 23 | |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 24 | def get_gdb_version(): |
| 25 | try: |
| 26 | proc = subprocess.Popen(["gdb", "-nx", "--version"], |
| 27 | stdout=subprocess.PIPE, |
| 28 | universal_newlines=True) |
| 29 | with proc: |
| 30 | version = proc.communicate()[0] |
| 31 | except OSError: |
| 32 | # This is what "no gdb" looks like. There may, however, be other |
| 33 | # errors that manifest this way too. |
| 34 | raise unittest.SkipTest("Couldn't find gdb on the path") |
| 35 | |
| 36 | # Regex to parse: |
| 37 | # 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7 |
| 38 | # 'GNU gdb (GDB) Fedora 7.9.1-17.fc22\n' -> 7.9 |
Victor Stinner | 479fea6 | 2015-09-03 15:42:26 +0200 | [diff] [blame] | 39 | # 'GNU gdb 6.1.1 [FreeBSD]\n' -> 6.1 |
| 40 | # 'GNU gdb (GDB) Fedora (7.5.1-37.fc18)\n' -> 7.5 |
Victor Stinner | a578eb3 | 2015-09-15 00:22:55 +0200 | [diff] [blame] | 41 | match = re.search(r"^GNU gdb.*?\b(\d+)\.(\d+)", version) |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 42 | if match is None: |
| 43 | raise Exception("unable to parse GDB version: %r" % version) |
| 44 | return (version, int(match.group(1)), int(match.group(2))) |
| 45 | |
| 46 | gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version() |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 47 | if gdb_major_version < 7: |
Victor Stinner | 2f3ac1e | 2015-09-02 23:12:14 +0200 | [diff] [blame] | 48 | raise unittest.SkipTest("gdb versions before 7.0 didn't support python " |
| 49 | "embedding. Saw %s.%s:\n%s" |
| 50 | % (gdb_major_version, gdb_minor_version, |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 51 | gdb_version)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 52 | |
Vinay Sajip | f1b34ee | 2012-05-06 12:03:05 +0100 | [diff] [blame] | 53 | if not sysconfig.is_python_build(): |
| 54 | raise unittest.SkipTest("test_gdb only works on source builds at the moment.") |
| 55 | |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 56 | # Location of custom hooks file in a repository checkout. |
| 57 | checkout_hook_path = os.path.join(os.path.dirname(sys.executable), |
| 58 | 'python-gdb.py') |
| 59 | |
Victor Stinner | 5132493 | 2013-11-20 12:27:48 +0100 | [diff] [blame] | 60 | PYTHONHASHSEED = '123' |
| 61 | |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 62 | def run_gdb(*args, **env_vars): |
| 63 | """Runs gdb in --batch mode with the additional arguments given by *args. |
| 64 | |
| 65 | Returns its (stdout, stderr) decoded from utf-8 using the replace handler. |
| 66 | """ |
| 67 | if env_vars: |
| 68 | env = os.environ.copy() |
| 69 | env.update(env_vars) |
| 70 | else: |
| 71 | env = None |
Victor Stinner | 7869a4e | 2014-08-16 14:38:02 +0200 | [diff] [blame] | 72 | # -nx: Do not execute commands from any .gdbinit initialization files |
| 73 | # (issue #22188) |
| 74 | base_cmd = ('gdb', '--batch', '-nx') |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 75 | if (gdb_major_version, gdb_minor_version) >= (7, 4): |
| 76 | base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path) |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 77 | proc = subprocess.Popen(base_cmd + args, |
Martin Panter | 7a5fe6d | 2016-01-16 05:18:47 +0000 | [diff] [blame] | 78 | # Redirect stdin to prevent GDB from messing with |
| 79 | # the terminal settings |
| 80 | stdin=subprocess.PIPE, |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 81 | stdout=subprocess.PIPE, |
| 82 | stderr=subprocess.PIPE, |
| 83 | env=env) |
| 84 | with proc: |
| 85 | out, err = proc.communicate() |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 86 | return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace') |
| 87 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 88 | # Verify that "gdb" was built with the embedded python support enabled: |
Antoine Pitrou | e50240c | 2013-11-23 17:40:36 +0100 | [diff] [blame] | 89 | gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)") |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 90 | if not gdbpy_version: |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 91 | raise unittest.SkipTest("gdb not built with embedded python support") |
| 92 | |
Nick Coghlan | ce34687 | 2013-09-22 19:38:16 +1000 | [diff] [blame] | 93 | # Verify that "gdb" can load our custom hooks, as OS security settings may |
| 94 | # disallow this without a customised .gdbinit. |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 95 | _, gdbpy_errors = run_gdb('--args', sys.executable) |
| 96 | if "auto-loading has been declined" in gdbpy_errors: |
| 97 | msg = "gdb security settings prevent use of custom hooks: " |
| 98 | raise unittest.SkipTest(msg + gdbpy_errors.rstrip()) |
Nick Coghlan | be4e4b5 | 2012-06-17 18:57:20 +1000 | [diff] [blame] | 99 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 100 | def gdb_has_frame_select(): |
| 101 | # Does this build of gdb have gdb.Frame.select ? |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 102 | stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))") |
| 103 | m = re.match(r'.*\[(.*)\].*', stdout) |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 104 | if not m: |
| 105 | raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test") |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 106 | gdb_frame_dir = m.group(1).split(', ') |
| 107 | return "'select'" in gdb_frame_dir |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 108 | |
| 109 | HAS_PYUP_PYDOWN = gdb_has_frame_select() |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 110 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 111 | BREAKPOINT_FN='builtin_id' |
| 112 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 113 | class DebuggerTests(unittest.TestCase): |
| 114 | |
| 115 | """Test that the debugger can debug Python.""" |
| 116 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 117 | def get_stack_trace(self, source=None, script=None, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 118 | breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 119 | cmds_after_breakpoint=None, |
| 120 | import_site=False): |
| 121 | ''' |
| 122 | Run 'python -c SOURCE' under gdb with a breakpoint. |
| 123 | |
| 124 | Support injecting commands after the breakpoint is reached |
| 125 | |
| 126 | Returns the stdout from gdb |
| 127 | |
| 128 | cmds_after_breakpoint: if provided, a list of strings: gdb commands |
| 129 | ''' |
| 130 | # We use "set breakpoint pending yes" to avoid blocking with a: |
| 131 | # Function "foo" not defined. |
| 132 | # Make breakpoint pending on future shared library load? (y or [n]) |
| 133 | # error, which typically happens python is dynamically linked (the |
| 134 | # breakpoints of interest are to be found in the shared library) |
| 135 | # When this happens, we still get: |
Victor Stinner | 67df3a4 | 2010-04-21 13:53:05 +0000 | [diff] [blame] | 136 | # Function "textiowrapper_write" not defined. |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 137 | # emitted to stderr each time, alas. |
| 138 | |
| 139 | # Initially I had "--eval-command=continue" here, but removed it to |
| 140 | # avoid repeated print breakpoints when traversing hierarchical data |
| 141 | # structures |
| 142 | |
| 143 | # Generate a list of commands in gdb's language: |
| 144 | commands = ['set breakpoint pending yes', |
| 145 | 'break %s' % breakpoint, |
Serhiy Storchaka | fdc9953 | 2015-01-31 11:48:52 +0200 | [diff] [blame] | 146 | |
Serhiy Storchaka | fdc9953 | 2015-01-31 11:48:52 +0200 | [diff] [blame] | 147 | # The tests assume that the first frame of printed |
| 148 | # backtrace will not contain program counter, |
| 149 | # that is however not guaranteed by gdb |
| 150 | # therefore we need to use 'set print address off' to |
| 151 | # make sure the counter is not there. For example: |
| 152 | # #0 in PyObject_Print ... |
| 153 | # is assumed, but sometimes this can be e.g. |
| 154 | # #0 0x00003fffb7dd1798 in PyObject_Print ... |
| 155 | 'set print address off', |
| 156 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 157 | 'run'] |
Serhiy Storchaka | 17d337b | 2015-02-06 08:35:20 +0200 | [diff] [blame] | 158 | |
| 159 | # GDB as of 7.4 onwards can distinguish between the |
| 160 | # value of a variable at entry vs current value: |
| 161 | # http://sourceware.org/gdb/onlinedocs/gdb/Variables.html |
| 162 | # which leads to the selftests failing with errors like this: |
| 163 | # AssertionError: 'v@entry=()' != '()' |
| 164 | # Disable this: |
| 165 | if (gdb_major_version, gdb_minor_version) >= (7, 4): |
| 166 | commands += ['set print entry-values no'] |
| 167 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 168 | if cmds_after_breakpoint: |
| 169 | commands += cmds_after_breakpoint |
| 170 | else: |
| 171 | commands += ['backtrace'] |
| 172 | |
| 173 | # print commands |
| 174 | |
| 175 | # Use "commands" to generate the arguments with which to invoke "gdb": |
Martin Panter | 40e102c | 2015-12-08 21:54:42 +0000 | [diff] [blame] | 176 | args = ['--eval-command=%s' % cmd for cmd in commands] |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 177 | args += ["--args", |
| 178 | sys.executable] |
| 179 | |
| 180 | if not import_site: |
| 181 | # -S suppresses the default 'import site' |
| 182 | args += ["-S"] |
| 183 | |
| 184 | if source: |
| 185 | args += ["-c", source] |
| 186 | elif script: |
| 187 | args += [script] |
| 188 | |
| 189 | # print args |
Antoine Pitrou | f6eb31f | 2013-11-24 14:58:17 +0100 | [diff] [blame] | 190 | # print (' '.join(args)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 191 | |
| 192 | # Use "args" to invoke gdb, capturing stdout, stderr: |
Victor Stinner | 5132493 | 2013-11-20 12:27:48 +0100 | [diff] [blame] | 193 | out, err = run_gdb(*args, PYTHONHASHSEED=PYTHONHASHSEED) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | 81641d6 | 2013-05-01 00:15:44 +0200 | [diff] [blame] | 195 | errlines = err.splitlines() |
| 196 | unexpected_errlines = [] |
| 197 | |
| 198 | # Ignore some benign messages on stderr. |
| 199 | ignore_patterns = ( |
| 200 | 'Function "%s" not defined.' % breakpoint, |
| 201 | "warning: no loadable sections found in added symbol-file" |
| 202 | " system-supplied DSO", |
| 203 | "warning: Unable to find libthread_db matching" |
| 204 | " inferior's thread library, thread debugging will" |
| 205 | " not be available.", |
| 206 | "warning: Cannot initialize thread debugging" |
| 207 | " library: Debugger service failed", |
| 208 | 'warning: Could not load shared library symbols for ' |
| 209 | 'linux-vdso.so', |
| 210 | 'warning: Could not load shared library symbols for ' |
| 211 | 'linux-gate.so', |
Serhiy Storchaka | 6b688d8 | 2015-02-14 22:44:35 +0200 | [diff] [blame] | 212 | 'warning: Could not load shared library symbols for ' |
| 213 | 'linux-vdso64.so', |
Antoine Pitrou | 81641d6 | 2013-05-01 00:15:44 +0200 | [diff] [blame] | 214 | 'Do you need "set solib-search-path" or ' |
| 215 | '"set sysroot"?', |
Victor Stinner | 5ac1b93 | 2013-06-25 21:54:32 +0200 | [diff] [blame] | 216 | 'warning: Source file is more recent than executable.', |
Victor Stinner | 23ed7e3 | 2013-11-25 10:43:59 +0100 | [diff] [blame] | 217 | # Issue #19753: missing symbols on System Z |
Victor Stinner | f4a4898 | 2013-11-24 18:55:25 +0100 | [diff] [blame] | 218 | 'Missing separate debuginfo for ', |
Victor Stinner | 23ed7e3 | 2013-11-25 10:43:59 +0100 | [diff] [blame] | 219 | 'Try: zypper install -C ', |
Antoine Pitrou | 81641d6 | 2013-05-01 00:15:44 +0200 | [diff] [blame] | 220 | ) |
| 221 | for line in errlines: |
| 222 | if not line.startswith(ignore_patterns): |
| 223 | unexpected_errlines.append(line) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 224 | |
| 225 | # Ensure no unexpected error messages: |
Antoine Pitrou | 81641d6 | 2013-05-01 00:15:44 +0200 | [diff] [blame] | 226 | self.assertEqual(unexpected_errlines, []) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 227 | return out |
| 228 | |
| 229 | def get_gdb_repr(self, source, |
| 230 | cmds_after_breakpoint=None, |
| 231 | import_site=False): |
| 232 | # Given an input python source representation of data, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 233 | # run "python -c'id(DATA)'" under gdb with a breakpoint on |
| 234 | # builtin_id and scrape out gdb's representation of the "op" |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 235 | # parameter, and verify that the gdb displays the same string |
| 236 | # |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 237 | # Verify that the gdb displays the expected string |
| 238 | # |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 239 | # For a nested structure, the first time we hit the breakpoint will |
| 240 | # give us the top-level structure |
Antoine Pitrou | f6eb31f | 2013-11-24 14:58:17 +0100 | [diff] [blame] | 241 | |
| 242 | # NOTE: avoid decoding too much of the traceback as some |
| 243 | # undecodable characters may lurk there in optimized mode |
| 244 | # (issue #19743). |
| 245 | cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"] |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 246 | gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 247 | cmds_after_breakpoint=cmds_after_breakpoint, |
| 248 | import_site=import_site) |
| 249 | # gdb can insert additional '\n' and space characters in various places |
| 250 | # in its output, depending on the width of the terminal it's connected |
| 251 | # to (using its "wrap_here" function) |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 252 | m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 253 | gdb_output, re.DOTALL) |
| 254 | if not m: |
| 255 | self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output)) |
| 256 | return m.group(1), gdb_output |
| 257 | |
| 258 | def assertEndsWith(self, actual, exp_end): |
| 259 | '''Ensure that the given "actual" string ends with "exp_end"''' |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 260 | self.assertTrue(actual.endswith(exp_end), |
| 261 | msg='%r did not end with %r' % (actual, exp_end)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 262 | |
| 263 | def assertMultilineMatches(self, actual, pattern): |
| 264 | m = re.match(pattern, actual, re.DOTALL) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 265 | if not m: |
| 266 | self.fail(msg='%r did not match %r' % (actual, pattern)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 267 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 268 | def get_sample_script(self): |
| 269 | return findfile('gdb_sample.py') |
| 270 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 271 | class PrettyPrintTests(DebuggerTests): |
| 272 | def test_getting_backtrace(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 273 | gdb_output = self.get_stack_trace('id(42)') |
| 274 | self.assertTrue(BREAKPOINT_FN in gdb_output) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f6eb31f | 2013-11-24 14:58:17 +0100 | [diff] [blame] | 276 | def assertGdbRepr(self, val, exp_repr=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 277 | # Ensure that gdb's rendering of the value in a debugged process |
| 278 | # matches repr(value) in this process: |
Antoine Pitrou | f6eb31f | 2013-11-24 14:58:17 +0100 | [diff] [blame] | 279 | gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')') |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 280 | if not exp_repr: |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 281 | exp_repr = repr(val) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 282 | self.assertEqual(gdb_repr, exp_repr, |
| 283 | ('%r did not equal expected %r; full output was:\n%s' |
| 284 | % (gdb_repr, exp_repr, gdb_output))) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 285 | |
| 286 | def test_int(self): |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 287 | 'Verify the pretty-printing of various int values' |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 288 | self.assertGdbRepr(42) |
| 289 | self.assertGdbRepr(0) |
| 290 | self.assertGdbRepr(-7) |
| 291 | self.assertGdbRepr(1000000000000) |
| 292 | self.assertGdbRepr(-1000000000000000) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 293 | |
| 294 | def test_singletons(self): |
| 295 | 'Verify the pretty-printing of True, False and None' |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 296 | self.assertGdbRepr(True) |
| 297 | self.assertGdbRepr(False) |
| 298 | self.assertGdbRepr(None) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 299 | |
| 300 | def test_dicts(self): |
| 301 | 'Verify the pretty-printing of dictionaries' |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 302 | self.assertGdbRepr({}) |
| 303 | self.assertGdbRepr({'foo': 'bar'}, "{'foo': 'bar'}") |
| 304 | self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, "{'douglas': 42, 'foo': 'bar'}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 305 | |
| 306 | def test_lists(self): |
| 307 | 'Verify the pretty-printing of lists' |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 308 | self.assertGdbRepr([]) |
| 309 | self.assertGdbRepr(list(range(5))) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 310 | |
| 311 | def test_bytes(self): |
| 312 | 'Verify the pretty-printing of bytes' |
| 313 | self.assertGdbRepr(b'') |
| 314 | self.assertGdbRepr(b'And now for something hopefully the same') |
| 315 | self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text') |
| 316 | self.assertGdbRepr(b'this is a tab:\t' |
| 317 | b' this is a slash-N:\n' |
| 318 | b' this is a slash-R:\r' |
| 319 | ) |
| 320 | |
| 321 | self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80') |
| 322 | |
| 323 | self.assertGdbRepr(bytes([b for b in range(255)])) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 324 | |
| 325 | def test_strings(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 326 | 'Verify the pretty-printing of unicode strings' |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 327 | encoding = locale.getpreferredencoding() |
| 328 | def check_repr(text): |
| 329 | try: |
| 330 | text.encode(encoding) |
| 331 | printable = True |
| 332 | except UnicodeEncodeError: |
Antoine Pitrou | 4c7c421 | 2010-09-09 20:40:28 +0000 | [diff] [blame] | 333 | self.assertGdbRepr(text, ascii(text)) |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 334 | else: |
| 335 | self.assertGdbRepr(text) |
| 336 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 337 | self.assertGdbRepr('') |
| 338 | self.assertGdbRepr('And now for something hopefully the same') |
| 339 | self.assertGdbRepr('string with embedded NUL here \0 and then some more text') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 340 | |
| 341 | # Test printing a single character: |
| 342 | # U+2620 SKULL AND CROSSBONES |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 343 | check_repr('\u2620') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 344 | |
| 345 | # Test printing a Japanese unicode string |
| 346 | # (I believe this reads "mojibake", using 3 characters from the CJK |
| 347 | # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE) |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 348 | check_repr('\u6587\u5b57\u5316\u3051') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 349 | |
| 350 | # Test a character outside the BMP: |
| 351 | # U+1D121 MUSICAL SYMBOL C CLEF |
| 352 | # This is: |
| 353 | # UTF-8: 0xF0 0x9D 0x84 0xA1 |
| 354 | # UTF-16: 0xD834 0xDD21 |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 355 | check_repr(chr(0x1D121)) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 356 | |
| 357 | def test_tuples(self): |
| 358 | 'Verify the pretty-printing of tuples' |
Victor Stinner | 5132493 | 2013-11-20 12:27:48 +0100 | [diff] [blame] | 359 | self.assertGdbRepr(tuple(), '()') |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 360 | self.assertGdbRepr((1,), '(1,)') |
| 361 | self.assertGdbRepr(('foo', 'bar', 'baz')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 362 | |
| 363 | def test_sets(self): |
| 364 | 'Verify the pretty-printing of sets' |
Antoine Pitrou | a78cccb | 2013-09-22 00:14:27 +0200 | [diff] [blame] | 365 | if (gdb_major_version, gdb_minor_version) < (7, 3): |
| 366 | self.skipTest("pretty-printing of sets needs gdb 7.3 or later") |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 367 | self.assertGdbRepr(set(), 'set()') |
| 368 | self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}") |
| 369 | self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 370 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 371 | # Ensure that we handle sets containing the "dummy" key value, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 372 | # which happens on deletion: |
| 373 | gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b']) |
Victor Stinner | 5132493 | 2013-11-20 12:27:48 +0100 | [diff] [blame] | 374 | s.remove('a') |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 375 | id(s)''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 376 | self.assertEqual(gdb_repr, "{'b'}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 377 | |
| 378 | def test_frozensets(self): |
| 379 | 'Verify the pretty-printing of frozensets' |
Antoine Pitrou | a78cccb | 2013-09-22 00:14:27 +0200 | [diff] [blame] | 380 | if (gdb_major_version, gdb_minor_version) < (7, 3): |
| 381 | self.skipTest("pretty-printing of frozensets needs gdb 7.3 or later") |
Victor Stinner | a282825 | 2013-11-21 10:25:09 +0100 | [diff] [blame] | 382 | self.assertGdbRepr(frozenset(), 'frozenset()') |
| 383 | self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})") |
| 384 | self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 385 | |
| 386 | def test_exceptions(self): |
| 387 | # Test a RuntimeError |
| 388 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 389 | try: |
| 390 | raise RuntimeError("I am an error") |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 391 | except RuntimeError as e: |
| 392 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 393 | ''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 394 | self.assertEqual(gdb_repr, |
| 395 | "RuntimeError('I am an error',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | # Test division by zero: |
| 399 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 400 | try: |
| 401 | a = 1 / 0 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 402 | except ZeroDivisionError as e: |
| 403 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 404 | ''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 405 | self.assertEqual(gdb_repr, |
| 406 | "ZeroDivisionError('division by zero',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 407 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 408 | def test_modern_class(self): |
| 409 | 'Verify the pretty-printing of new-style class instances' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 410 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 411 | class Foo: |
| 412 | pass |
| 413 | foo = Foo() |
| 414 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 415 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 416 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 417 | self.assertTrue(m, |
| 418 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 419 | |
| 420 | def test_subclassing_list(self): |
| 421 | 'Verify the pretty-printing of an instance of a list subclass' |
| 422 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 423 | class Foo(list): |
| 424 | pass |
| 425 | foo = Foo() |
| 426 | foo += [1, 2, 3] |
| 427 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 428 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 429 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 430 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 431 | self.assertTrue(m, |
| 432 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 433 | |
| 434 | def test_subclassing_tuple(self): |
| 435 | 'Verify the pretty-printing of an instance of a tuple subclass' |
| 436 | # This should exercise the negative tp_dictoffset code in the |
| 437 | # new-style class support |
| 438 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 439 | class Foo(tuple): |
| 440 | pass |
| 441 | foo = Foo((1, 2, 3)) |
| 442 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 443 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 444 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 445 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 446 | self.assertTrue(m, |
| 447 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 448 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 449 | def assertSane(self, source, corruption, exprepr=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 450 | '''Run Python under gdb, corrupting variables in the inferior process |
| 451 | immediately before taking a backtrace. |
| 452 | |
| 453 | Verify that the variable's representation is the expected failsafe |
| 454 | representation''' |
| 455 | if corruption: |
| 456 | cmds_after_breakpoint=[corruption, 'backtrace'] |
| 457 | else: |
| 458 | cmds_after_breakpoint=['backtrace'] |
| 459 | |
| 460 | gdb_repr, gdb_output = \ |
| 461 | self.get_gdb_repr(source, |
| 462 | cmds_after_breakpoint=cmds_after_breakpoint) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 463 | if exprepr: |
| 464 | if gdb_repr == exprepr: |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 465 | # gdb managed to print the value in spite of the corruption; |
| 466 | # this is good (see http://bugs.python.org/issue8330) |
| 467 | return |
| 468 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 469 | # Match anything for the type name; 0xDEADBEEF could point to |
| 470 | # something arbitrary (see http://bugs.python.org/issue8330) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 471 | pattern = '<.* at remote 0x-?[0-9a-f]+>' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 472 | |
| 473 | m = re.match(pattern, gdb_repr) |
| 474 | if not m: |
| 475 | self.fail('Unexpected gdb representation: %r\n%s' % \ |
| 476 | (gdb_repr, gdb_output)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 477 | |
| 478 | def test_NULL_ptr(self): |
| 479 | 'Ensure that a NULL PyObject* is handled gracefully' |
| 480 | gdb_repr, gdb_output = ( |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 481 | self.get_gdb_repr('id(42)', |
| 482 | cmds_after_breakpoint=['set variable v=0', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 483 | 'backtrace']) |
| 484 | ) |
| 485 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 486 | self.assertEqual(gdb_repr, '0x0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 487 | |
| 488 | def test_NULL_ob_type(self): |
| 489 | 'Ensure that a PyObject* with NULL ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 490 | self.assertSane('id(42)', |
| 491 | 'set v->ob_type=0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 492 | |
| 493 | def test_corrupt_ob_type(self): |
| 494 | 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 495 | self.assertSane('id(42)', |
| 496 | 'set v->ob_type=0xDEADBEEF', |
| 497 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 498 | |
| 499 | def test_corrupt_tp_flags(self): |
| 500 | 'Ensure that a PyObject* with a type with corrupt tp_flags is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 501 | self.assertSane('id(42)', |
| 502 | 'set v->ob_type->tp_flags=0x0', |
| 503 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 504 | |
| 505 | def test_corrupt_tp_name(self): |
| 506 | 'Ensure that a PyObject* with a type with corrupt tp_name is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 507 | self.assertSane('id(42)', |
| 508 | 'set v->ob_type->tp_name=0xDEADBEEF', |
| 509 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 510 | |
| 511 | def test_builtins_help(self): |
| 512 | 'Ensure that the new-style class _Helper in site.py can be handled' |
| 513 | # (this was the issue causing tracebacks in |
| 514 | # http://bugs.python.org/issue8032#msg100537 ) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 515 | gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 516 | |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 517 | m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 518 | self.assertTrue(m, |
| 519 | msg='Unexpected rendering %r' % gdb_repr) |
| 520 | |
| 521 | def test_selfreferential_list(self): |
| 522 | '''Ensure that a reference loop involving a list doesn't lead proxyval |
| 523 | into an infinite loop:''' |
| 524 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 525 | self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 526 | self.assertEqual(gdb_repr, '[3, 4, 5, [...]]') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 527 | |
| 528 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 529 | self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 530 | self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 531 | |
| 532 | def test_selfreferential_dict(self): |
| 533 | '''Ensure that a reference loop involving a dict doesn't lead proxyval |
| 534 | into an infinite loop:''' |
| 535 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 536 | self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 537 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 538 | self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 539 | |
| 540 | def test_selfreferential_old_style_instance(self): |
| 541 | gdb_repr, gdb_output = \ |
| 542 | self.get_gdb_repr(''' |
| 543 | class Foo: |
| 544 | pass |
| 545 | foo = Foo() |
| 546 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 547 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 548 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 549 | gdb_repr), |
| 550 | 'Unexpected gdb representation: %r\n%s' % \ |
| 551 | (gdb_repr, gdb_output)) |
| 552 | |
| 553 | def test_selfreferential_new_style_instance(self): |
| 554 | gdb_repr, gdb_output = \ |
| 555 | self.get_gdb_repr(''' |
| 556 | class Foo(object): |
| 557 | pass |
| 558 | foo = Foo() |
| 559 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 560 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 561 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 562 | gdb_repr), |
| 563 | 'Unexpected gdb representation: %r\n%s' % \ |
| 564 | (gdb_repr, gdb_output)) |
| 565 | |
| 566 | gdb_repr, gdb_output = \ |
| 567 | self.get_gdb_repr(''' |
| 568 | class Foo(object): |
| 569 | pass |
| 570 | a = Foo() |
| 571 | b = Foo() |
| 572 | a.an_attr = b |
| 573 | b.an_attr = a |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 574 | id(a)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 575 | self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 576 | gdb_repr), |
| 577 | 'Unexpected gdb representation: %r\n%s' % \ |
| 578 | (gdb_repr, gdb_output)) |
| 579 | |
| 580 | def test_truncation(self): |
| 581 | 'Verify that very long output is truncated' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 582 | gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 583 | self.assertEqual(gdb_repr, |
| 584 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, " |
| 585 | "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, " |
| 586 | "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, " |
| 587 | "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, " |
| 588 | "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, " |
| 589 | "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, " |
| 590 | "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, " |
| 591 | "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, " |
| 592 | "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, " |
| 593 | "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, " |
| 594 | "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, " |
| 595 | "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, " |
| 596 | "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, " |
| 597 | "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, " |
| 598 | "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, " |
| 599 | "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, " |
| 600 | "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, " |
| 601 | "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, " |
| 602 | "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, " |
| 603 | "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, " |
| 604 | "224, 225, 226...(truncated)") |
| 605 | self.assertEqual(len(gdb_repr), |
| 606 | 1024 + len('...(truncated)')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 607 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 608 | def test_builtin_method(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 609 | gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 610 | self.assertTrue(re.match('<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 611 | gdb_repr), |
| 612 | 'Unexpected gdb representation: %r\n%s' % \ |
| 613 | (gdb_repr, gdb_output)) |
| 614 | |
| 615 | def test_frames(self): |
| 616 | gdb_output = self.get_stack_trace(''' |
| 617 | def foo(a, b, c): |
| 618 | pass |
| 619 | |
| 620 | foo(3, 4, 5) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 621 | id(foo.__code__)''', |
| 622 | breakpoint='builtin_id', |
| 623 | cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)'] |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 624 | ) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 625 | self.assertTrue(re.match('.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 626 | gdb_output, |
| 627 | re.DOTALL), |
| 628 | 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) |
| 629 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 630 | @unittest.skipIf(python_is_optimized(), |
| 631 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 632 | class PyListTests(DebuggerTests): |
| 633 | def assertListing(self, expected, actual): |
| 634 | self.assertEndsWith(actual, expected) |
| 635 | |
| 636 | def test_basic_command(self): |
| 637 | 'Verify that the "py-list" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 638 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 639 | cmds_after_breakpoint=['py-list']) |
| 640 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 641 | self.assertListing(' 5 \n' |
| 642 | ' 6 def bar(a, b, c):\n' |
| 643 | ' 7 baz(a, b, c)\n' |
| 644 | ' 8 \n' |
| 645 | ' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 646 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 647 | ' 11 \n' |
| 648 | ' 12 foo(1, 2, 3)\n', |
| 649 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 650 | |
| 651 | def test_one_abs_arg(self): |
| 652 | 'Verify the "py-list" command with one absolute argument' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 653 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 654 | cmds_after_breakpoint=['py-list 9']) |
| 655 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 656 | self.assertListing(' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 657 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 658 | ' 11 \n' |
| 659 | ' 12 foo(1, 2, 3)\n', |
| 660 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 661 | |
| 662 | def test_two_abs_args(self): |
| 663 | 'Verify the "py-list" command with two absolute arguments' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 664 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 665 | cmds_after_breakpoint=['py-list 1,3']) |
| 666 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 667 | self.assertListing(' 1 # Sample script for use by test_gdb.py\n' |
| 668 | ' 2 \n' |
| 669 | ' 3 def foo(a, b, c):\n', |
| 670 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 671 | |
| 672 | class StackNavigationTests(DebuggerTests): |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 673 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 674 | @unittest.skipIf(python_is_optimized(), |
| 675 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 676 | def test_pyup_command(self): |
| 677 | 'Verify that the "py-up" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 678 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 679 | cmds_after_breakpoint=['py-up']) |
| 680 | self.assertMultilineMatches(bt, |
| 681 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 682 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 683 | baz\(a, b, c\) |
| 684 | $''') |
| 685 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 686 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 687 | def test_down_at_bottom(self): |
| 688 | 'Verify handling of "py-down" at the bottom of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 689 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 690 | cmds_after_breakpoint=['py-down']) |
| 691 | self.assertEndsWith(bt, |
| 692 | 'Unable to find a newer python frame\n') |
| 693 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 694 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 695 | def test_up_at_top(self): |
| 696 | 'Verify handling of "py-up" at the top of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 697 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 698 | cmds_after_breakpoint=['py-up'] * 4) |
| 699 | self.assertEndsWith(bt, |
| 700 | 'Unable to find an older python frame\n') |
| 701 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 702 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 703 | @unittest.skipIf(python_is_optimized(), |
| 704 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 705 | def test_up_then_down(self): |
| 706 | 'Verify "py-up" followed by "py-down"' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 707 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 708 | cmds_after_breakpoint=['py-up', 'py-down']) |
| 709 | self.assertMultilineMatches(bt, |
| 710 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 711 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 712 | baz\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 713 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 714 | id\(42\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 715 | $''') |
| 716 | |
| 717 | class PyBtTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 718 | @unittest.skipIf(python_is_optimized(), |
| 719 | "Python was compiled with optimizations") |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 720 | def test_bt(self): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 721 | 'Verify that the "py-bt" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 722 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 723 | cmds_after_breakpoint=['py-bt']) |
| 724 | self.assertMultilineMatches(bt, |
| 725 | r'''^.* |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 726 | Traceback \(most recent call first\): |
| 727 | File ".*gdb_sample.py", line 10, in baz |
| 728 | id\(42\) |
| 729 | File ".*gdb_sample.py", line 7, in bar |
| 730 | baz\(a, b, c\) |
| 731 | File ".*gdb_sample.py", line 4, in foo |
| 732 | bar\(a, b, c\) |
| 733 | File ".*gdb_sample.py", line 12, in <module> |
| 734 | foo\(1, 2, 3\) |
| 735 | ''') |
| 736 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 737 | @unittest.skipIf(python_is_optimized(), |
| 738 | "Python was compiled with optimizations") |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 739 | def test_bt_full(self): |
| 740 | 'Verify that the "py-bt-full" command works' |
| 741 | bt = self.get_stack_trace(script=self.get_sample_script(), |
| 742 | cmds_after_breakpoint=['py-bt-full']) |
| 743 | self.assertMultilineMatches(bt, |
| 744 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 745 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 746 | baz\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 747 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 748 | bar\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 749 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\) |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 750 | foo\(1, 2, 3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 751 | ''') |
| 752 | |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 753 | @unittest.skipUnless(_thread, |
| 754 | "Python was compiled without thread support") |
| 755 | def test_threads(self): |
| 756 | 'Verify that "py-bt" indicates threads that are waiting for the GIL' |
| 757 | cmd = ''' |
| 758 | from threading import Thread |
| 759 | |
| 760 | class TestThread(Thread): |
| 761 | # These threads would run forever, but we'll interrupt things with the |
| 762 | # debugger |
| 763 | def run(self): |
| 764 | i = 0 |
| 765 | while 1: |
| 766 | i += 1 |
| 767 | |
| 768 | t = {} |
| 769 | for i in range(4): |
| 770 | t[i] = TestThread() |
| 771 | t[i].start() |
| 772 | |
| 773 | # Trigger a breakpoint on the main thread |
| 774 | id(42) |
| 775 | |
| 776 | ''' |
| 777 | # Verify with "py-bt": |
| 778 | gdb_output = self.get_stack_trace(cmd, |
| 779 | cmds_after_breakpoint=['thread apply all py-bt']) |
| 780 | self.assertIn('Waiting for the GIL', gdb_output) |
| 781 | |
| 782 | # Verify with "py-bt-full": |
| 783 | gdb_output = self.get_stack_trace(cmd, |
| 784 | cmds_after_breakpoint=['thread apply all py-bt-full']) |
| 785 | self.assertIn('Waiting for the GIL', gdb_output) |
| 786 | |
| 787 | @unittest.skipIf(python_is_optimized(), |
| 788 | "Python was compiled with optimizations") |
| 789 | # Some older versions of gdb will fail with |
| 790 | # "Cannot find new threads: generic error" |
| 791 | # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround |
| 792 | @unittest.skipUnless(_thread, |
| 793 | "Python was compiled without thread support") |
| 794 | def test_gc(self): |
| 795 | 'Verify that "py-bt" indicates if a thread is garbage-collecting' |
| 796 | cmd = ('from gc import collect\n' |
| 797 | 'id(42)\n' |
| 798 | 'def foo():\n' |
| 799 | ' collect()\n' |
| 800 | 'def bar():\n' |
| 801 | ' foo()\n' |
| 802 | 'bar()\n') |
| 803 | # Verify with "py-bt": |
| 804 | gdb_output = self.get_stack_trace(cmd, |
| 805 | cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'], |
| 806 | ) |
| 807 | self.assertIn('Garbage-collecting', gdb_output) |
| 808 | |
| 809 | # Verify with "py-bt-full": |
| 810 | gdb_output = self.get_stack_trace(cmd, |
| 811 | cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'], |
| 812 | ) |
| 813 | self.assertIn('Garbage-collecting', gdb_output) |
| 814 | |
| 815 | @unittest.skipIf(python_is_optimized(), |
| 816 | "Python was compiled with optimizations") |
| 817 | # Some older versions of gdb will fail with |
| 818 | # "Cannot find new threads: generic error" |
| 819 | # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround |
| 820 | @unittest.skipUnless(_thread, |
| 821 | "Python was compiled without thread support") |
| 822 | def test_pycfunction(self): |
| 823 | 'Verify that "py-bt" displays invocations of PyCFunction instances' |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 824 | # Tested function must not be defined with METH_NOARGS or METH_O, |
| 825 | # otherwise call_function() doesn't call PyCFunction_Call() |
| 826 | cmd = ('from time import gmtime\n' |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 827 | 'def foo():\n' |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 828 | ' gmtime(1)\n' |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 829 | 'def bar():\n' |
| 830 | ' foo()\n' |
| 831 | 'bar()\n') |
| 832 | # Verify with "py-bt": |
| 833 | gdb_output = self.get_stack_trace(cmd, |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 834 | breakpoint='time_gmtime', |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 835 | cmds_after_breakpoint=['bt', 'py-bt'], |
| 836 | ) |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 837 | self.assertIn('<built-in method gmtime', gdb_output) |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 838 | |
| 839 | # Verify with "py-bt-full": |
| 840 | gdb_output = self.get_stack_trace(cmd, |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 841 | breakpoint='time_gmtime', |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 842 | cmds_after_breakpoint=['py-bt-full'], |
| 843 | ) |
Victor Stinner | 79644f9 | 2015-03-27 15:42:37 +0100 | [diff] [blame] | 844 | self.assertIn('#0 <built-in method gmtime', gdb_output) |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 845 | |
| 846 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 847 | class PyPrintTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 848 | @unittest.skipIf(python_is_optimized(), |
| 849 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 850 | def test_basic_command(self): |
| 851 | 'Verify that the "py-print" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 852 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 853 | cmds_after_breakpoint=['py-print args']) |
| 854 | self.assertMultilineMatches(bt, |
| 855 | r".*\nlocal 'args' = \(1, 2, 3\)\n.*") |
| 856 | |
Vinay Sajip | 2549f87 | 2012-01-04 12:07:30 +0000 | [diff] [blame] | 857 | @unittest.skipIf(python_is_optimized(), |
| 858 | "Python was compiled with optimizations") |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 859 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 860 | def test_print_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 861 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 862 | cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) |
| 863 | self.assertMultilineMatches(bt, |
| 864 | r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") |
| 865 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 866 | @unittest.skipIf(python_is_optimized(), |
| 867 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 868 | def test_printing_global(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 869 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 870 | cmds_after_breakpoint=['py-print __name__']) |
| 871 | self.assertMultilineMatches(bt, |
| 872 | r".*\nglobal '__name__' = '__main__'\n.*") |
| 873 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 874 | @unittest.skipIf(python_is_optimized(), |
| 875 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 876 | def test_printing_builtin(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 877 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 878 | cmds_after_breakpoint=['py-print len']) |
| 879 | self.assertMultilineMatches(bt, |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 880 | r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 881 | |
| 882 | class PyLocalsTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 883 | @unittest.skipIf(python_is_optimized(), |
| 884 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 885 | def test_basic_command(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 886 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 887 | cmds_after_breakpoint=['py-locals']) |
| 888 | self.assertMultilineMatches(bt, |
| 889 | r".*\nargs = \(1, 2, 3\)\n.*") |
| 890 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 891 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Vinay Sajip | 2549f87 | 2012-01-04 12:07:30 +0000 | [diff] [blame] | 892 | @unittest.skipIf(python_is_optimized(), |
| 893 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 894 | def test_locals_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 895 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 896 | cmds_after_breakpoint=['py-up', 'py-locals']) |
| 897 | self.assertMultilineMatches(bt, |
| 898 | r".*\na = 1\nb = 2\nc = 3\n.*") |
| 899 | |
| 900 | def test_main(): |
Antoine Pitrou | d0f3e07 | 2013-09-21 23:56:17 +0200 | [diff] [blame] | 901 | if support.verbose: |
Victor Stinner | 2f3ac1e | 2015-09-02 23:12:14 +0200 | [diff] [blame] | 902 | print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version)) |
Victor Stinner | 5b6b4a8 | 2015-09-02 23:19:55 +0200 | [diff] [blame] | 903 | for line in gdb_version.splitlines(): |
Antoine Pitrou | d0f3e07 | 2013-09-21 23:56:17 +0200 | [diff] [blame] | 904 | print(" " * 4 + line) |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 905 | run_unittest(PrettyPrintTests, |
| 906 | PyListTests, |
| 907 | StackNavigationTests, |
| 908 | PyBtTests, |
| 909 | PyPrintTests, |
| 910 | PyLocalsTests |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 911 | ) |
| 912 | |
| 913 | if __name__ == "__main__": |
| 914 | test_main() |