From 9be4ce1441ea347bdea8dc4dd76cca49d69086e2 Mon Sep 17 00:00:00 2001 From: Michelle Baert Date: Fri, 15 Jan 2016 12:51:38 +0100 Subject: [PATCH 1/3] Fixing issue #24 -NameError: name 'unicode' is not defined The Python3 way, using unicode strings instead of bytes. --- py-src/ltmain.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/py-src/ltmain.py b/py-src/ltmain.py index e07f173..9f9b0f4 100644 --- a/py-src/ltmain.py +++ b/py-src/ltmain.py @@ -46,11 +46,21 @@ def asUnicode(s): except: return str(s) -def ensureUtf(s): - if type(s) == unicode: - return s.encode('utf8', 'ignore') +def ensureUtf(s, encoding='utf8'): + """Converts input to unicode if necessary. + + If `s` is bytes, it will be decoded using the `encoding` parameters. + + This function is used for preprocessing /source/ and /filename/ arguments + to the builtin function `compile`. + """ + # In Python2, str == bytes. + # In Python3, bytes remains unchanged, but str means unicode + # while unicode is not defined anymore + if type(s) == bytes: + return s.decode(encoding, 'ignore') else: - return str(s) + return s def findLoc(body, line, total): for i in range(len(body)): From 54924c5ccd1835bf56ee54f96d7dda1890c31ee3 Mon Sep 17 00:00:00 2001 From: Michelle Baert Date: Fri, 15 Jan 2016 12:55:06 +0100 Subject: [PATCH 2/3] Renaming ensuringUtf() to toUnicode() --- py-src/ltmain.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/py-src/ltmain.py b/py-src/ltmain.py index 9f9b0f4..a23901c 100644 --- a/py-src/ltmain.py +++ b/py-src/ltmain.py @@ -46,7 +46,7 @@ def asUnicode(s): except: return str(s) -def ensureUtf(s, encoding='utf8'): +def toUnicode(s, encoding='utf8'): """Converts input to unicode if necessary. If `s` is bytes, it will be decoded using the `encoding` parameters. @@ -200,11 +200,11 @@ def handleEval(data): loc = form[0] isEval = False try: - code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval') + code= compile(toUnicode(code), ensureUtf(data[2]["name"]), 'eval') isEval = True except: try: - code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec') + code= compile(toUnicode(code), ensureUtf(data[2]["name"]), 'exec') except: e = traceback.format_exc() send(data[0], "editor.eval.python.exception", {"ex": cleanTrace(e), "meta": loc}) @@ -270,11 +270,11 @@ def ipyEval(data): loc = form[0] isEval = False try: - compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval') + compile(toUnicode(code), ensureUtf(data[2]["name"]), 'eval') isEval = True except: try: - compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec') + compile(toUnicode(code), ensureUtf(data[2]["name"]), 'exec') except: e = traceback.format_exc() send(data[0], "editor.eval.python.exception", {"ex": cleanTrace(e), "meta": loc}) From 52ab4c4437bb5c976c4372885b05727f626fa624 Mon Sep 17 00:00:00 2001 From: Michelle Baert Date: Fri, 15 Jan 2016 17:00:54 +0100 Subject: [PATCH 3/3] completed previous rename :/ --- py-src/ltmain.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py-src/ltmain.py b/py-src/ltmain.py index a23901c..44a9420 100644 --- a/py-src/ltmain.py +++ b/py-src/ltmain.py @@ -200,11 +200,11 @@ def handleEval(data): loc = form[0] isEval = False try: - code= compile(toUnicode(code), ensureUtf(data[2]["name"]), 'eval') + code= compile(toUnicode(code), toUnicode(data[2]["name"]), 'eval') isEval = True except: try: - code= compile(toUnicode(code), ensureUtf(data[2]["name"]), 'exec') + code= compile(toUnicode(code), toUnicode(data[2]["name"]), 'exec') except: e = traceback.format_exc() send(data[0], "editor.eval.python.exception", {"ex": cleanTrace(e), "meta": loc}) @@ -270,11 +270,11 @@ def ipyEval(data): loc = form[0] isEval = False try: - compile(toUnicode(code), ensureUtf(data[2]["name"]), 'eval') + compile(toUnicode(code), toUnicode(data[2]["name"]), 'eval') isEval = True except: try: - compile(toUnicode(code), ensureUtf(data[2]["name"]), 'exec') + compile(toUnicode(code), toUnicode(data[2]["name"]), 'exec') except: e = traceback.format_exc() send(data[0], "editor.eval.python.exception", {"ex": cleanTrace(e), "meta": loc})