Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import os
import io
import sys
import copy
import subprocess
import contextlib
import cppimport
import cppimport.build_module
import cppimport.templating
import cppimport.import_hook
cppimport.set_quiet(False)
@contextlib.contextmanager
def appended(filename, text):
orig = open(filename, 'r').read()
open(filename, 'a').write(text)
try:
yield
finally:
open(filename, 'w').write(orig)
def subprocess_check(test_code, returncode = 0):
p = subprocess.Popen([
'python', '-c', test_code
], cwd = os.path.dirname(__file__))
p.wait()
assert(p.returncode == returncode)
import cppimport
cppimport.force_rebuild()
cppimport.set_quiet(False)
snowdrift = cppimport.imp("snowdrift")
test_polymorphism = cppimport.imp("test_polymorphism")
import unittest
import fwdpy11.fitness as fp11w
import re
class testFitnessPolymorphism(unittest.TestCase):
@classmethod
def setUpClass(self):
self.w = [
fp11w.SlocusAdditive(),
fp11w.SlocusMult(),
snowdrift.SlocusSnowdrift(0.2, -0.2, 1, -2)]
self.v = test_polymorphism.test_callback_names(self.w)
def testCallbackNameLen(self):
self.assertEqual(len(self.v), 3)
def test_package_mymodule():
mymodule = cppimport.imp("apackage.mymodule")
module_tester(mymodule)
import cppimport
cppimport.force_rebuild()
cppimport.set_quiet(False)
snowdrift = cppimport.imp("snowdrift")
test_polymorphism = cppimport.imp("test_polymorphism")
import unittest
import fwdpy11.fitness as fp11w
import re
class testFitnessPolymorphism(unittest.TestCase):
@classmethod
def setUpClass(self):
self.w = [
fp11w.SlocusAdditive(),
fp11w.SlocusMult(),
snowdrift.SlocusSnowdrift(0.2, -0.2, 1, -2)]
self.v = test_polymorphism.test_callback_names(self.w)
def test_find_module_cpppath():
mymodule_loc = cppimport.find.find_module_cpppath("mymodule")
mymodule_dir = os.path.dirname(mymodule_loc)
assert(os.path.basename(mymodule_loc) == "mymodule.cpp")
apackage = cppimport.find.find_module_cpppath("apackage.mymodule")
apackage_correct = os.path.join(mymodule_dir, 'apackage', 'mymodule.cpp')
assert(apackage == apackage_correct)
inner = cppimport.find.find_module_cpppath("apackage.inner.mymodule")
inner_correct = os.path.join(mymodule_dir, 'apackage', 'inner', 'mymodule.cpp')
assert(inner == inner_correct)
def test_find_module_cpppath():
mymodule_loc = cppimport.find.find_module_cpppath("mymodule")
mymodule_dir = os.path.dirname(mymodule_loc)
assert(os.path.basename(mymodule_loc) == "mymodule.cpp")
apackage = cppimport.find.find_module_cpppath("apackage.mymodule")
apackage_correct = os.path.join(mymodule_dir, 'apackage', 'mymodule.cpp')
assert(apackage == apackage_correct)
inner = cppimport.find.find_module_cpppath("apackage.inner.mymodule")
inner_correct = os.path.join(mymodule_dir, 'apackage', 'inner', 'mymodule.cpp')
assert(inner == inner_correct)
def test_find_module_cpppath():
mymodule_loc = cppimport.find.find_module_cpppath("mymodule")
mymodule_dir = os.path.dirname(mymodule_loc)
assert(os.path.basename(mymodule_loc) == "mymodule.cpp")
apackage = cppimport.find.find_module_cpppath("apackage.mymodule")
apackage_correct = os.path.join(mymodule_dir, 'apackage', 'mymodule.cpp')
assert(apackage == apackage_correct)
inner = cppimport.find.find_module_cpppath("apackage.inner.mymodule")
inner_correct = os.path.join(mymodule_dir, 'apackage', 'inner', 'mymodule.cpp')
assert(inner == inner_correct)
def test_run_cpp_tests():
from cppimport import cppimport
cppimport('test_main').run_tests()
def test_import_hook():
# Force rebuild to make sure we're not just reloading the already compiled
# module from disk
cppimport.force_rebuild(True)
import hook_test
cppimport.force_rebuild(False)
def test_import_hook():
# Force rebuild to make sure we're not just reloading the already compiled
# module from disk
cppimport.force_rebuild(True)
import hook_test
cppimport.force_rebuild(False)