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

future imports fix #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions py-src/ltmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
import asyncore
import signal
import lttools
import __future__
import operator
import functools

oldout = sys.stdout
olderr = sys.stderr
stop = False
local = True
threads = []
currentClient = 0
compilerFlags = 0

class Printer():
cur = ""
Expand Down Expand Up @@ -147,6 +151,18 @@ def stopped():
global stop
return stop

def featureFlags(code):
futureItems = __future__.__dict__.iteritems()
#ensure we only try to get flags for features
featureNames = [k for k,v in futureItems if hasattr(v, 'compiler_flag')]
names = [n for n in code.co_names[1:] if n in featureNames]

return [getattr(__future__, name).compiler_flag for name in names]

def addFlags(current, new):
new.append(current)
return functools.reduce(operator.or_, new)

def handleEval(data):
result = None
code = cleanCode(data[2]["code"])
Expand Down Expand Up @@ -190,11 +206,14 @@ def handleEval(data):
loc = form[0]
isEval = False
try:
code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval')
code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval', compilerFlags)
isEval = True
except:
try:
code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec')
global compilerFlags
code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec', compilerFlags)
if code.co_names[0] == '__future__':
compilerFlags = addFlags(compilerFlags, featureFlags(code))
except:
e = traceback.format_exc()
send(data[0], "editor.eval.python.exception", {"ex": cleanTrace(e), "meta": loc})
Expand Down