1
1
require ' libluazmq'
2
2
require ' socket'
3
+ require ' json'
3
4
4
5
ipython = {}
5
6
6
7
dofile (' session.lua' )
8
+ dofile (' completer.lua' )
7
9
8
10
-- ! A file like object that publishes the stream to a 0MQ PUB socket.
9
11
local OutStream = torch .class (" ipython.OutStream" )
10
12
11
13
function OutStream :__init (session , pub_socket , name , max_buffer )
14
+ max_buffer = max_buffer or 200
12
15
self .session = session
13
16
self .pub_socket = pub_socket
14
17
self .name = name
@@ -33,11 +36,11 @@ function OutStream:flush()
33
36
if self ._buffer then
34
37
local data = table.concat (self ._buffer )
35
38
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 )
37
40
print (ipython .Message (msg ))
38
- self .pub_socet . send_json (msg )
41
+ self .pub_socket : send ( json . encode (msg ) )
39
42
self ._buffer_len = 0
40
- self ._bufer = {}
43
+ self ._buffer = {}
41
44
end
42
45
end
43
46
end
@@ -63,7 +66,7 @@ function OutStream:write(s)
63
66
end
64
67
65
68
function OutStream :_maybe_send ()
66
- if string.find (self .buffer [# self .buffer ], " \n " ) then
69
+ if string.find (self ._buffer [# self ._buffer ], " \n " ) then
67
70
self :flush ()
68
71
end
69
72
if self ._buffer_len > self .max_buffer then
@@ -95,7 +98,7 @@ function DisplayHook:__call(obj)
95
98
96
99
-- __builtin__._ = obj -- ?
97
100
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 ) )
99
102
end
100
103
function DisplayHook :set_parent (parent )
101
104
self .parent_header = extract_header (parent )
110
113
111
114
function RawInput :__call (prompt )
112
115
local msg = self .session :msg (' raw_input' )
113
- self .socket :send_json ( msg )
116
+ self .socket :send ( json . encode ( msg ) )
114
117
while true do
115
- local result , msg = self .socket :recv_json (zmq .NOBLOCK )
118
+ local result , msg = json . decode ( self .socket :recv (zmq .NOBLOCK ) )
116
119
if result then
117
120
return msg .content .data
118
121
end
@@ -123,14 +126,16 @@ function RawInput:__call(prompt)
123
126
end
124
127
125
128
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
127
132
self .session = session
128
133
self .reply_socket = reply_socket
129
134
self .pub_socket = pub_socket
130
135
self .user_ns = {}
131
136
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 )
134
139
135
140
-- Build dict of handlers for message types
136
141
self .handlers = {}
@@ -152,14 +157,14 @@ function Kernel:abort_queue()
152
157
if self .reply_socket .rcvmore ~= 0 then
153
158
error (" Unexpected missing message part" )
154
159
end
155
- msg = self .reply_socket :recv_json ( )
160
+ msg = json . decode ( self .reply_socket :recv () )
156
161
print (" Aborting:" , ipython .Message (msg ))
157
162
local msg_type = msg .msg_type
158
163
local reply_type = msg_type :gmatch (" _" )[1 ] .. " _reply"
159
164
local reply_msg = self .session .msg (reply_type , { status = ' aborted' }, msg )
160
165
print (ipython .Message (reply_msg ))
161
166
self .reply_socket :send (ident , zmq .SNDMORE )
162
- self .reply_socket :send_json ( reply_msg )
167
+ self .reply_socket :send ( json . encode ( reply_msg ) )
163
168
socket .sleep (0.1 )
164
169
end
165
170
end
@@ -171,12 +176,17 @@ function Kernel:execute_request(ident, parent)
171
176
end
172
177
local code = parent .content .code
173
178
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
176
182
-- 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
180
190
local reply_content
181
191
if not result then
182
192
local res = ' error'
@@ -188,15 +198,15 @@ function Kernel:execute_request(ident, parent)
188
198
evalue = returned
189
199
}
190
200
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 ) )
192
202
reply_content = exc_content
193
- else
203
+ else
194
204
reply_content = {status = ' ok' }
195
205
end
196
206
local reply_msg = self .session :msg (' execute_reply' , reply_content , parent )
197
207
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 ) )
200
210
if reply_msg .content .status == ' error' then
201
211
self :abort_queue ()
202
212
end
@@ -215,17 +225,21 @@ function Kernel:complete(msg)
215
225
return self .completer :complete (msg .content .line , msg .content .text )
216
226
end
217
227
function Kernel :start ()
228
+ print (" starting...." )
218
229
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 )
222
236
local omsg = ipython .Message (msg )
223
237
print (omsg )
224
- local handler = self .handler [omsg .msg_type ]
238
+ local handler = self .handlers [omsg .msg_type ]
225
239
if not handler then
226
240
print (" UNKNOWN MESSAGE TYPE: " .. omsg )
227
241
else
228
- handler (ident , omsg )
242
+ handler (self , ident , omsg )
229
243
end
230
244
end
231
245
end
@@ -250,18 +264,26 @@ function main()
250
264
251
265
local stdout = ipython .OutStream (session , pub_socket , ' stdout' )
252
266
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 " )
255
274
end
256
- local display_hook = DisplayHook (session , pub_socket )
275
+ local display_hook = ipython . DisplayHook (session , pub_socket )
257
276
-- sys.display_hook = display_hook
258
277
259
278
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
260
282
kernel .user_ns [' sleep' ] = socket .sleep
261
283
kernel .user_ns [' s' ] = " test string"
262
284
263
285
print " Use Ctrl-\\ (NOT Ctrl-C!) to terminate."
264
- kernel . start ()
286
+ kernel : start ()
265
287
266
288
end
267
289
0 commit comments