From 79009e113eb1c064d54b4d5c6c90d9004816c76e Mon Sep 17 00:00:00 2001 From: Nate Lust Date: Sun, 12 Jan 2014 23:17:43 -0500 Subject: [PATCH] Fix import issue of Queue, on python 3 its simply queue. Also addressed a namespace issue being created and importing in lttools in the toModule function creating a blank namespace with the file name to connect to the popen ipython process. This way the file does not have to be run and imported if it is not fully complete or there are bugs, which would cause the import to fail. --- py-src/ltipy.py | 28 ++++++++++++++++++++++++++-- py-src/ltmain.py | 6 ++++-- py-src/lttools.py | 18 ++++++++++++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/py-src/ltipy.py b/py-src/ltipy.py index 6f65a71..5e2d240 100644 --- a/py-src/ltipy.py +++ b/py-src/ltipy.py @@ -9,6 +9,7 @@ import pickle import json + def noop(): pass @@ -24,15 +25,21 @@ def km_from_string(s=''): or just 'kernel-12345.json' for IPython 0.12 """ global km, kc, send, Empty - from os.path import join as pjoin + from IPython.config.loader import KeyValueConfigLoader - from Queue import Empty + + if int(sys.version.split(' ')[0][0]) > 2: + from queue import Empty + else: + from Queue import Empty + try: from IPython.kernel import ( KernelManager, find_connection_file, ) + except ImportError: from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager from IPython.zmq.kernelapp import kernel_aliases @@ -54,6 +61,7 @@ def km_from_string(s=''): send = km.shell_channel.execute respond(None, "python.client.error.ipython-version", None) + return @@ -84,6 +92,7 @@ def msgId(m): return m['parent_header']['msg_id'] def normalize(m): + mid = msgId(m) content = m['content'] @@ -163,9 +172,17 @@ def msgloop(): global ipy while not ipy.returncode and not ltmain.stopped(): for m in msgs(): + #mw(str(time.time())+'\n','a') + #mw(str(m)+'\n\n','a') handleMsg(m) time.sleep(0.01) + #def mw(string,mode): + #f = open('/Users/nate/Desktop/log_file.txt',mode) + #f.write(string) + #f.close() + + def initIPy(s): try: cur = km_from_string(s) @@ -175,9 +192,11 @@ def initIPy(s): disconnected() loc = os.path.dirname(__file__) + send("import sys\nsys.path.append('" + loc.replace('\\','\\\\') + "')\nimport lttools") connected() msgloop() + disconnected() except: disconnected() @@ -189,6 +208,7 @@ def IPyOutput(l): m = re.search('--existing (.*\.json)', l) if m: initIPy(m.group(1)) + if re.search('ImportError: IPython.zmq', l): respond(None, "python.client.error.pyzmq", None) @@ -201,6 +221,7 @@ def listenIPy(): disconnected() break IPyOutput(next_line) + def startIPy(opts): global ipy @@ -208,6 +229,7 @@ def startIPy(opts): global disconnected global connected global km + global f respond = opts["respond"] connected = opts["connected"] disconnected = opts["disconnected"] @@ -228,6 +250,8 @@ def startIPy(opts): disconnected() return None + + def killIPy(): global ipy try: diff --git a/py-src/ltmain.py b/py-src/ltmain.py index 574e7ee..e5b2e05 100644 --- a/py-src/ltmain.py +++ b/py-src/ltmain.py @@ -346,10 +346,10 @@ def start(type): sys.stdout.flush() info["type"] = type s.send((json.dumps(info)+ "\n").encode('utf-8')); - sys.stdout = Printer() sys.stderr = Printer() + asyncore.loop() def connected(): @@ -403,6 +403,7 @@ def sendoff(self, msg): curDir = os.getcwd() sys.path.append(curDir) name = os.path.basename(curDir) + #global info try: cid = int(sys.argv[2]) @@ -418,4 +419,5 @@ def sendoff(self, msg): ltipy.startIPy({"respond": send, "connected": connected, "disconnected": disconnected}) - #disconnected() + #disconnected() + diff --git a/py-src/lttools.py b/py-src/lttools.py index 392fc1c..93a8a97 100644 --- a/py-src/lttools.py +++ b/py-src/lttools.py @@ -36,16 +36,30 @@ def watch(exp, meta): sys.stdout.write("__WATCH " + pickle.dumps({"meta": meta, "result": pp.pformat(exp)})) return exp +def factory(): + import imp + NewClass = imp.new_module('') + return NewClass + def toModule(path): name = toModuleNameByPath(path) if name in sys.modules: return sys.modules[name] else: + #not sure what the loop is for as the exec only cared about name and not mname + #ignored this for now parts = name.split(".") for idx in range(len(parts)): mname = ".".join(parts[:idx+1]) - __import__(mname) - exec("import sys", sys.modules[name].__dict__) + #__import__(mname) + dummy = factory() + dummy.__name__ = name + #exec("import sys", sys.modules[name].__dict__) + #not sure why these were exec statements and not just normal statements + #but I followed the same pattern below + exec('import sys') + exec('sys.modules[name] = dummy') + exec('import sys', sys.modules[name].__dict__) return sys.modules[name] def switch_ns(path):