Avoid more gdb errors for insufficient gdb.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 28 Jan 2011 08:19:24 +0000 (00:19 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 28 Jan 2011 08:19:24 +0000 (00:19 -0800)
Cython/Debugger/Tests/TestLibCython.py

index 20da0c582d0047d1f42375dd20bd46d20edb48c6..1b37d44fed546b0721edca0b2bc322af1faeca3b 100644 (file)
@@ -29,6 +29,49 @@ with open(codefile) as f:
 # can't access the module anymore. Get it from sys.modules instead.
 build_ext = sys.modules['Cython.Distutils.build_ext']
 
+
+have_gdb = None
+def test_gdb():
+    global have_gdb
+    if have_gdb is None:
+        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().decode('ascii')
+            p.wait()
+            p.stdout.close()
+
+        if have_gdb:
+            # Based on Lib/test/test_gdb.py
+            regex = "^GNU gdb [^\d]*(\d+)\.(\d+)"
+            gdb_version_number = list(map(int, re.search(regex, gdb_version).groups()))
+            
+            if gdb_version_number >= [7, 2]:
+                python_version_script = tempfile.NamedTemporaryFile(mode='w+')
+                python_version_script.write(
+                    'python import sys; print("%s %s" % sys.version_info[:2])')
+                python_version_script.flush()
+                p = subprocess.Popen(['gdb', '-batch', '-x', python_version_script.name],
+                                     stdout=subprocess.PIPE)
+                python_version = p.stdout.read().decode('ascii')
+                p.wait()
+                python_version_number = list(map(int, python_version.split()))
+
+        # Be Python 3 compatible
+        if (not have_gdb
+            or gdb_version_number < [7, 2]
+            or python_version_number < [2, 6]):
+            warnings.warn(
+                'Skipping gdb tests, need gdb >= 7.2 with Python >= 2.6')
+            have_gdb = False
+
+    return have_gdb
+
+
 class DebuggerTestCase(unittest.TestCase):
 
     def setUp(self):
@@ -36,6 +79,9 @@ class DebuggerTestCase(unittest.TestCase):
         Run gdb and have cygdb import the debug information from the code
         defined in TestParseTreeTransforms's setUp method
         """
+        if not test_gdb():
+            return
+
         self.tempdir = tempfile.mkdtemp()
         self.destfile = os.path.join(self.tempdir, 'codefile.pyx')
         self.debug_dest = os.path.join(self.tempdir,
@@ -100,6 +146,8 @@ class DebuggerTestCase(unittest.TestCase):
         # )
 
     def tearDown(self):
+        if not test_gdb():
+            return
         os.chdir(self.cwd)
         shutil.rmtree(self.tempdir)
 
@@ -107,6 +155,9 @@ class DebuggerTestCase(unittest.TestCase):
 class GdbDebuggerTestCase(DebuggerTestCase):
 
     def setUp(self):
+        if not test_gdb():
+            return
+
         super(GdbDebuggerTestCase, self).setUp()
 
         prefix_code = textwrap.dedent('''\
@@ -166,25 +217,26 @@ class GdbDebuggerTestCase(DebuggerTestCase):
             p.wait()
             p.stdout.close()
 
-        if have_gdb:
-            python_version_script = tempfile.NamedTemporaryFile(mode='w+')
-            python_version_script.write(
-                'python import sys; print("%s %s" % sys.version_info[:2])')
-            python_version_script.flush()
-            p = subprocess.Popen(['gdb', '-batch', '-x', python_version_script.name],
-                                 stdout=subprocess.PIPE)
-            python_version = p.stdout.read().decode('ascii')
-            p.wait()
-            python_version_number = [int(a) for a in python_version.split()]
-
         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()
-
+            gdb_version_number = list(map(int, re.search(regex, gdb_version).groups()))
+            
+            if gdb_version_number >= [7, 2]:
+                python_version_script = tempfile.NamedTemporaryFile(mode='w+')
+                python_version_script.write(
+                    'python import sys; print("%s %s" % sys.version_info[:2])')
+                python_version_script.flush()
+                p = subprocess.Popen(['gdb', '-batch', '-x', python_version_script.name],
+                                     stdout=subprocess.PIPE)
+                python_version = p.stdout.read().decode('ascii')
+                p.wait()
+                python_version_number = list(map(int, python_version.split()))
+
+        
         # Be Python 3 compatible
         if (not have_gdb
-            or list(map(int, gdb_version_number)) < [7, 2]
+            or gdb_version_number < [7, 2]
             or python_version_number < [2, 6]):
             self.p = None
             warnings.warn(
@@ -197,6 +249,9 @@ class GdbDebuggerTestCase(DebuggerTestCase):
                 env=env)
 
     def tearDown(self):
+        if not test_gdb():
+            return
+
         super(GdbDebuggerTestCase, self).tearDown()
         if self.p:
             self.p.stderr.close()
@@ -207,7 +262,7 @@ class GdbDebuggerTestCase(DebuggerTestCase):
 class TestAll(GdbDebuggerTestCase):
 
     def test_all(self):
-        if self.p is None:
+        if not test_gdb():
             return
 
         out, err = self.p.communicate()