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
baa6b4e3
Commit
baa6b4e3
authored
Apr 30, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
The cache key for safe_exec has to be hashed to keep it a reasonable size.
parent
09fbbe7b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
1 deletions
+20
-1
common/lib/capa/capa/safe_exec/safe_exec.py
+6
-1
common/lib/capa/capa/safe_exec/tests/test_safe_exec.py
+14
-0
No files found.
common/lib/capa/capa/safe_exec/safe_exec.py
View file @
baa6b4e3
...
...
@@ -4,6 +4,8 @@ from codejail.safe_exec import safe_exec as codejail_safe_exec
from
codejail.safe_exec
import
json_safe
from
.
import
lazymod
import
hashlib
# Establish the Python environment for Capa.
# Capa assumes float-friendly division always.
# The name "random" is a properly-seeded stand-in for the random module.
...
...
@@ -53,7 +55,10 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None
# Check the cache for a previous result.
if
cache
:
canonical_globals
=
sorted
(
json_safe
(
globals_dict
)
.
iteritems
())
key
=
"safe_exec
%
r
%
s
%
r"
%
(
random_seed
,
code
,
canonical_globals
)
md5er
=
hashlib
.
md5
()
md5er
.
update
(
code
)
md5er
.
update
(
repr
(
canonical_globals
))
key
=
"safe_exec
%
r
%
s"
%
(
random_seed
,
md5er
.
hexdigest
())
cached
=
cache
.
get
(
key
)
if
cached
is
not
None
:
globals_dict
.
update
(
cached
)
...
...
common/lib/capa/capa/safe_exec/tests/test_safe_exec.py
View file @
baa6b4e3
...
...
@@ -65,9 +65,13 @@ class DictCache(object):
self
.
cache
=
d
def
get
(
self
,
key
):
# Actual cache implementations have limits on key length
assert
len
(
key
)
<=
250
return
self
.
cache
.
get
(
key
)
def
set
(
self
,
key
,
value
):
# Actual cache implementations have limits on key length
assert
len
(
key
)
<=
250
self
.
cache
[
key
]
=
value
...
...
@@ -90,3 +94,13 @@ class TestSafeExecCaching(unittest.TestCase):
g
=
{}
safe_exec
(
"a = int(math.pi)"
,
g
,
cache
=
DictCache
(
cache
))
self
.
assertEqual
(
g
[
'a'
],
17
)
def
test_cache_large_code_chunk
(
self
):
# Caching used to die on memcache with more than 250 bytes of code.
# Check that it doesn't any more.
code
=
"a = 0
\n
"
+
(
"a += 1
\n
"
*
12345
)
g
=
{}
cache
=
{}
safe_exec
(
code
,
g
,
cache
=
DictCache
(
cache
))
self
.
assertEqual
(
g
[
'a'
],
12345
)
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