Skip to content
Snippets Groups Projects
Commit cda5e1a4 authored by Victor Shnayder's avatar Victor Shnayder
Browse files

Merge pull request #937 from MITx/victor/hotfix/unicode-in-code

Catch unicode errors in contextualize_text and deal with them.
parents 2e03a484 1806699f
No related branches found
No related tags found
No related merge requests found
......@@ -26,9 +26,20 @@ def compare_with_tolerance(v1, v2, tol):
def contextualize_text(text, context): # private
''' Takes a string with variables. E.g. $a+$b.
Does a substitution of those variables from the context '''
if not text: return text
if not text:
return text
for key in sorted(context, lambda x, y: cmp(len(y), len(x))):
text = text.replace('$' + key, str(context[key]))
# TODO (vshnayder): This whole replacement thing is a big hack
# right now--context contains not just the vars defined in the
# program, but also e.g. a reference to the numpy module.
# Should be a separate dict of variables that should be
# replaced.
if '$' + key in text:
try:
s = str(context[key])
except UnicodeEncodeError:
s = context[key].encode('utf8', errors='ignore')
text = text.replace('$' + key, s)
return text
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment