TestCyCache.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import difflib
  2. import glob
  3. import gzip
  4. import os
  5. import tempfile
  6. import Cython.Build.Dependencies
  7. import Cython.Utils
  8. from Cython.TestUtils import CythonTest
  9. class TestCyCache(CythonTest):
  10. def setUp(self):
  11. CythonTest.setUp(self)
  12. self.temp_dir = tempfile.mkdtemp(
  13. prefix='cycache-test',
  14. dir='TEST_TMP' if os.path.isdir('TEST_TMP') else None)
  15. self.src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
  16. self.cache_dir = tempfile.mkdtemp(prefix='cache', dir=self.temp_dir)
  17. def cache_files(self, file_glob):
  18. return glob.glob(os.path.join(self.cache_dir, file_glob))
  19. def fresh_cythonize(self, *args, **kwargs):
  20. Cython.Utils.clear_function_caches()
  21. Cython.Build.Dependencies._dep_tree = None # discard method caches
  22. Cython.Build.Dependencies.cythonize(*args, **kwargs)
  23. def test_cycache_switch(self):
  24. content1 = 'value = 1\n'
  25. content2 = 'value = 2\n'
  26. a_pyx = os.path.join(self.src_dir, 'a.pyx')
  27. a_c = a_pyx[:-4] + '.c'
  28. open(a_pyx, 'w').write(content1)
  29. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  30. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  31. self.assertEqual(1, len(self.cache_files('a.c*')))
  32. a_contents1 = open(a_c).read()
  33. os.unlink(a_c)
  34. open(a_pyx, 'w').write(content2)
  35. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  36. a_contents2 = open(a_c).read()
  37. os.unlink(a_c)
  38. self.assertNotEqual(a_contents1, a_contents2, 'C file not changed!')
  39. self.assertEqual(2, len(self.cache_files('a.c*')))
  40. open(a_pyx, 'w').write(content1)
  41. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  42. self.assertEqual(2, len(self.cache_files('a.c*')))
  43. a_contents = open(a_c).read()
  44. self.assertEqual(
  45. a_contents, a_contents1,
  46. msg='\n'.join(list(difflib.unified_diff(
  47. a_contents.split('\n'), a_contents1.split('\n')))[:10]))
  48. def test_cycache_uses_cache(self):
  49. a_pyx = os.path.join(self.src_dir, 'a.pyx')
  50. a_c = a_pyx[:-4] + '.c'
  51. open(a_pyx, 'w').write('pass')
  52. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  53. a_cache = os.path.join(self.cache_dir, os.listdir(self.cache_dir)[0])
  54. gzip.GzipFile(a_cache, 'wb').write('fake stuff'.encode('ascii'))
  55. os.unlink(a_c)
  56. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  57. a_contents = open(a_c).read()
  58. self.assertEqual(a_contents, 'fake stuff',
  59. 'Unexpected contents: %s...' % a_contents[:100])
  60. def test_multi_file_output(self):
  61. a_pyx = os.path.join(self.src_dir, 'a.pyx')
  62. a_c = a_pyx[:-4] + '.c'
  63. a_h = a_pyx[:-4] + '.h'
  64. a_api_h = a_pyx[:-4] + '_api.h'
  65. open(a_pyx, 'w').write('cdef public api int foo(int x): return x\n')
  66. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  67. expected = [a_c, a_h, a_api_h]
  68. for output in expected:
  69. self.assertTrue(os.path.exists(output), output)
  70. os.unlink(output)
  71. self.fresh_cythonize(a_pyx, cache=self.cache_dir)
  72. for output in expected:
  73. self.assertTrue(os.path.exists(output), output)
  74. def test_options_invalidation(self):
  75. hash_pyx = os.path.join(self.src_dir, 'options.pyx')
  76. hash_c = hash_pyx[:-len('.pyx')] + '.c'
  77. open(hash_pyx, 'w').write('pass')
  78. self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False)
  79. self.assertEqual(1, len(self.cache_files('options.c*')))
  80. os.unlink(hash_c)
  81. self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=True)
  82. self.assertEqual(2, len(self.cache_files('options.c*')))
  83. os.unlink(hash_c)
  84. self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False, show_version=False)
  85. self.assertEqual(2, len(self.cache_files('options.c*')))
  86. os.unlink(hash_c)
  87. self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False, show_version=True)
  88. self.assertEqual(2, len(self.cache_files('options.c*')))