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
283fc47a
Commit
283fc47a
authored
Feb 27, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Jailed code importing random explicitly would get the wrong seed.
parent
f3e8d5bb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
0 deletions
+14
-0
common/lib/capa/capa/safe_exec.py
+2
-0
common/lib/capa/capa/tests/test_safe_exec.py
+12
-0
No files found.
common/lib/capa/capa/safe_exec.py
View file @
283fc47a
...
...
@@ -9,9 +9,11 @@ CODE_PROLOG = """\
from __future__ import division
import random as random_module
import sys
random = random_module.Random(
%
r)
random.Random = random_module.Random
del random_module
sys.modules['random'] = random
"""
def
safe_exec
(
code
,
globals_dict
,
random_seed
=
None
,
python_path
=
None
):
...
...
common/lib/capa/capa/tests/test_safe_exec.py
View file @
283fc47a
...
...
@@ -37,6 +37,18 @@ class TestSafeExec(unittest.TestCase):
safe_exec
(
"rnums = [random.randint(0, 999) for _ in xrange(100)]"
,
g
,
random_seed
=
17
)
self
.
assertEqual
(
g
[
'rnums'
],
rnums
)
def
test_random_is_still_importable
(
self
):
g
=
{}
r
=
random
.
Random
(
17
)
rnums
=
[
r
.
randint
(
0
,
999
)
for
_
in
xrange
(
100
)]
# With a seed, the results are predictable even from the random module
safe_exec
(
"import random
\n
"
"rnums = [random.randint(0, 999) for _ in xrange(100)]
\n
"
,
g
,
random_seed
=
17
)
self
.
assertEqual
(
g
[
'rnums'
],
rnums
)
def
test_python_lib
(
self
):
pylib
=
os
.
path
.
dirname
(
__file__
)
+
"/test_files/pylib"
g
=
{}
...
...
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