Skip to content

Commit f07e71a

Browse files
committed
Fix many bugs. Now works, slightly.
Add dummy completer
1 parent 50e52a4 commit f07e71a

File tree

4 files changed

+69
-33
lines changed

4 files changed

+69
-33
lines changed

completer.lua

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
local KernelCompleter = torch.class("ipython.KernelCompleter")
3+
4+
function KernelCompleter:__init(namespace)
5+
self.namespace = namespace
6+
7+
end
8+
9+
function KernelCompleter:complete(line, text)
10+
local matches = {}
11+
-- TODO completion
12+
return matches
13+
end

ipython-0-0.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description = {
1111
homepage = "https://github.com/d11/torch-ipython"
1212
}
1313

14-
dependencies = { 'torch >= 7.0', 'parallel', 'uuid'}
14+
dependencies = { 'torch >= 7.0', 'parallel', 'uuid', 'luajson'}
1515
build = {
1616
type = "command",
1717
build_command = [[

kernel.lua

+52-30
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
require 'libluazmq'
22
require 'socket'
3+
require 'json'
34

45
ipython = {}
56

67
dofile('session.lua')
8+
dofile('completer.lua')
79

810
--! A file like object that publishes the stream to a 0MQ PUB socket.
911
local OutStream = torch.class("ipython.OutStream")
1012

1113
function OutStream:__init(session, pub_socket, name, max_buffer)
14+
max_buffer = max_buffer or 200
1215
self.session = session
1316
self.pub_socket = pub_socket
1417
self.name = name
@@ -33,11 +36,11 @@ function OutStream:flush()
3336
if self._buffer then
3437
local data = table.concat(self._buffer)
3538
local content = { name = self.name, data = data }
36-
local msg = self.session.msg('stream', content, self.parent_header)
39+
local msg = self.session:msg('stream', content, self.parent_header)
3740
print(ipython.Message(msg))
38-
self.pub_socet.send_json(msg)
41+
self.pub_socket:send(json.encode(msg))
3942
self._buffer_len = 0
40-
self._bufer = {}
43+
self._buffer = {}
4144
end
4245
end
4346
end
@@ -63,7 +66,7 @@ function OutStream:write(s)
6366
end
6467

6568
function OutStream:_maybe_send()
66-
if string.find(self.buffer[#self.buffer], "\n") then
69+
if string.find(self._buffer[#self._buffer], "\n") then
6770
self:flush()
6871
end
6972
if self._buffer_len > self.max_buffer then
@@ -95,7 +98,7 @@ function DisplayHook:__call(obj)
9598

9699
-- __builtin__._ = obj -- ?
97100
local msg = self.session:msg("pytout", { data = tostring(obj) }, self.parent_header)
98-
self.pub_socket:send_json(msg)
101+
self.pub_socket:send(json.encode(msg))
99102
end
100103
function DisplayHook:set_parent(parent)
101104
self.parent_header = extract_header(parent)
@@ -110,9 +113,9 @@ end
110113

111114
function RawInput:__call(prompt)
112115
local msg = self.session:msg('raw_input')
113-
self.socket:send_json(msg)
116+
self.socket:send(json.encode(msg))
114117
while true do
115-
local result, msg = self.socket:recv_json(zmq.NOBLOCK)
118+
local result, msg = json.decode(self.socket:recv(zmq.NOBLOCK))
116119
if result then
117120
return msg.content.data
118121
end
@@ -123,14 +126,16 @@ function RawInput:__call(prompt)
123126
end
124127

125128
local Kernel = torch.class("ipython.Kernel")
126-
function Kernel:__init(session, reply_socket, pub_socket)
129+
function Kernel:__init(session, reply_socket, pub_socket, stdout, stderr)
130+
self.stdout = stdout
131+
self.stderr = stderr
127132
self.session = session
128133
self.reply_socket = reply_socket
129134
self.pub_socket = pub_socket
130135
self.user_ns = {}
131136
self.history = {}
132-
self.compiler = CommandCompiler()
133-
self.completer = KernelCompleter(self.user_ns)
137+
-- self.compiler = CommandCompiler()
138+
self.completer = ipython.KernelCompleter(self.user_ns)
134139

135140
-- Build dict of handlers for message types
136141
self.handlers = {}
@@ -152,14 +157,14 @@ function Kernel:abort_queue()
152157
if self.reply_socket.rcvmore ~= 0 then
153158
error("Unexpected missing message part")
154159
end
155-
msg = self.reply_socket:recv_json()
160+
msg = json.decode(self.reply_socket:recv())
156161
print("Aborting:", ipython.Message(msg))
157162
local msg_type = msg.msg_type
158163
local reply_type = msg_type:gmatch("_")[1] .. "_reply"
159164
local reply_msg = self.session.msg(reply_type, { status = 'aborted'}, msg)
160165
print(ipython.Message(reply_msg))
161166
self.reply_socket:send(ident, zmq.SNDMORE)
162-
self.reply_socket:send_json(reply_msg)
167+
self.reply_socket:send(json.encode(reply_msg))
163168
socket.sleep(0.1)
164169
end
165170
end
@@ -171,12 +176,17 @@ function Kernel:execute_request(ident, parent)
171176
end
172177
local code = parent.content.code
173178
local pyin_msg = self.session:msg('pyin', {code=code}, parent)
174-
self.pub_socket:send_json(pyin_msg)
175-
local comp_code = self.compiler(code, '<zmq-kernel>')
179+
self.pub_socket:send(json.encode(pyin_msg))
180+
-- local comp_code = self.compiler(code, '<zmq-kernel>')
181+
local comp_code = code
176182
-- TODO sys.displayhook.set_parent(parent)
177-
local func = function() loadstring(comp_code) end
178-
setfenv(func, self.user_ns)
179-
local result, returned = pcall(func())
183+
local func, msg = loadstring(comp_code)
184+
local result
185+
local returned = msg
186+
if func then
187+
setfenv(func, self.user_ns)
188+
result, returned = pcall(func)
189+
end
180190
local reply_content
181191
if not result then
182192
local res = 'error'
@@ -188,15 +198,15 @@ function Kernel:execute_request(ident, parent)
188198
evalue = returned
189199
}
190200
local exc_msg = self.session:msg('pyerr', exc_content, parent)
191-
self.pub_socket:send_json(exc_msg)
201+
self.pub_socket:send(json.encode(exc_msg))
192202
reply_content = exc_content
193-
else
203+
else
194204
reply_content = {status = 'ok'}
195205
end
196206
local reply_msg = self.session:msg('execute_reply', reply_content, parent)
197207
print(ipython.Message(reply_msg))
198-
self.reply_socket:send(ident, zmq.SNDMORE)
199-
self.reply_socket:send_json(reply_msg)
208+
-- self.reply_socket:send(ident, zmq.SNDMORE) -- TODO ?
209+
self.reply_socket:send(json.encode(reply_msg))
200210
if reply_msg.content.status == 'error' then
201211
self:abort_queue()
202212
end
@@ -215,17 +225,21 @@ function Kernel:complete(msg)
215225
return self.completer:complete(msg.content.line, msg.content.text)
216226
end
217227
function Kernel:start()
228+
print("starting....")
218229
while true do
219-
local ident = self.reply_socket:recv()
220-
assert(self.reply_socket.rcvmore ~= 0, "Unexpected missing message part")
221-
local msg = self.reply_socket:recv_json()
230+
print("waiting on reply socket")
231+
-- local ident = self.reply_socket:recv()
232+
-- print("recieved ident " .. ident)
233+
-- assert(self.reply_socket.rcvmore ~= 0, "Unexpected missing message part")
234+
local msg = json.decode(self.reply_socket:recv())
235+
print("recieved msg ", msg)
222236
local omsg = ipython.Message(msg)
223237
print(omsg)
224-
local handler = self.handler[omsg.msg_type]
238+
local handler = self.handlers[omsg.msg_type]
225239
if not handler then
226240
print("UNKNOWN MESSAGE TYPE: " .. omsg)
227241
else
228-
handler(ident, omsg)
242+
handler(self, ident, omsg)
229243
end
230244
end
231245
end
@@ -250,18 +264,26 @@ function main()
250264

251265
local stdout = ipython.OutStream(session, pub_socket, 'stdout')
252266
local stderr = ipython.OutStream(session, pub_socket, 'stderr')
253-
print = function(args)
254-
stdout:write(table.concat(args))
267+
local newprint = function(args)
268+
print("print", args)
269+
local msg = args
270+
if type(args) == 'table' then
271+
msg = table.concat(args)
272+
end
273+
stdout:write(tostring(msg).. "\n")
255274
end
256-
local display_hook = DisplayHook(session, pub_socket)
275+
local display_hook = ipython.DisplayHook(session, pub_socket)
257276
-- sys.display_hook = display_hook
258277

259278
local kernel = ipython.Kernel(session, reply_socket, pub_socket)
279+
kernel.user_ns['print'] = newprint
280+
kernel.user_ns['torch'] = torch
281+
kernel.user_ns['loadstring'] = loadstring
260282
kernel.user_ns['sleep'] = socket.sleep
261283
kernel.user_ns['s'] = "test string"
262284

263285
print "Use Ctrl-\\ (NOT Ctrl-C!) to terminate."
264-
kernel.start()
286+
kernel:start()
265287

266288
end
267289

session.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'libluazmq'
2+
require 'json'
23
local uuid = require 'uuid'
34

45
local Message = torch.class("ipython.Message")
@@ -78,14 +79,14 @@ function Session:send(socket, msg_type, content, parent, ident)
7879
if ident then
7980
socket:send(ident, zmq.SNDMORE)
8081
end
81-
socket:send_json(msg)
82+
socket:send(json.encode(msg))
8283
local omsg = Message(msg)
8384
return omsg
8485
end
8586

8687
function Session:recv(socket, mode)
8788
mode = mode or zmq.NOBLOCK
88-
local result, msg = pcall(socket:recv_json(mode))
89+
local result, msg = pcall(json.decode(socket:recv(mode)))
8990
if result then
9091
return Message(msg)
9192
else

0 commit comments

Comments
 (0)