From 49c4e03090be83bc4066329d03b3c688d297e824 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Sun, 5 Jul 2020 14:17:31 +0800 Subject: [PATCH] [test] [bug] Use runpy instead of sys.executable in test_runtime.py to fix #1406 --- python/taichi/misc/test.py | 7 +++++++ tests/python/test_runtime.py | 11 ++++------- 2 files changed, 11 insertions(+), 7 deletions(-) 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