fix #480: float() as a type cast for function return values
[cython.git] / runtests.py
index a57a778a2c36fff3abd42db8cbae5b023d10d0ac..9ac487f7b5e77d455711687efa048850e9894488 100644 (file)
@@ -9,6 +9,7 @@ import shutil
 import unittest
 import doctest
 import operator
+import tempfile
 try:
     from StringIO import StringIO
 except ImportError:
@@ -33,7 +34,8 @@ TEST_RUN_DIRS = ['run', 'pyregr']
 # Lists external modules, and a matcher matching tests
 # which should be excluded if the module is not present.
 EXT_DEP_MODULES = {
-    'numpy' : re.compile('.*\.numpy_.*').match
+    'numpy' : re.compile('.*\.numpy_.*').match,
+    'pstats' : re.compile('.*\.pstats_.*').match
 }
 
 def get_numpy_include_dirs():
@@ -401,51 +403,62 @@ class CythonRunTestCase(CythonCompileTestCase):
             return
 
         # fork to make sure we do not keep the tested module loaded
-        input, output = os.pipe()
+        result_handle, result_file = tempfile.mkstemp()
+        os.close(result_handle)
         child_id = os.fork()
         if not child_id:
             result_code = 0
             try:
-                output = os.fdopen(output, 'wb')
-                tests = None
                 try:
-                    partial_result = PartialTestResult(result)
-                    tests = doctest.DocTestSuite(module_name)
-                    tests.run(partial_result)
-                    gc.collect()
-                except Exception:
-                    if tests is None:
-                        # importing failed, try to fake a test class
-                        tests = _FakeClass(
-                            failureException=None,
-                            shortDescription = self.shortDescription,
-                            **{module_name: None})
-                    partial_result.addError(tests, sys.exc_info())
-                    result_code = 1
-                pickle.dump(partial_result.data(), output)
+                    tests = None
+                    try:
+                        partial_result = PartialTestResult(result)
+                        tests = doctest.DocTestSuite(module_name)
+                        tests.run(partial_result)
+                        gc.collect()
+                    except Exception:
+                        if tests is None:
+                            # importing failed, try to fake a test class
+                            tests = _FakeClass(
+                                failureException=None,
+                                shortDescription = self.shortDescription,
+                                **{module_name: None})
+                        partial_result.addError(tests, sys.exc_info())
+                        result_code = 1
+                    output = open(result_file, 'wb')
+                    pickle.dump(partial_result.data(), output)
+                except:
+                    import traceback
+                    traceback.print_exc()
             finally:
                 try: output.close()
                 except: pass
                 os._exit(result_code)
 
-        cid, result_code = os.waitpid(child_id, 0)
-        if result_code in (0,1):
-            input = os.fdopen(input, 'rb')
-            try:
-                PartialTestResult.join_results(result, pickle.load(input))
-            finally:
-                input.close()
-        if result_code:
-            raise Exception("Tests in module '%s' exited with status %d" %
-                            (module_name, result_code >> 8))
+        try:
+            cid, result_code = os.waitpid(child_id, 0)
+            if result_code in (0,1):
+                input = open(result_file, 'rb')
+                try:
+                    PartialTestResult.join_results(result, pickle.load(input))
+                finally:
+                    input.close()
+            if result_code:
+                raise Exception("Tests in module '%s' exited with status %d" %
+                                (module_name, result_code >> 8))
+        finally:
+            try: os.unlink(result_file)
+            except: pass
 
 
 is_private_field = re.compile('^_[^_]').match
 
 class _FakeClass(object):
     def __init__(self, **kwargs):
-        self.shortDescription = lambda x: kwargs.get('module_name')
+        self._shortDescription = kwargs.get('module_name')
         self.__dict__.update(kwargs)
+    def shortDescription(self):
+        return self._shortDescription
 
 try: # Py2.7+ and Py3.2+
     from unittest.runner import _TextTestResult
@@ -730,8 +743,13 @@ if __name__ == '__main__':
                              ''')
             sys.path.insert(0, cy3_dir)
     elif sys.version_info[0] >= 3:
-        # make sure we do not import (or run) Cython itself
-        options.with_cython = False
+        # make sure we do not import (or run) Cython itself (unless
+        # 2to3 was already run)
+        cy3_dir = os.path.join(WORKDIR, 'Cy3')
+        if os.path.isdir(cy3_dir):
+            sys.path.insert(0, cy3_dir)
+        else:
+            options.with_cython = False
         options.doctests    = False
         options.unittests   = False
         options.pyregr      = False
@@ -846,7 +864,7 @@ if __name__ == '__main__':
                 os.path.join(sys.prefix, 'lib', 'python'+sys.version[:3], 'test'),
                 'pyregr'))
 
-    unittest.TextTestRunner(verbosity=options.verbosity).run(test_suite)
+    result = unittest.TextTestRunner(verbosity=options.verbosity).run(test_suite)
 
     if options.coverage:
         coverage.stop()
@@ -865,3 +883,5 @@ if __name__ == '__main__':
     if options.with_refnanny:
         import refnanny
         sys.stderr.write("\n".join([repr(x) for x in refnanny.reflog]))
+
+    sys.exit(not result.wasSuccessful())