diff options
author | 2021-05-18 16:35:40 -0400 | |
---|---|---|
committer | 2021-05-18 16:35:40 -0400 | |
commit | 6e98935b08cef12589b033b15c5146f8e42e9ad5 (patch) | |
tree | ec2103164e8ee28978f4a3bafe736aed2c6f3780 | |
parent | feat: Allow custom arguments to be passed to eval (diff) |
chore: Improve documentation for arguments in /eval
-rw-r--r-- | snekbox/api/resources/eval.py | 10 | ||||
-rw-r--r-- | snekbox/nsjail.py | 14 |
2 files changed, 16 insertions, 8 deletions
diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py index 5b4d5a2..722a918 100644 --- a/snekbox/api/resources/eval.py +++ b/snekbox/api/resources/eval.py @@ -26,7 +26,7 @@ class EvalResource: }, "args": { "type": "array", - "contains": { + "items": { "type": "string" } } @@ -44,6 +44,8 @@ class EvalResource: """ Evaluate Python code and return stdout, stderr, and the return code. + The optional `args` parameter can be passed, and it woul replace the "-c" option. + The return codes mostly resemble those of a Unix shell. Some noteworthy cases: - None @@ -56,14 +58,14 @@ class EvalResource: Request body: >>> { - ... "input": "print(1 + 1)" + ... "input": "[i for i in range(1000)]", ... "args": ["-m", "timeit"] # This is optional ... } Response format: >>> { - ... "stdout": "2\\n", + ... "10000 loops, best of 5: 23.8 usec per loop\n", ... "returncode": 0 ... } @@ -80,7 +82,7 @@ class EvalResource: args = req.media.get("args", ("-c",)) try: - result = self.nsjail.python3(code, extra_args=args) + result = self.nsjail.python3(code, py_args=args) except Exception: log.exception("An exception occurred while trying to process the request") raise falcon.HTTPInternalServerError diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 835f384..e25f800 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -171,14 +171,20 @@ class NsJail: return "".join(output) - def python3(self, code: str, *args, extra_args: Iterable[str] = ("-c",)) -> CompletedProcess: + def python3( + self, + code: str, + *, + nsjail_args: Iterable[str] = (), + py_args: Iterable[str] = ("-c",) + ) -> CompletedProcess: """ Execute Python 3 code in an isolated environment and return the completed process. Additional arguments passed will be used to override the values in the NsJail config. These arguments are only options for NsJail; they do not affect Python's arguments. - The `extra_args` keyword argument can be given, and this would replace the "-c" argument + The `py_args` keyword argument can be given, and this would replace the "-c" argument given by default. """ cgroup = self._create_dynamic_cgroups() @@ -191,9 +197,9 @@ class NsJail: # Set our dynamically created parent cgroups "--cgroup_mem_parent", cgroup, "--cgroup_pids_parent", cgroup, - *args, + *nsjail_args, "--", - self.config.exec_bin.path, *self.config.exec_bin.arg, *extra_args, code + self.config.exec_bin.path, *self.config.exec_bin.arg, *py_args, code ) msg = "Executing code..." |