def test_rebuild_when_dependencies_change(self): # Given. data = self.data depends = ["test_rebuild"] s = ExtModule(data, root=self.root, depends=depends) fname = self._create_dummy_module() f_stat = os.stat(fname) with self._add_root_to_sys_path(): # When self.assertTrue(s.should_recompile()) s.build() # Then. self.assertFalse(s.should_recompile()) # Now lets re-create the module and try again. # When. fname = self._create_dummy_module() # Update the timestamp to make it newer, otherwise we need to # sleep. os.utime(fname, (f_stat.st_atime, f_stat.st_mtime + 10)) # Then. self.assertTrue(s.should_recompile())
def compile(self, code): # Note, we do not add carray or particle_array as nnps_base would # have been rebuilt anyway if they changed. depends = ["pysph.base.nnps_base"] self._ext_mod = ExtModule(code, verbose=True, depends=depends) self._module = self._ext_mod.load() return self._module
def _check_compile(root): with mock.patch('shutil.copy') as m: s = ExtModule("print('hello')", root=root) s.build() if m.called: # If it was called, do the copy to mimic the action. shutil.copy(*m.call_args[0]) return m.call_count
def test_default_root(self): try: data = self.data s = ExtModule(data) self.assertTrue(exists(join(s.root, 'build'))) self.assertEqual(s.hash, get_md5(data)) self.assertEqual(s.code, data) self.assertTrue(exists(s.src_path)) self.assertEqual(data, open(s.src_path).read()) finally: os.unlink(s.src_path)
def _check_write_source(root): """Used to create an ExtModule and test if a file was opened. It returns the number of times "open" was called. """ m = mock.mock_open() with mock.patch('pysph.base.ext_module.open', m, create=True): s = ExtModule("print('hello')", root=root) if m.called: with open(*m.call_args[0]) as fp: fp.write("junk") return m.call_count
def test_constructor(self): data = self.data s = ExtModule(data, root=self.root) self.assertTrue(exists(join(self.root, 'build'))) self.assertEqual(s.hash, get_md5(data)) self.assertEqual(s.code, data) expect_name = 'm_%s' % (s.hash) self.assertEqual(s.name, expect_name) self.assertEqual(s.src_path, join(self.root, expect_name + '.pyx')) self.assertEqual(s.ext_path, join(self.root, expect_name + get_config_var('SO'))) self.assertTrue(exists(s.src_path)) self.assertEqual(data, open(s.src_path).read())
def compile(self): """Compile the generated code to an extension module and setup the objects that need this by calling their setup_compiled_module. """ if self.ext_mod is not None: return code = self._get_code() # Note, we do not add carray or particle_array as nnps_base would have # been rebuilt anyway if they changed. depends = ["pysph.base.nnps_base"] self.ext_mod = ExtModule(code, verbose=True, depends=depends) mod = self.ext_mod.load() self.module = mod self.acceleration_eval_helper.setup_compiled_module(mod) cython_a_eval = self.acceleration_eval.c_acceleration_eval if self.integrator is not None: self.integrator_helper.setup_compiled_module(mod, cython_a_eval)
def _check_write_source(root): """Used to create an ExtModule and test if a file was opened. It returns the number of times "open" was called. """ m = mock.mock_open() orig_side_effect = m.side_effect def _side_effect(*args, **kw): with open(*args, **kw) as fp: fp.write("junk") return orig_side_effect(*args, **kw) m.side_effect = _side_effect with mock.patch('pysph.base.ext_module.open', m, create=True): ExtModule("print('hello')", root=root) return m.call_count
def test_load_module(self): data = self.data s = ExtModule(data, root=self.root) mod = s.load() self.assertEqual(mod.f(), "hello world") self.assertTrue(exists(s.ext_path))