Merge pull request #1954 from edx/fix/will/md5-unicode-error

Fix UnicodeEncodeError when generating cache keys
This commit is contained in:
Ned Batchelder
2013-05-06 12:45:47 -07:00
2 changed files with 14 additions and 1 deletions

View File

@@ -58,7 +58,7 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None
if cache:
canonical_globals = sorted(json_safe(globals_dict).iteritems())
md5er = hashlib.md5()
md5er.update(code)
md5er.update(repr(code))
md5er.update(repr(canonical_globals))
key = "safe_exec.%r.%s" % (random_seed, md5er.hexdigest())
cached = cache.get(key)

View File

@@ -143,6 +143,19 @@ class TestSafeExecCaching(unittest.TestCase):
safe_exec(code, g, cache=DictCache(cache))
self.assertEqual(g['a'], 17)
def test_unicode_submission(self):
# Check that using non-ASCII unicode does not raise an encoding error.
# Try several non-ASCII unicode characters
for code in [129, 500, 2**8 - 1, 2**16 - 1]:
code_with_unichr = unicode("# ") + unichr(code)
try:
safe_exec(code_with_unichr, {}, cache=DictCache({}))
except UnicodeEncodeError:
self.fail("Tried executing code with non-ASCII unicode: {0}".format(code))
class TestRealProblems(unittest.TestCase):
def test_802x(self):