63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package judge
|
|
|
|
import (
|
|
"time"
|
|
|
|
"lincodex/runner/internal/sandbox"
|
|
)
|
|
|
|
// Language describes how the sandbox checks and runs a source file.
|
|
type Language struct {
|
|
ID string
|
|
// SourceFile is the file name of the source inside sandbox.BoxPath.
|
|
SourceFile string
|
|
// Compile is the optional compile or syntax check command.
|
|
Compile []string
|
|
// CompileLimits bound the compile step; they do not depend on the run.
|
|
CompileLimits sandbox.Limits
|
|
// Run is the command of every test; the test input is its stdin.
|
|
Run []string
|
|
}
|
|
|
|
// pythonSyntaxCheck compiles the source without running it and prints only
|
|
// the exception line, so that a syntax error is a compile_error with a short
|
|
// message. compile() never executes the program.
|
|
const pythonSyntaxCheck = `import sys, traceback
|
|
path = sys.argv[1]
|
|
try:
|
|
with open(path, "rb") as stream:
|
|
compile(stream.read(), "main.py", "exec", dont_inherit=True)
|
|
except (SyntaxError, ValueError) as error:
|
|
sys.stderr.write("".join(traceback.format_exception_only(type(error), error)))
|
|
sys.exit(1)
|
|
`
|
|
|
|
// Limits of the Python syntax check. A 64 KiB source compiles far below
|
|
// them; a pathological source fails with compile_error.
|
|
const (
|
|
pythonCompileCPU = 5 * time.Second
|
|
pythonCompileWall = 10 * time.Second
|
|
pythonCompileMemory = 256 << 20
|
|
pythonCompileOutput = 64 << 10
|
|
)
|
|
|
|
// Python3 returns the Python 3 language for the interpreter at path. The
|
|
// interpreter runs in isolated mode (-I: no environment variables, no user
|
|
// site directory, no script directory on sys.path) and never writes
|
|
// bytecode (-B).
|
|
func Python3(interpreter string) Language {
|
|
source := sandbox.BoxPath + "/main.py"
|
|
return Language{
|
|
ID: "python3",
|
|
SourceFile: "main.py",
|
|
Compile: []string{interpreter, "-I", "-B", "-c", pythonSyntaxCheck, source},
|
|
CompileLimits: sandbox.Limits{
|
|
CPUTime: pythonCompileCPU,
|
|
WallTime: pythonCompileWall,
|
|
MemoryBytes: pythonCompileMemory,
|
|
OutputBytes: pythonCompileOutput,
|
|
},
|
|
Run: []string{interpreter, "-I", "-B", source},
|
|
}
|
|
}
|