From 5177e8c656480fcf12c22cc697c629e2c5ebd3f0 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Thu, 18 Oct 2012 20:23:49 -0400 Subject: [PATCH 1/2] Catch unicode errors in contextualize_text and deal with them. --- common/lib/capa/capa/util.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index 75bd9fb5bc..68cc23655a 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -28,7 +28,17 @@ def contextualize_text(text, context): # private Does a substitution of those variables from the context ''' 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 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 are + # 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 From 1806699f26f6c1ca7965dee0a83ff897eba293f8 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Thu, 18 Oct 2012 20:25:52 -0400 Subject: [PATCH 2/2] typos --- common/lib/capa/capa/util.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index 68cc23655a..10e984611b 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -26,13 +26,14 @@ 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))): # TODO (vshnayder): This whole replacement thing is a big hack - # right now--context contains not just 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 are - # should be replaced. + # 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])