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
12b68767
Commit
12b68767
authored
Feb 20, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
safe_exec seeds the random module, and now we have tests for it.
parent
ab8a3050
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
3 deletions
+46
-3
common/lib/capa/capa/safe_exec.py
+9
-3
common/lib/capa/capa/tests/test_safe_exec.py
+37
-0
No files found.
common/lib/capa/capa/safe_exec.py
View file @
12b68767
...
@@ -2,11 +2,17 @@
...
@@ -2,11 +2,17 @@
import
codejail.safe_exec
import
codejail.safe_exec
def
safe_exec
(
code
,
globals_dict
,
locals_dict
):
CODE_PROLOG
=
"""
\
import random as random_module
random = random_module.Random(
%
r)
random.Random = random_module.Random
"""
def
safe_exec
(
code
,
globals_dict
,
locals_dict
,
random_seed
=
None
):
code_prolog
=
CODE_PROLOG
%
random_seed
codejail
.
safe_exec
.
safe_exec
(
codejail
.
safe_exec
.
safe_exec
(
code
,
globals_dict
,
locals_dict
,
future_division
=
True
,
code
_prolog
+
code
,
globals_dict
,
locals_dict
,
future_division
=
True
,
assumed_imports
=
[
assumed_imports
=
[
"random"
,
"numpy"
,
"numpy"
,
"math"
,
"math"
,
"scipy"
,
"scipy"
,
...
...
common/lib/capa/capa/tests/test_safe_exec.py
0 → 100644
View file @
12b68767
"""Test safe_exec.py"""
import
random
import
unittest
from
capa.safe_exec
import
safe_exec
class
TestSafeExec
(
unittest
.
TestCase
):
def
test_set_values
(
self
):
g
,
l
=
{},
{}
safe_exec
(
"a = 17"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
17
)
def
test_division
(
self
):
g
,
l
=
{},
{}
# Future division: 1/2 is 0.5.
safe_exec
(
"a = 1/2"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
0.5
)
def
test_assumed_imports
(
self
):
g
,
l
=
{},
{}
# Math is always available.
safe_exec
(
"a = int(math.pi)"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
3
)
def
test_random_seeding
(
self
):
g
,
l
=
{},
{}
r
=
random
.
Random
(
17
)
rnums
=
[
r
.
randint
(
0
,
999
)
for
_
in
xrange
(
100
)]
# Without a seed, the results are unpredictable
safe_exec
(
"rnums = [random.randint(0, 999) for _ in xrange(100)]"
,
g
,
l
)
self
.
assertNotEqual
(
l
[
'rnums'
],
rnums
)
# With a seed, the results are predictable
safe_exec
(
"rnums = [random.randint(0, 999) for _ in xrange(100)]"
,
g
,
l
,
random_seed
=
17
)
self
.
assertEqual
(
l
[
'rnums'
],
rnums
)
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