Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
4b234a63
Commit
4b234a63
authored
Feb 20, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Future division is really a capa concern, not a general-purpose codejail concern. Move it.
parent
7187b10f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
5 additions
and
21 deletions
+5
-21
common/lib/capa/capa/safe_exec.py
+3
-2
common/lib/codejail/codejail/safe_exec.py
+2
-10
common/lib/codejail/codejail/tests/test_safe_exec.py
+0
-9
No files found.
common/lib/capa/capa/safe_exec.py
View file @
4b234a63
...
...
@@ -3,8 +3,9 @@
import
codejail.safe_exec
# This will set up the name "random" as a properly-seeded stand-in for the
# random module.
# random module.
Also, capa assumes float-friendly division always.
CODE_PROLOG
=
"""
\
from __future__ import division
import random as random_module
random = random_module.Random(
%
r)
random.Random = random_module.Random
...
...
@@ -17,7 +18,7 @@ def safe_exec(code, globals_dict, locals_dict, random_seed=None, python_path=Non
"""
code_prolog
=
CODE_PROLOG
%
random_seed
codejail
.
safe_exec
.
safe_exec
(
code_prolog
+
code
,
globals_dict
,
locals_dict
,
future_division
=
True
,
code_prolog
+
code
,
globals_dict
,
locals_dict
,
python_path
=
python_path
,
assumed_imports
=
[
"numpy"
,
...
...
common/lib/codejail/codejail/safe_exec.py
View file @
4b234a63
...
...
@@ -28,15 +28,13 @@ def names_and_modules(assumed_imports):
yield
modname
,
modname
def
safe_exec
(
code
,
globals_dict
,
locals_dict
,
future_division
=
False
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
def
safe_exec
(
code
,
globals_dict
,
locals_dict
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
"""Execute code as "exec" does, but safely.
`code` is a string of Python code. `globals_dict` and `locals_dict` are
dictionaries to use as the globals and locals. Modifications the code
makes to `locals_dict` are reflected in the dictionary on return.
`future_division` determines whether Python-3-style division is used.
`assumed_imports` is a list of modules to make available as implicit
imports for the code. Entries are either a name, "mod", which makes
"import mod" part of the code, or a pair, ("f", "fooey"), which makes
...
...
@@ -48,9 +46,6 @@ def safe_exec(code, globals_dict, locals_dict, future_division=False, assumed_im
the_code
=
[]
files
=
list
(
files
or
())
if
future_division
:
the_code
.
append
(
"from __future__ import division
\n
"
)
the_code
.
append
(
textwrap
.
dedent
(
"""
\
import json
import sys
...
...
@@ -90,7 +85,7 @@ def safe_exec(code, globals_dict, locals_dict, future_division=False, assumed_im
locals_dict
.
update
(
json
.
loads
(
res
.
stdout
))
def
not_safe_exec
(
code
,
globals_dict
,
locals_dict
,
future_division
=
False
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
def
not_safe_exec
(
code
,
globals_dict
,
locals_dict
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
"""Another implementation of `safe_exec`, but not safe.
This can be swapped in for debugging problems in sandboxed Python code.
...
...
@@ -115,9 +110,6 @@ def not_safe_exec(code, globals_dict, locals_dict, future_division=False, assume
jd
[
k
]
=
v
return
json
.
loads
(
json
.
dumps
(
jd
))
if
future_division
:
code
=
"from __future__ import division
\n
"
+
code
g_dict
=
straw
(
globals_dict
)
l_dict
=
straw
(
locals_dict
)
...
...
common/lib/codejail/codejail/tests/test_safe_exec.py
View file @
4b234a63
...
...
@@ -13,15 +13,6 @@ class SafeExecTests(object):
self
.
safe_exec
(
"a = 17"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
17
)
def
test_division
(
self
):
g
,
l
=
{},
{}
# No future division: 1/2 is 0.
self
.
safe_exec
(
"a = 1/2"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
0
)
# Future division: 1/2 is 0.5.
self
.
safe_exec
(
"a = 1/2"
,
g
,
l
,
future_division
=
True
)
self
.
assertEqual
(
l
[
'a'
],
0.5
)
def
test_assumed_imports
(
self
):
g
,
l
=
{},
{}
# Using string without importing it is bad.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment