Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] [bug] Use runpy in test_runtime.py to fix #1406 #1410

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/taichi/misc/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def run_in_sandbox(content):
def run_in_sandbox(content):
import runpy

filename = make_temp_file()
with open(filename, 'w') as f:
f.write(content)
return runpy.run_path(filename)
11 changes: 4 additions & 7 deletions tests/python/test_runtime.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Expand All @@ -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
Expand Down