Commit 4f33b8e0 by Will Daly

Fixed a UnicodeEncodeError that occurred when generating cache keys

from non-ASCII unicode code submissions.
parent e3de0dc8
...@@ -58,7 +58,7 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None ...@@ -58,7 +58,7 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None
if cache: if cache:
canonical_globals = sorted(json_safe(globals_dict).iteritems()) canonical_globals = sorted(json_safe(globals_dict).iteritems())
md5er = hashlib.md5() md5er = hashlib.md5()
md5er.update(code) md5er.update(repr(code))
md5er.update(repr(canonical_globals)) md5er.update(repr(canonical_globals))
key = "safe_exec.%r.%s" % (random_seed, md5er.hexdigest()) key = "safe_exec.%r.%s" % (random_seed, md5er.hexdigest())
cached = cache.get(key) cached = cache.get(key)
......
...@@ -143,6 +143,19 @@ class TestSafeExecCaching(unittest.TestCase): ...@@ -143,6 +143,19 @@ class TestSafeExecCaching(unittest.TestCase):
safe_exec(code, g, cache=DictCache(cache)) safe_exec(code, g, cache=DictCache(cache))
self.assertEqual(g['a'], 17) 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): class TestRealProblems(unittest.TestCase):
def test_802x(self): def test_802x(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment