diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 920c018084b81c..931031e3a04399 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -414,6 +414,11 @@ The :mod:`test.support` module defines the following functions: Return ``True`` if Python was not built with ``-O0`` or ``-Og``. +.. function:: python_has_debug_info() + + Return ``True`` if Python was built with ``-g``. + + .. function:: with_pymalloc() Return :data:`_testcapi.WITH_PYMALLOC`. diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index e9edf675b9105a..920c00c88adc1c 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -555,6 +555,7 @@ def collect_test_support(info_add): call_func(info_add, 'test_support._is_gui_available', support, '_is_gui_available') call_func(info_add, 'test_support.python_is_optimized', support, 'python_is_optimized') + call_func(info_add, 'test_support.python_has_debug_info', support, 'python_has_debug_info') def collect_cc(info_add): diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index b5538d22fb2ace..8dde47d3efc949 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1643,6 +1643,16 @@ def disable_gc(): gc.enable() +def python_has_debug_info(): + """Find if Python was built with debug information.""" + cflags = sysconfig.get_config_var('PY_CFLAGS') or '' + final_opt = "" + for opt in cflags.split(): + if opt.startswith('-g'): + final_opt = opt + return final_opt == '-g' + + def python_is_optimized(): """Find if Python was built with optimizations.""" cflags = sysconfig.get_config_var('PY_CFLAGS') or '' diff --git a/Lib/test/test_gdb.py b/Lib/test/test_gdb.py index 1c5e18b678ca7f..b8fa26c488a5f6 100644 --- a/Lib/test/test_gdb.py +++ b/Lib/test/test_gdb.py @@ -13,7 +13,7 @@ import unittest from test import support -from test.support import run_unittest, findfile, python_is_optimized +from test.support import run_unittest, findfile, python_has_debug_info def get_gdb_version(): try: @@ -657,8 +657,8 @@ def foo(a, b, c): re.DOTALL), 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) -@unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") +@unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") class PyListTests(DebuggerTests): def assertListing(self, expected, actual): self.assertEndsWith(actual, expected) @@ -701,8 +701,8 @@ def test_two_abs_args(self): class StackNavigationTests(DebuggerTests): @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_pyup_command(self): 'Verify that the "py-up" command works' bt = self.get_stack_trace(script=self.get_sample_script(), @@ -730,8 +730,8 @@ def test_up_at_top(self): 'Unable to find an older python frame\n') @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_up_then_down(self): 'Verify "py-up" followed by "py-down"' bt = self.get_stack_trace(script=self.get_sample_script(), @@ -745,8 +745,8 @@ def test_up_then_down(self): $''') class PyBtTests(DebuggerTests): - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_bt(self): 'Verify that the "py-bt" command works' bt = self.get_stack_trace(script=self.get_sample_script(), @@ -765,8 +765,8 @@ def test_bt(self): foo\(1, 2, 3\) ''') - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_bt_full(self): 'Verify that the "py-bt-full" command works' bt = self.get_stack_trace(script=self.get_sample_script(), @@ -813,8 +813,8 @@ def run(self): cmds_after_breakpoint=['thread apply all py-bt-full']) self.assertIn('Waiting for the GIL', gdb_output) - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") # Some older versions of gdb will fail with # "Cannot find new threads: generic error" # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround @@ -839,8 +839,8 @@ def test_gc(self): ) self.assertIn('Garbage-collecting', gdb_output) - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") # Some older versions of gdb will fail with # "Cannot find new threads: generic error" # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround @@ -881,8 +881,8 @@ def test_pycfunction(self): gdb_output, ) - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_wrapper_call(self): cmd = textwrap.dedent(''' class MyList(list): @@ -908,8 +908,8 @@ def __init__(self): class PyPrintTests(DebuggerTests): - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_basic_command(self): 'Verify that the "py-print" command works' bt = self.get_stack_trace(script=self.get_sample_script(), @@ -917,8 +917,8 @@ def test_basic_command(self): self.assertMultilineMatches(bt, r".*\nlocal 'args' = \(1, 2, 3\)\n.*") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") def test_print_after_up(self): bt = self.get_stack_trace(script=self.get_sample_script(), @@ -926,16 +926,16 @@ def test_print_after_up(self): self.assertMultilineMatches(bt, r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_printing_global(self): bt = self.get_stack_trace(script=self.get_sample_script(), cmds_after_breakpoint=['py-up', 'py-print __name__']) self.assertMultilineMatches(bt, r".*\nglobal '__name__' = '__main__'\n.*") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_printing_builtin(self): bt = self.get_stack_trace(script=self.get_sample_script(), cmds_after_breakpoint=['py-up', 'py-print len']) @@ -943,8 +943,8 @@ def test_printing_builtin(self): r".*\nbuiltin 'len' = \n.*") class PyLocalsTests(DebuggerTests): - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_basic_command(self): bt = self.get_stack_trace(script=self.get_sample_script(), cmds_after_breakpoint=['py-up', 'py-locals']) @@ -952,8 +952,8 @@ def test_basic_command(self): r".*\nargs = \(1, 2, 3\)\n.*") @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") - @unittest.skipIf(python_is_optimized(), - "Python was compiled with optimizations") + @unittest.skipUnless(python_has_debug_info(), + "Python was compiled without debug info") def test_locals_after_up(self): bt = self.get_stack_trace(script=self.get_sample_script(), cmds_after_breakpoint=['py-up', 'py-up', 'py-locals']) diff --git a/Misc/NEWS.d/next/Tests/2019-06-24-10-44-47.bpo-37382.ykFAk6.rst b/Misc/NEWS.d/next/Tests/2019-06-24-10-44-47.bpo-37382.ykFAk6.rst new file mode 100644 index 00000000000000..0a1e8c7e85d411 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2019-06-24-10-44-47.bpo-37382.ykFAk6.rst @@ -0,0 +1,3 @@ +GDB tests are now run whenever Python is compiled with debug info, +regardless of optimization flags. A new function +``test.support.python_has_debug_info`` is added for this.