diff --git a/python/taichi/misc/test.py b/python/taichi/misc/test.py index 45dcbefb096f3..251b9eab6c9cb 100644 --- a/python/taichi/misc/test.py +++ b/python/taichi/misc/test.py @@ -31,3 +31,10 @@ def make_temp_file(*args, **kwargs): fd, name = mkstemp(*args, **kwargs) os.close(fd) return name + + +def run_in_sandbox(content): + filename = make_temp_file() + with open(filename, 'w') as f: + f.write(content) + return runpy.run_path(filename) diff --git a/tests/python/test_runtime.py b/tests/python/test_runtime.py index a46c5eeab14dc..c52fd39bba17a 100644 --- a/tests/python/test_runtime.py +++ b/tests/python/test_runtime.py @@ -1,13 +1,14 @@ import taichi as ti -from taichi import make_temp_file +from taichi import run_in_sandbox import sys, os +import runpy def test_without_init(): # We want to check if Taichi works well without ``ti.init()``. # But in test ``ti.init()`` will always be called in last ``@ti.all_archs``. # So we have to create a new Taichi instance, i.e. test in a sandbox. - content = ''' + run_in_sandbox(''' import taichi as ti assert ti.cfg.arch == ti.cpu @@ -16,11 +17,7 @@ def test_without_init(): x[1, 2] = 4 assert x[1, 2] == 4 -''' - filename = make_temp_file() - with open(filename, 'w') as f: - f.write(content) - assert os.system(f'{sys.executable} {filename}') == 0 +''') @ti.all_archs