Fix leak in try-break.
[cython.git] / runtests.py
index 6bbce9926592e4cf1b5744256c064bf9e30998dc..8e8925e1deefda6157df2d9afd19727ee401eda7 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import os, sys, re, shutil, unittest, doctest, ctypes
+import os, sys, re, shutil, unittest, doctest
 
 WITH_CYTHON = True
 
@@ -10,7 +10,7 @@ from distutils.command.build_ext import build_ext as _build_ext
 distutils_distro = Distribution()
 
 TEST_DIRS = ['compile', 'errors', 'run', 'pyregr']
-TEST_RUN_DIRS = ['run', 'pyregr']
+TEST_RUN_DIRS = ['run', 'pyregr', 'bugs']
 
 # Lists external modules, and a matcher matching tests
 # which should be excluded if the module is not present.
@@ -327,7 +327,6 @@ class CythonRunTestCase(CythonCompileTestCase):
             self.setUp()
             self.runCompileTest()
             if not self.cython_only:
-                sys.stderr.write('running doctests in %s ...\n' % self.module)
                 doctest.DocTestSuite(self.module).run(result)
         except Exception:
             result.addError(self, sys.exc_info())
@@ -348,7 +347,6 @@ class CythonUnitTestCase(CythonCompileTestCase):
         try:
             self.setUp()
             self.runCompileTest()
-            sys.stderr.write('running tests in %s ...\n' % self.module)
             unittest.defaultTestLoader.loadTestsFromName(self.module).run(result)
         except Exception:
             result.addError(self, sys.exc_info())
@@ -484,9 +482,9 @@ if __name__ == '__main__':
     parser.add_option("--cython-only", dest="cython_only",
                       action="store_true", default=False,
                       help="only compile pyx to c, do not run C compiler or run the tests")
-    parser.add_option("--refnanny", dest="with_refnanny",
-                      action="store_true", default=False,
-                      help="also test that Cython-generated code does correct reference counting")
+    parser.add_option("--no-refnanny", dest="with_refnanny",
+                      action="store_false", default=True,
+                      help="do not regression test reference counting")
     parser.add_option("--sys-pyregr", dest="system_pyregr",
                       action="store_true", default=False,
                       help="run the regression tests of the CPython installation")
@@ -533,13 +531,6 @@ if __name__ == '__main__':
         from Cython.Compiler import Errors
         Errors.LEVEL = 0 # show all warnings
 
-    if options.with_refnanny:
-        ctypes.PyDLL("Cython/Runtime/refnanny.so", mode=ctypes.RTLD_GLOBAL)
-        sys.path.append("Cython/Runtime")
-        import refnanny
-        del sys.path[-1]
-        CFLAGS.append("-DCYTHON_REFNANNY")
-
     # RUN ALL TESTS!
     ROOTDIR = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), 'tests')
     WORKDIR = os.path.join(os.getcwd(), 'BUILD')
@@ -547,7 +538,9 @@ if __name__ == '__main__':
     UNITTEST_ROOT = os.path.join(os.getcwd(), UNITTEST_MODULE)
     if WITH_CYTHON:
         if os.path.exists(WORKDIR):
-            shutil.rmtree(WORKDIR, ignore_errors=True)
+            for path in os.listdir(WORKDIR):
+                if path in ("support",): continue
+                shutil.rmtree(os.path.join(WORKDIR, path), ignore_errors=True)
     if not os.path.exists(WORKDIR):
         os.makedirs(WORKDIR)
 
@@ -559,10 +552,19 @@ if __name__ == '__main__':
     sys.stderr.write("Python %s\n" % sys.version)
     sys.stderr.write("\n")
 
+    if options.with_refnanny:
+        from pyximport.pyxbuild import pyx_to_dll
+        libpath = pyx_to_dll(os.path.join("Cython", "Runtime", "refnanny.pyx"),
+                             build_in_temp=True,
+                             pyxbuild_dir=os.path.join(WORKDIR, "support"))
+        sys.path.insert(0, os.path.split(libpath)[0])
+        CFLAGS.append("-DCYTHON_REFNANNY")
+
     test_bugs = False
-    for ticket_number in options.tickets:
-        test_bugs = True
-        cmd_args.append('bugs.*T%s$' % ticket_number)
+    if options.tickets:
+        for ticket_number in options.tickets:
+            test_bugs = True
+            cmd_args.append('bugs.*T%s$' % ticket_number)
     if not test_bugs:
         for selector in cmd_args:
             if selector.startswith('bugs'):
@@ -631,4 +633,5 @@ if __name__ == '__main__':
             sys.stderr.write("   %s\n" % test)
 
     if options.with_refnanny:
+        import refnanny
         sys.stderr.write("\n".join([repr(x) for x in refnanny.reflog]))