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

Commit

Permalink
Merge pull request #47 from RockyRoad29/Python3Unicode
Browse files Browse the repository at this point in the history
Python3 unicode -  Fixing issue #24 -NameError: name 'unicode' is not defined
  • Loading branch information
sbauer322 authored Apr 11, 2017
2 parents 972ceb0 + 52ab4c4 commit 8ac9fa4
Showing 1 changed file with 18 additions and 8 deletions.
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

0 comments on commit 8ac9fa4

Please sign in to comment.