/ test_cython / runtests.py
runtests.py
 1  """
 2  Run some tests for Cython integration
 3  
 4  To run the test just do
 5  
 6      python runtests.py
 7  
 8  The test suite consists of three files:
 9  
10  - runtests.py: a master file
11  - setup_cython.py: the setup file to control Cython compilation
12  - test_cython.pyx: the cython file which uses the C-API of gmpy2
13  """
14  
15  import gmpy2
16  import shutil
17  import subprocess
18  import sys
19  import tempfile
20  import os
21  from distutils.dir_util import copy_tree
22  
23  try:
24      import Cython
25  except ImportError:
26      sys.stderr.write('Cython is not installed... skipping cython tests')
27      sys.exit(0)
28  
29  print()
30  print("Unit tests for gmpy2 {0} with Cython {1}".format(gmpy2.version(), Cython.__version__))
31  print()
32  
33  
34  try:
35      old_path = os.getcwd()
36      tempdir_path = tempfile.mkdtemp()
37  
38      dirname = os.path.dirname(__file__)
39      if dirname != '':
40          os.chdir(dirname)
41      copy_tree('./', tempdir_path)
42      os.chdir(tempdir_path)
43  
44      if subprocess.call([sys.executable, 'setup_cython.py', 'build_ext', '--inplace']):
45          raise SystemExit('compilation failed')
46  
47  
48      if subprocess.call([sys.executable, '-c', 'import test_cython; test_cython.run()']):
49          raise SystemExit('cython test failed')
50  
51  finally:
52      os.chdir(old_path)
53      shutil.rmtree(tempdir_path)