-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrunCode.py
More file actions
164 lines (151 loc) · 5.88 KB
/
runCode.py
File metadata and controls
164 lines (151 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from contextlib import contextmanager
from dataclasses import dataclass
import importlib
import os
import runpy
import sys
from typing import Optional
from .constants import *
from .exceptionHandler import handleCurrentException
from . import instrument
from .myLogging import *
from . import stacktrace
from . import utils
class Lib:
def __init__(self, mod, properlyImported):
self.properlyImported = properlyImported
if not properlyImported:
self.initModule = mod['initModule']
self.resetTestCount = mod['resetTestCount']
self.printTestResults = mod['printTestResults']
self.dict = mod
else:
self.initModule = mod.initModule
self.resetTestCount = mod.resetTestCount
self.printTestResults = mod.printTestResults
d = {}
self.dict = d
for name in dir(mod):
if name and name[0] != '_':
d[name] = getattr(mod, name)
@dataclass
class RunSetup:
def __init__(self, pathDir: str, args: Optional[list[str]] = None, installProfile: bool = True):
self.pathDir = os.path.abspath(pathDir)
self.args = args
self.sysPathInserted = False
self.oldArgs = sys.argv
self.installProfile = installProfile
def __enter__(self):
if self.pathDir not in sys.path:
sys.path.insert(0, self.pathDir)
self.sysPathInserted = True
if self.args is not None:
sys.argv = self.args
if self.installProfile:
self.originalProfile = sys.getprofile()
stacktrace.installProfileHook()
def __exit__(self, exc_type, value, traceback):
if self.installProfile:
sys.setprofile(self.originalProfile)
if self.sysPathInserted:
sys.path.remove(self.pathDir)
self.sysPathInserted = False
if self.args is not None:
sys.argv = self.oldArgs
def prepareLib(onlyCheckRunnable, enableTypeChecking):
libDefs = None
mod = 'wypp'
verbose('Attempting to import ' + mod)
wypp = importlib.import_module(mod)
libDefs = Lib(wypp, True)
verbose(f'Successfully imported module {mod} from file {wypp.__file__}')
libDefs.initModule(enableChecks=not onlyCheckRunnable,
enableTypeChecking=enableTypeChecking,
quiet=onlyCheckRunnable)
return libDefs
def debugModule(name):
if name in sys.modules:
m = sys.modules["copy"]
print(f"Module {name} already loaded from:", getattr(m, "__file__", None))
print("CWD:", os.getcwd())
print("sys.path[0]:", sys.path[0])
print("First few sys.path entries:")
for p in sys.path[:5]:
print(" ", p)
spec = importlib.util.find_spec(name)
print("Resolved spec:", spec)
if spec:
print("Origin:", spec.origin)
print("Loader:", type(spec.loader).__name__)
@contextmanager
def freshModules():
original_modules = sys.modules.copy()
stdlib_path = os.path.dirname(os.__file__)
whitelist = ('sys', 'os', 'contextlib', 'runpy', 'wypp', '__main__', '__wypp__')
def is_core(name, module):
if name in sys.stdlib_module_names or name in sys.builtin_module_names:
return True
module_file = getattr(module, '__file__', None)
if module_file and module_file.startswith(stdlib_path):
return True
if name in whitelist:
return True
return False
try:
for mod_name in list(sys.modules.keys()):
if not is_core(mod_name, sys.modules[mod_name]):
del sys.modules[mod_name]
yield
finally:
# 4. Restore everything
sys.modules.clear()
sys.modules.update(original_modules)
def runCode(fileToRun, globals, doTypecheck=True, extraDirs=None) -> dict:
if not extraDirs:
extraDirs = []
modName = os.path.basename(os.path.splitext(fileToRun)[0])
with instrument.setupFinder(os.path.dirname(fileToRun), modName, extraDirs, doTypecheck):
sys.dont_write_bytecode = True
if DEBUG:
debugModule(modName)
with freshModules():
res = runpy.run_module(modName, init_globals=globals, run_name='__wypp__', alter_sys=False)
return res
def runStudentCode(fileToRun, globals, onlyCheckRunnable, doTypecheck=True, extraDirs=None) -> dict:
doRun = lambda: runCode(fileToRun, globals, doTypecheck=doTypecheck, extraDirs=extraDirs)
if onlyCheckRunnable:
try:
doRun()
except:
printStderr('Loading file %s crashed' % fileToRun)
handleCurrentException()
else:
utils.die(0)
return doRun()
# globals already contain libDefs
def runTestsInFile(testFile, globals, libDefs, doTypecheck=True, extraDirs=[]):
printStderr()
printStderr(f"Running tutor's tests in {testFile}")
libDefs.resetTestCount()
runCode(testFile, globals, doTypecheck=doTypecheck, extraDirs=extraDirs)
try:
runCode(testFile, globals, doTypecheck=doTypecheck, extraDirs=extraDirs)
except:
handleCurrentException()
return libDefs.dict['printTestResults']('Tutor: ')
# globals already contain libDefs
def performChecks(check, testFile, globals, libDefs, doTypecheck=True, extraDirs=None, loadingFailed=False):
prefix = ''
if check and testFile:
prefix = 'Student: '
testResultsStudent = libDefs.printTestResults(prefix, loadingFailed)
if check:
testResultsInstr = {'total': 0, 'failing': 0}
if testFile:
testDir = os.path.dirname(testFile)
with RunSetup(testDir):
testResultsInstr = runTestsInFile(testFile, globals, libDefs, doTypecheck=doTypecheck,
extraDirs=extraDirs)
failingSum = testResultsStudent['failing'] + testResultsInstr['failing']
utils.die(0 if failingSum < 1 else 1)