Made tests less verbose by not using distutils.core.setup + skip debugger tests when...
authorMark Florisson <markflorisson88@gmail.com>
Sun, 5 Dec 2010 13:58:29 +0000 (14:58 +0100)
committerMark Florisson <markflorisson88@gmail.com>
Sun, 5 Dec 2010 13:58:29 +0000 (14:58 +0100)
Cython/Debugger/Cygdb.py
Cython/Debugger/Tests/TestLibCython.py
Cython/Debugger/Tests/test_libcython_in_gdb.py
Cython/Debugger/libpython.py
bin/cygdb
cygdb.py
runtests.py

index eefbdbe0994574b7272b4c645e10541126733967..d4ebe3102e456bcefd83c5a52dcde286f95669db 100644 (file)
@@ -36,7 +36,7 @@ def make_command_file(path_to_debug_info, prefix_code='', no_import=False):
     f.write(prefix_code)
     f.write('set breakpoint pending on\n')
     f.write("set print pretty on\n")
-    f.write('python from Cython.Debugger import libcython\n')
+    f.write('python from Cython.Debugger import libcython, libpython\n')
     
     if no_import:
         # don't do this, this overrides file command in .gdbinit
index 082657a5f030b396c2ac85091f328460728c5941..8489c79e544bb1924cff146eabbb1330439017c5 100644 (file)
@@ -5,6 +5,7 @@ import re
 import sys
 import uuid
 import shutil
+import warnings
 import textwrap
 import unittest
 import tempfile
@@ -13,6 +14,7 @@ import distutils.core
 from distutils import sysconfig
 from distutils import ccompiler
 
+import runtests
 import Cython.Distutils.extension
 from Cython.Debugger import Cygdb as cygdb
 
@@ -45,18 +47,48 @@ class DebuggerTestCase(unittest.TestCase):
         compiler = ccompiler.new_compiler()
         compiler.compile(['cfuncs.c'], debug=True)
         
-        ext = Cython.Distutils.extension.Extension(
-            'codefile',
-            ['codefile.pyx'], 
-            pyrex_gdb=True,
-            extra_objects=['cfuncs.o'])
-            
-        distutils.core.setup(
-            script_args=['build_ext', '--inplace'],
-            ext_modules=[ext],
-            cmdclass=dict(build_ext=Cython.Distutils.build_ext)
+        opts = dict(
+            test_directory=self.tempdir,
+            module='codefile',
         )
-    
+        
+        cython_compile_testcase = runtests.CythonCompileTestCase(
+            workdir=self.tempdir,
+            # we clean up everything (not only compiled files)
+            cleanup_workdir=False,
+            **opts
+        )
+        
+        cython_compile_testcase.run_cython(
+            targetdir=self.tempdir,
+            incdir=None,
+            annotate=False,
+            extra_compile_options={
+                'gdb_debug':True,
+                'output_dir':self.tempdir,
+            },
+            **opts
+        )
+        
+        cython_compile_testcase.run_distutils(
+            incdir=None,
+            workdir=self.tempdir,
+            extra_extension_args={'extra_objects':['cfuncs.o']},
+            **opts
+        )
+        
+        # ext = Cython.Distutils.extension.Extension(
+            # 'codefile',
+            # ['codefile.pyx'], 
+            # pyrex_gdb=True,
+            # extra_objects=['cfuncs.o'])
+        # 
+        # distutils.core.setup(
+            # script_args=['build_ext', '--inplace'],
+            # ext_modules=[ext],
+            # cmdclass=dict(build_ext=Cython.Distutils.build_ext)
+        # )
+        
     def tearDown(self):
         os.chdir(self.cwd)
         shutil.rmtree(self.tempdir)
@@ -109,23 +141,47 @@ class GdbDebuggerTestCase(DebuggerTestCase):
         paths.append(os.path.dirname(os.path.dirname(
             os.path.abspath(Cython.__file__))))
         env = dict(os.environ, PYTHONPATH=os.pathsep.join(paths))
+        
+        try:
+            p = subprocess.Popen(['gdb', '-v'], stdout=subprocess.PIPE)
+            have_gdb = True
+        except OSError:
+            # gdb was not installed
+            have_gdb = False
+        else:
+            gdb_version = p.stdout.read()
+            p.wait()
+            p.stdout.close()
+        
+        if have_gdb:
+            # Based on Lib/test/test_gdb.py
+            regex = "^GNU gdb [^\d]*(\d+)\.(\d+)"
+            gdb_version_number = re.search(regex, gdb_version).groups()
 
-        self.p = subprocess.Popen(
-            args,
-            stdout=open(os.devnull, 'w'),
-            stderr=subprocess.PIPE,
-            env=env)
+        if not have_gdb or map(int, gdb_version_number) < [7, 2]:
+            self.p = None
+            warnings.warn('Skipping gdb tests, need gdb >= 7.2')
+        else:
+            self.p = subprocess.Popen(
+                args,
+                stdout=open(os.devnull, 'w'),
+                stderr=subprocess.PIPE,
+                env=env)
         
     def tearDown(self):
         super(GdbDebuggerTestCase, self).tearDown()
-        self.p.stderr.close()
-        self.p.wait()
+        if self.p:
+            self.p.stderr.close()
+            self.p.wait()
         os.remove(self.gdb_command_file)
         
    
 class TestAll(GdbDebuggerTestCase):
     
     def test_all(self):
+        if self.p is None:
+            return
+            
         out, err = self.p.communicate()
         border = '*' * 30
         start = '%s   v INSIDE GDB v   %s' % (border, border)
index 583d7db8bf7fa3a9285ff83c710c8bb62eab7873..1d61cd23608571fc2499cb9660f2b30e3d66fd93 100644 (file)
@@ -15,6 +15,7 @@ import unittest
 import textwrap
 import tempfile
 import traceback
+import itertools
 from test import test_support
 
 import gdb
@@ -248,6 +249,11 @@ class TestBacktrace(DebugTestCase):
         
         self.break_and_run('os.path.join("foo", "bar")')
         result = gdb.execute('cy bt', to_string=True)
+        
+        _debug(libpython.execute, libpython._execute, gdb.execute)
+        _debug(gdb.execute('cy list', to_string=True))
+        _debug(repr(result))
+        
         assert re.search(r'\#\d+ *0x.* in spam\(\) at .*codefile\.pyx:22', 
                          result), result
         assert 'os.path.join("foo", "bar")' in result, result
@@ -339,6 +345,16 @@ class TestExec(DebugTestCase):
         self.assertEqual('14', self.eval_command('some_random_var'))
 
 
+_do_debug = os.environ.get('CYTHON_GDB_DEBUG')
+if _do_debug:
+    _debug_file = open('/dev/tty', 'w')
+
+def _debug(*messages):
+    if _do_debug:
+        messages = itertools.chain([sys._getframe(1).f_code.co_name], 
+                                   messages)
+        _debug_file.write(' '.join(str(msg) for msg in messages) + '\n')
+
 def _main():
     try:
         gdb.lookup_type('PyModuleObject')
index 07a57a49c0f7dde4fed380bb4f6c2cc63a4424cd..b871756eeba2ef296163f21f20793f502bd0b5d7 100644 (file)
@@ -59,9 +59,13 @@ import itertools
 
 import gdb
 
-# I think this is the only way to fix this bug :'(
-# http://sourceware.org/bugzilla/show_bug.cgi?id=12285
-reload(sys).setdefaultencoding('UTF-8')
+if sys.version_info[0] < 3:
+    # I think this is the only way to fix this bug :'(
+    # http://sourceware.org/bugzilla/show_bug.cgi?id=12285
+    out, err = sys.stdout, sys.stderr
+    reload(sys).setdefaultencoding('UTF-8')
+    sys.stdout = out
+    sys.stderr = err
 
 # Look up the gdb.Type for some standard types:
 _type_char_ptr = gdb.lookup_type('char').pointer() # char*
index 0452d0b1c5c7598e86387e25c2b1eeed894164ed..7f2d57f5d606a0c5eff5e1dbabae9c2de1b3b2c1 100755 (executable)
--- a/bin/cygdb
+++ b/bin/cygdb
@@ -5,4 +5,4 @@ import sys
 from Cython.Debugger import Cygdb as cygdb
 
 if __name__ == '__main__':
-        cygdb.main()
+    cygdb.main()
index 0452d0b1c5c7598e86387e25c2b1eeed894164ed..7f2d57f5d606a0c5eff5e1dbabae9c2de1b3b2c1 100644 (file)
--- a/cygdb.py
+++ b/cygdb.py
@@ -5,4 +5,4 @@ import sys
 from Cython.Debugger import Cygdb as cygdb
 
 if __name__ == '__main__':
-        cygdb.main()
+    cygdb.main()
index 1fa0df42ec00970a5739902125cf756c8e0f8ebf..8faf3cf3b28d4e89ab07dc7b738e6b2f67d460eb 100644 (file)
@@ -342,15 +342,27 @@ class CythonCompileTestCase(unittest.TestCase):
         else:
             return geterrors()
 
-    def run_cython(self, test_directory, module, targetdir, incdir, annotate):
+    def run_cython(self, test_directory, module, targetdir, incdir, annotate,
+                   extra_compile_options=None):
         include_dirs = INCLUDE_DIRS[:]
         if incdir:
             include_dirs.append(incdir)
         source = self.find_module_source_file(
             os.path.join(test_directory, module + '.pyx'))
         target = os.path.join(targetdir, self.build_target_filename(module))
+        
+        if extra_compile_options is None:
+            extra_compile_options = {}
+        
+        try:
+            CompilationOptions
+        except NameError:
+            from Cython.Compiler.Main import CompilationOptions
+            from Cython.Compiler.Main import compile as cython_compile
+            from Cython.Compiler.Main import default_options
+        
         options = CompilationOptions(
-            pyrex_default_options,
+            default_options,
             include_path = include_dirs,
             output_file = target,
             annotate = annotate,
@@ -359,11 +371,13 @@ class CythonCompileTestCase(unittest.TestCase):
             language_level = self.language_level,
             generate_pxi = False,
             evaluate_tree_assertions = True,
+            **extra_compile_options
             )
         cython_compile(source, options=options,
                        full_module_name=module)
 
-    def run_distutils(self, test_directory, module, workdir, incdir):
+    def run_distutils(self, test_directory, module, workdir, incdir, 
+                      extra_extension_args=None):
         cwd = os.getcwd()
         os.chdir(workdir)
         try:
@@ -377,11 +391,16 @@ class CythonCompileTestCase(unittest.TestCase):
                 if match(module):
                     ext_include_dirs += get_additional_include_dirs()
             self.copy_related_files(test_directory, workdir, module)
+            
+            if extra_extension_args is None:
+                extra_extension_args = {}
+            
             extension = Extension(
                 module,
                 sources = self.find_source_files(workdir, module),
                 include_dirs = ext_include_dirs,
                 extra_compile_args = CFLAGS,
+                **extra_extension_args
                 )
             if self.language == 'cpp':
                 extension.language = 'c++'