Skip to content

Commit 339f02a

Browse files
committed
tests/run-perfbench.py: Don't allow imports from the cwd.
Make tests run in an isolated environment (i.e. `import io` would otherwise get the `tests/io` directory). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent 1097174 commit 339f02a

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

tests/perf_bench/core_import_mpy_multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def chdir(self, path):
4747
pass
4848

4949
def stat(self, path):
50-
if path == "__injected.mpy":
50+
if path == "/__injected.mpy":
5151
return tuple(0 for _ in range(10))
5252
else:
5353
raise OSError(-2) # ENOENT
@@ -58,7 +58,7 @@ def open(self, path, mode):
5858

5959
def mount():
6060
os.mount(FS(), "/__remote")
61-
os.chdir("/__remote")
61+
sys.path.insert(0, "/__remote")
6262

6363

6464
def test(r):

tests/perf_bench/core_import_mpy_single.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# The first import of a module will intern strings that don't already exist, and
33
# this test should be representative of what happens in a real application.
44

5-
import io, os
5+
import io, os, sys
66

77
if not (hasattr(io, "IOBase") and hasattr(os, "mount")):
88
print("SKIP")
@@ -102,7 +102,7 @@ def chdir(self, path):
102102
pass
103103

104104
def stat(self, path):
105-
if path == "__injected.mpy":
105+
if path == "/__injected.mpy":
106106
return tuple(0 for _ in range(10))
107107
else:
108108
raise OSError(-2) # ENOENT
@@ -113,7 +113,7 @@ def open(self, path, mode):
113113

114114
def mount():
115115
os.mount(FS(), "/__remote")
116-
os.chdir("/__remote")
116+
sys.path.insert(0, "/__remote")
117117

118118

119119
def test():

tests/run-perfbench.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ def run_benchmarks(args, target, param_n, param_m, n_average, test_list):
109109
continue
110110

111111
# Create test script
112+
test_script = b"import sys\nsys.path.remove('')\n\n"
112113
with open(test_file, "rb") as f:
113-
test_script = f.read()
114+
test_script += f.read()
114115
with open(BENCH_SCRIPT_DIR + "benchrun.py", "rb") as f:
115116
test_script += f.read()
116117
test_script += b"bm_run(%u, %u)\n" % (param_n, param_m)

0 commit comments

Comments
 (0)