Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Python3 unicode - Fixing issue #24 -NameError: name 'unicode' is not defined #47

Merged
merged 3 commits into from
Apr 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions py-src/ltmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ def asUnicode(s):
except:
return str(s)

def ensureUtf(s):
if type(s) == unicode:
return s.encode('utf8', 'ignore')
def toUnicode(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)):
Expand Down Expand Up @@ -190,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), toUnicode(data[2]["name"]), 'eval')
isEval = True
except:
try:
code= compile(ensureUtf(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})
Expand Down Expand Up @@ -260,11 +270,11 @@ def ipyEval(data):
loc = form[0]
isEval = False
try:
compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval')
compile(toUnicode(code), toUnicode(data[2]["name"]), 'eval')
isEval = True
except:
try:
compile(ensureUtf(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})
Expand Down