def from_object(obj): if as_i(obj.getitem(space.String(u"version"))) != 0: raise space.unwind(space.LError(u"bytecode version=0 required")) sources = as_list(obj.getitem(space.String(u"sources"))) constants = as_list(obj.getitem(space.String(u"constants"))) functions = [] for function_list in as_list(obj.getitem(space.String(u"functions"))): flags = as_i( function_list.getitem(space.String(u"flags"))) regc = as_i( function_list.getitem(space.String(u"regc"))) argc = rffi.r_ulong(as_i( function_list.getitem(space.String(u"argc")))) topc = rffi.r_ulong(as_i( function_list.getitem(space.String(u"topc")))) localc = as_i( function_list.getitem(space.String(u"localc"))) block_list = as_u8a(function_list.getitem(space.String(u"code"))) sourcemap = function_list.getitem(space.String(u"sourcemap")) exc_table = as_list(function_list.getitem(space.String(u"exceptions"))) block = lltype.malloc(u16_array, block_list.length/2) for i in range(block_list.length/2): a = rffi.r_long(block_list.uint8data[i*2+0]) b = rffi.r_long(block_list.uint8data[i*2+1]) block[i] = rffi.r_ushort((a << 8) | b) excs = [] for n in exc_table: excs.append(Exc( rffi.r_ulong(as_i(n.getitem(space.Integer(0)))), rffi.r_ulong(as_i(n.getitem(space.Integer(1)))), rffi.r_ulong(as_i(n.getitem(space.Integer(2)))), rffi.r_ulong(as_i(n.getitem(space.Integer(3)))), )) functions.append(Function(flags, regc, argc, topc, localc, block, sourcemap, excs[:])) return Program(Unit(constants[:], functions[:], sources[:]))
def Compress_compress_beta(self, inp, out): stream = self.stream i_data = cast(inp.getattr(u"data"), Uint8Data, u"inp.data") i_start = cast(inp.getattr(u"start"), Integer, u"inp.start").value i_stop = cast(inp.getattr(u"stop"), Integer, u"inp.stop").value if not (0 <= i_start <= i_stop <= i_data.length): raise unwind(LTypeError(u"inp.start/stop invalid")) o_data = cast(out.getattr(u"data"), Uint8Data, u"out.data") o_start = cast(out.getattr(u"start"), Integer, u"out.start").value o_stop = cast(out.getattr(u"stop"), Integer, u"out.stop").value if not (0 <= o_start <= o_stop <= o_data.length): raise unwind(LTypeError(u"out.start/stop invalid")) stream.c_next_in = rffi.ptradd(i_data.uint8data, i_start) stream.c_avail_in = rffi.r_uint(i_stop - i_start) stream.c_next_out = rffi.ptradd(o_data.uint8data, o_stop) stream.c_avail_out = rffi.r_uint(o_data.length - o_stop) if i_start == i_stop: err = rzlib._deflate(stream, rzlib.Z_FINISH) else: err = rzlib._deflate(stream, 0) if err == rzlib.Z_NEED_DICT: raise unwind(LError(u"Z_NEED_DICT")) if err == rzlib.Z_BUF_ERROR: raise unwind(LError(u"Z_BUF_ERROR")) if err == rzlib.Z_OK or err == rzlib.Z_STREAM_END: inp.setattr(u"start", Integer(i_stop - rffi.r_long(stream.c_avail_in))) out.setattr(u"stop", Integer(rffi.r_long(stream.c_avail_out) + o_stop)) return null
def write_message(self, std, text): if core.get_ec() != self.ec: # Just discard those damn messages if return # they come up in wrong thread. array = space.to_uint8array(text) bufs, nbufs = uv_callback.obj2bufs(array) if isinstance(std, fs.WriteStream) and not std.fileobj.closed: fd = std.fileobj.fd req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) _ = uv.fs_write(self.ec.uv_loop, req, fd, bufs, nbufs, std.offset, lltype.nullptr(uv.fs_cb.TO)) if rffi.r_long(req.c_result) > 0: std.offset += rffi.r_long(req.c_result) lltype.free(bufs, flavor="raw") lltype.free(req, flavor="raw") elif isinstance(std, uv_stream.Stream) and not std.closed: stream = std.stream req = lltype.malloc(uv.write_ptr.TO, flavor='raw', zero=True) req.c_data = rffi.cast(rffi.VOIDP, bufs) res = uv.write(req, stream, bufs, nbufs, _logging_write_callback_) if rffi.r_long(res) < 0: lltype.free(bufs, flavor="raw") lltype.free(req, flavor="raw") else: return
def getattr(self, name): if name == u"doc": return self.doc elif name == u"loc": unit = self.function.unit trace = TraceEntry(0, unit.sources, self.function.sourcemap, unit.path) name, col0, lno0, col1, lno1 = trace.pc_location() start = space.Exnihilo() start.setattr(u"col", space.Integer(col0)) start.setattr(u"lno", space.Integer(lno0)) stop = space.Exnihilo() stop.setattr(u"col", space.Integer(col1)) stop.setattr(u"lno", space.Integer(lno1)) obj = space.Exnihilo() obj.setattr(u"source", name) obj.setattr(u"start", start) obj.setattr(u"stop", stop) return obj elif name == u"spec": spec = space.Exnihilo() spec.setattr(u'argc', space.Integer(rffi.r_long(self.function.argc))) spec.setattr( u'optional', space.Integer( rffi.r_long(self.function.topc - self.function.argc))) spec.setattr(u'is_variadic', space.boolean(self.function.flags & 1 == 1)) spec.setattr(u'varnames', self.function.varnames) return spec else: return space.Object.getattr(self, name)
def from_object(obj): if as_i(obj.getitem(space.String(u"version"))) != 0: raise space.unwind(space.LError(u"bytecode version=0 required")) sources = as_list(obj.getitem(space.String(u"sources"))) constants = as_list(obj.getitem(space.String(u"constants"))) functions = [] for function_list in as_list(obj.getitem(space.String(u"functions"))): flags = as_i(function_list.getitem(space.String(u"flags"))) regc = as_i(function_list.getitem(space.String(u"regc"))) argc = rffi.r_ulong(as_i(function_list.getitem(space.String(u"argc")))) topc = rffi.r_ulong(as_i(function_list.getitem(space.String(u"topc")))) localc = as_i(function_list.getitem(space.String(u"localc"))) block_list = as_u8a(function_list.getitem(space.String(u"code"))) sourcemap = function_list.getitem(space.String(u"sourcemap")) exc_table = as_list(function_list.getitem(space.String(u"exceptions"))) block = lltype.malloc(u16_array, block_list.length / 2) for i in range(block_list.length / 2): a = rffi.r_long(block_list.uint8data[i * 2 + 0]) b = rffi.r_long(block_list.uint8data[i * 2 + 1]) block[i] = rffi.r_ushort((a << 8) | b) excs = [] for n in exc_table: excs.append( Exc( rffi.r_ulong(as_i(n.getitem(space.Integer(0)))), rffi.r_ulong(as_i(n.getitem(space.Integer(1)))), rffi.r_ulong(as_i(n.getitem(space.Integer(2)))), rffi.r_ulong(as_i(n.getitem(space.Integer(3)))), )) functions.append( Function(flags, regc, argc, topc, localc, block, sourcemap, excs[:])) return Program(Unit(constants[:], functions[:], sources[:]))
def getattr(self, name): if name == u"doc": return self.doc elif name == u"loc": source, start, stop = self.source_location obj = space.Exnihilo() obj.setattr(u"source", space.String(source)) obj.setattr(u"start", space.Integer(start)) obj.setattr(u"stop", space.Integer(stop)) return obj elif name == u"spec": argc, optional, variadic, varnames, argtypes = self.spec varnames = [ space.String(name.decode('utf-8')) for name in varnames ] spec = space.Exnihilo() spec.setattr(u'argc', space.Integer(rffi.r_long(argc))) spec.setattr(u'optional', space.Integer(rffi.r_long(optional))) spec.setattr(u'is_variadic', space.boolean(variadic)) spec.setattr(u'varnames', space.List(list(varnames))) if argtypes is not None: spec.setattr(u'argtypes', space.List(list(argtypes))) else: spec.setattr(u'argtypes', space.null) return spec else: return Object.getattr(self, name)
def from_file(path): stream = bincode.decoder.open_file(path) assert stream.read(8) == bincode.common.header functions = [] for i in range(stream.read_u16()): flags = stream.read_u16() regc = stream.read_u16() argc = stream.read_u16() localc = stream.read_u16() blocklen = stream.read_u16() block = lltype.malloc(u16_array, blocklen) for i in range(blocklen): block[i] = rffi.r_ushort(stream.read_u16()) functions.append(Function(flags, regc, argc, localc, block)) constants = [] for i in range(stream.read_u16()): klass = stream.read_ubyte() if klass == 1: constants.append(space.String(stream.read_string())) elif klass == 2: constants.append(space.Integer(rffi.r_long(stream.read_u64()))) elif klass == 3: constants.append(space.Integer(-rffi.r_long(stream.read_u64()))) elif klass == 4: constants.append(space.Float(stream.read_double())) else: assert False, klass return Program(Unit(constants[:], functions[:]))
def write_message(self, std, text): if core.g.ec.get() is not self.ec: self.log_dump.append(text) return if std is self.stderr: text = "\n".join(self.log_dump + [text]) self.log_dump = [] array = space.to_uint8array(text) bufs, nbufs = uv_callback.obj2bufs(array) if isinstance(std, fs.WriteStream) and not std.fileobj.closed: fd = std.fileobj.fd req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) _ = uv.fs_write(self.ec.uv_loop, req, fd, bufs, nbufs, std.offset, lltype.nullptr(uv.fs_cb.TO)) if rffi.r_long(req.c_result) > 0: std.offset += rffi.r_long(req.c_result) lltype.free(bufs, flavor="raw") lltype.free(req, flavor="raw") elif isinstance(std, uv_stream.Stream) and not std.closed: stream = std.stream req = lltype.malloc(uv.write_ptr.TO, flavor='raw', zero=True) req.c_data = rffi.cast(rffi.VOIDP, bufs) res = uv.write(req, stream, bufs, nbufs, _logging_write_callback_) if rffi.r_long(res) < 0: lltype.free(bufs, flavor="raw") lltype.free(req, flavor="raw") else: return
def addressof(obj): if isinstance(obj, Handle): return Integer(rffi.r_long(rffi.cast(rffi.LONG, obj.pointer))) elif isinstance(obj, Mem): return Integer(rffi.r_long(rffi.cast(rffi.LONG, obj.pointer))) elif isinstance(obj, Uint8Data): return Integer(rffi.r_long(rffi.cast(rffi.LONG, obj.uint8data))) raise unwind(LTypeError(u"Can get address of memory locations only"))
def excs_introspection(excs): out = [] for exc in excs: o = space.Exnihilo() o.setattr(u"start", space.Integer(rffi.r_long(exc.start))) o.setattr(u"stop", space.Integer(rffi.r_long(exc.stop))) o.setattr(u"label", space.Integer(rffi.r_long(exc.label))) o.setattr(u"reg", space.Integer(rffi.r_long(exc.reg))) out.append(o) return space.List(out)
def getaddrinfo(node, service): req = lltype.malloc(uv.getaddrinfo_ptr.TO, flavor='raw', zero=True) if node is None: node_string = lltype.nullptr(rffi.CCHARP.TO) else: node_string = rffi.str2charp(node.string.encode('utf-8')) if service is None: service_string = lltype.nullptr(rffi.CCHARP.TO) else: service_string = rffi.str2charp(service.string.encode('utf-8')) try: response = uv_callback.getaddrinfo(req) status, res = response.wait( uv.getaddrinfo(response.ec.uv_loop, req, uv_callback.getaddrinfo.cb, node_string, service_string, lltype.nullptr(uv.addrinfo_ptr.TO))) if rffi.r_long(status) < 0: raise uv_callback.to_error(status) this = res result = [] while this != lltype.nullptr(uv.addrinfo_ptr.TO): entry = Exnihilo() entry.setattr(u"flags", Integer(rffi.r_long(this.c_ai_flags))) entry.setattr(u"family", Integer(rffi.r_long(this.c_ai_family))) entry.setattr(u"socktype", Integer(rffi.r_long(this.c_ai_socktype))) entry.setattr(u"protocol", Integer(rffi.r_long(this.c_ai_protocol))) entry.setattr( u"addr", copy_to_uint8array(rffi.cast(rffi.VOIDP, this.c_ai_addr), this.c_ai_addrlen, rffi.sizeof(uv.sockaddr_storage))) if this.c_ai_canonname: entry.setattr( u"canonname", from_cstring(rffi.charp2str(this.c_ai_canonname))) else: entry.setattr(u"canonname", null) result.append(entry) this = rffi.cast(uv.addrinfo_ptr, this.c_ai_next) uv.freeaddrinfo(res) return List(result) finally: if node_string: lltype.free(node_string, flavor='raw') if service_string: lltype.free(service_string, flavor='raw') lltype.free(req, flavor='raw')
def TTY_get_winsize(self): self.check_closed() width = lltype.malloc(rffi.INTP.TO, 1, flavor='raw', zero=True) height = lltype.malloc(rffi.INTP.TO, 1, flavor='raw', zero=True) try: check(uv.tty_get_winsize(self.tty, width, height)) w = rffi.r_long(width[0]) h = rffi.r_long(height[0]) obj = Exnihilo() obj.setattr(u"width", Integer(w)) obj.setattr(u"height", Integer(h)) return obj finally: lltype.free(width, flavor='raw') lltype.free(height, flavor='raw')
def scandir(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) dirent = lltype.malloc(uv.dirent_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_scandir( response.ec.uv_loop, req, path, 0, # TODO: check if there are meaningful flags for this. uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) listing = [] while True: res = uv.fs_scandir_next(req, dirent) if res == uv.EOF: break elif res < 0: raise uv_callback.to_error(res) entry = Exnihilo() entry.setattr(u"path", from_cstring(rffi.charp2str(dirent.c_name))) if dirent.c_type in uv.dirent2name: entry.setattr(u"type", String(uv.dirent2name[dirent.c_type])) else: entry.setattr(u"type", Integer(rffi.r_long(dirent.c_type))) listing.append(entry) return List(listing) finally: uv.fs_req_cleanup(req) lltype.free(dirent, flavor='raw') lltype.free(req, flavor='raw')
def getattr(self, name): if name == u"closure": return self.closure elif name == u"is_generator": return space.boolean(self.closure.function.flags & 2 != 0) elif name == u"excs": return self.excs_l elif name == u"regc": return space.Integer(rffi.r_long(self.closure.function.regc)) elif name == u"localc": return space.Integer(rffi.r_long(self.closure.function.localc)) elif name == u"length": return space.Integer(len(self.closure.function.block)) elif name == u"module": return self.closure.frame.module return space.Object.getattr(self, name)
def getitem(self, index): if isinstance(index, space.Slice): start, stop, step = index.clamped(0, self.length - 1) if step != 1: result = [] # TODO: keep it as Uint8Array? for i in range(start, stop, step): result.append( numbers.Integer(rffi.r_long(self.uint8data[i]))) return space.List(result) parent = self.parent if isinstance(self, Uint8Slice) else self return Uint8Slice(rffi.ptradd(self.uint8data, start), stop - start, parent) index = space.cast(index, numbers.Integer, u"index not an integer") if not 0 <= index.value < self.length: raise space.unwind(space.LKeyError(self, index)) return numbers.Integer(rffi.r_long(self.uint8data[index.value]))
def UDP_recv(self): self.check_closed() ec = core.get_ec() if len(self.read_queue) == 0: uv_callback.push(ec.uv__udp_recv, self) status = uv.udp_recv_start(self.udp, _udp_alloc_callback_, _udp_read_callback_once_) if status < 0: uv_callback.drop(ec.uv__udp_recv, self.udp) raise uv_callback.to_error(status) if len(self.read_queue) == 0: if self.read_greenlet is not None: raise unwind(LError(u"async collision")) self.read_greenlet = ec.current core.switch([ec.eventloop]) array, nread, status, addr, flags = self.read_queue.pop(0) if nread < 0: raise uv_callback.to_error(nread) if status < 0: raise uv_callback.to_error(status) if array is None: array = Uint8Slice(lltype.nullptr(rffi.UCHARP.TO), 0, None) elif array.length != nread: array = array.subslice(nread) res = Exnihilo() res.setattr(u"data", array) res.setattr(u"addr", addr) res.setattr(u"flags", Integer(rffi.r_long(flags))) return res
def Handle_get_fileno(self): fd = lltype.malloc(rffi.LONGP.TO, 1, flavor='raw', zero=True) try: check(uv.fileno(self.handle, fd)) return Integer(rffi.r_long(fd[0])) finally: lltype.free(fd, flavor='raw')
def Uint8Iterator_next(self): if self.index < self.length: res = self.uint8data[self.index] self.index += 1 return space.Integer(rffi.r_long(res)) else: raise StopIteration()
def call(self, argv): varargs = self.function.flags & 1 == 1 argc = self.function.argc topc = self.function.topc L = len(argv) if L < argc: # The pc=0 refers to the function itself. This entry # is really helpful when trying to determine the origin of a CallError. head_entry = TraceEntry(rffi.r_long(0), self.function.unit.sources, self.function.sourcemap, self.function.unit.path) unwinder = space.unwind(space.LCallError(argc, topc, varargs, L)) unwinder.traceback.contents.append(head_entry) raise unwinder # We are using this trait. #if L > topc and not varargs: # raise space.Error(u"too many arguments [%d], from %d to %d arguments allowed" % (L, argc, topc)) frame = Frame(self.function, self.frame.module, self.frame) for i in range(min(topc, L)): frame.local[i] = argv[i] if varargs: frame.local[topc] = space.List(argv[min(topc, L):]) regv = new_register_array(self.function.regc) if self.function.flags & 2 != 0: return Generator(0, self.function.block, regv, frame) else: return interpret(0, self.function.block, regv, frame)
def Handle_get_recv_buffer_size(self): self.check_closed() value = lltype.malloc(rffi.INTP.TO, 1, flavor='raw', zero=True) try: check(uv.recv_buffer_size(self.handle, value)) return Integer(rffi.r_long(value[0])) finally: lltype.free(value, flavor='raw')
def Stream_try_write(self, data): self.check_closed() bufs, nbufs = uv_callback.obj2bufs(data) try: status = uv.try_write(self.stream, bufs, nbufs) check(status) return Integer(rffi.r_long(status)) finally: lltype.free(bufs, flavor='raw')
def to_error(result): for errno, Exc in errors: if errno == rffi.r_long(result): return space.unwind(Exc()) else: return space.unwind( space.LUVError( rffi.charp2str(uv.err_name(result)).decode('utf-8'), rffi.charp2str(uv.strerror(result)).decode('utf-8')))
def Union_get_fields(self): fields = [] for name in self.namespace: offset, ctype = self.namespace[name] fields.append( List([String(name), Integer(rffi.r_long(offset)), unshadow(ctype)])) return List(fields)
def int2Rational(num, den): assert isinstance(num, int) assert isinstance(den, int) if den < 0: # expects den to be an unsigned long int num, den = (-1) * num, (-1) * den result = Rational() c_mpq_set_si(result.value, rffi.r_long(num), rffi.r_ulong(den)) c_mpq_canonicalize(result.value) return result
def _udp_alloc_callback_(handle, suggested_size, buf): ec = core.get_ec() self = uv_callback.peek(ec.uv__udp_recv, handle) if self.read_buffer_size > 0: array = alloc_uint8array(self.read_buffer_size) else: array = alloc_uint8array(rffi.r_long(suggested_size)) self.alloc_buffers.append(array) buf.c_base = rffi.cast(rffi.CCHARP, array.uint8data) buf.c_len = rffi.r_size_t(array.length)
def eventloop(self): ''' Poll each socket in turn and respond to any messages. For long running evaluations the heartbeat message may be sent too late ''' pollitem = rffi.lltype.malloc(rzmq.pollitem_t, flavor='raw') pollitem.c_events = rffi.r_short(rzmq.POLLIN) while True: # Shell pollitem.c_socket = self.shell rc = rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(100)) if rc > 0: rlogging.debug('shell message') request = self.msg_recv(self.shell) self.msg_send( self.iopub, self.construct_message( # report kernel busy request[0], request[3], '{"execution_state":"busy"}', 'status')) self.shell_msg(request) self.msg_send( self.iopub, self.construct_message( # report kernel idle request[0], request[3], '{"execution_state":"idle"}', 'status')) # Control pollitem.c_socket = self.control while rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(1)) > 0: rlogging.debug('control message') request = self.msg_recv(self.control) self.control_msg(request) # HB pollitem.c_socket = self.hb while rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(1)) > 0: rlogging.debug('hearbeat message') request = self.msg_recv(self.hb) self.hb_msg(request)
def int_shl(a, b): a_v = int(a.value) b_v = int(b.value) if b_v < LONG_BIT: # 0 <= b < LONG_BIT c = ovfcheck(a_v << b_v) return Integer(rffi.r_long(c)) if b_v < 0: raise space.unwind(space.LError(u"negative shift count")) # b_v >= LONG_BIT if a_v == 0: return a raise OverflowError
def getattr(self, name): if name == u"doc": return self.doc elif name == u"loc": return sourceloc_introspection(0, self.function) elif name == u"spec": spec = space.Exnihilo() spec.setattr(u'argc', space.Integer(rffi.r_long(self.function.argc))) spec.setattr( u'optional', space.Integer( rffi.r_long(self.function.topc - self.function.argc))) spec.setattr(u'is_variadic', space.boolean(self.function.flags & 1 == 1)) spec.setattr(u'varnames', self.function.varnames) return spec elif name == u"code": return Introspection(self) else: return space.Object.getattr(self, name)
def sendfile(out_file, in_file, in_offset, length): req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_sendfile(response.ec.uv_loop, req, out_file.fd, in_file.fd, in_offset.value, length.value, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return Integer(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def _fs_event_cb_(handle, filename, events, status): ec = core.get_ec() try: self = uv_callback.peek(ec.uv__fs_event, handle) status = rffi.r_long(status) if self.status == 0 and status < 0: uv_callback.drop(ec.uv__fs_event, handle) self.status = status return obj = Exnihilo() obj.setattr(u"path", from_cstring(rffi.charp2str(filename))) if rffi.r_long(events) == 1: obj.setattr(u"type", String(u"rename")) else: obj.setattr(u"type", String(u"change")) if self.greenlet is None: self.data.append(obj) else: greenlet, self.greenlet = self.greenlet, None core.switch([greenlet, obj]) except Unwinder as unwinder: core.root_unwind(ec, unwinder)
def get_function_name(function): entry = TraceEntry(rffi.r_long(0), function.unit.sources, function.sourcemap, function.unit.path) source, col0, lno0, col1, lno1 = entry.pc_location() if isinstance(source, pathobj.Path): source = pathobj.Path_to_string(source) if isinstance(source, space.String): src = source.string.encode('utf-8') else: src = source.repr().encode('utf-8') if len(src) > 200: # Must not be longer than 255 chars. src = src[:197] + '...' src = src.replace(':', ';') return "py:L%s:%d:%s" % ("%d_%d" % (lno0, lno1), lno0, src)
def File_pwrite(self, data, offset): bufs, nbufs = uv_callback.obj2bufs(data) req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_write(response.ec.uv_loop, req, self.fd, bufs, nbufs, offset.value, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return Integer(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(bufs, flavor='raw') lltype.free(req, flavor='raw')
def eventloop(self): ''' Poll each socket in turn and respond to any messages. For long running evaluations the heartbeat message may be sent too late ''' pollitem = rffi.lltype.malloc(rzmq.pollitem_t, flavor='raw') pollitem.c_events = rffi.r_short(rzmq.POLLIN) while True: # Shell pollitem.c_socket = self.shell rc = rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(100)) if rc > 0: rlogging.debug('shell message') request = self.msg_recv(self.shell) self.msg_send(self.iopub, self.construct_message( # report kernel busy request[0], request[3], '{"execution_state":"busy"}', 'status')) self.shell_msg(request) self.msg_send(self.iopub, self.construct_message( # report kernel idle request[0], request[3], '{"execution_state":"idle"}', 'status')) # Control pollitem.c_socket = self.control while rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(1)) > 0: rlogging.debug('control message') request = self.msg_recv(self.control) self.control_msg(request) # HB pollitem.c_socket = self.hb while rzmq.poll(pollitem, rffi.r_int(1), rffi.r_long(1)) > 0: rlogging.debug('hearbeat message') request = self.msg_recv(self.hb) self.hb_msg(request)
def rlong(fd): "http://en.wikipedia.org/wiki/Variable-length_quantity" # Slightly broken, doesn't handle integer overflow. sign = +1 output = 0 ubyte = ord(fd.read(1)[0]) if ubyte & 0x40 == 0x40: sign = -1 ubyte &= 0xBF while ubyte & 0x80: output |= ubyte & 0x7F output <<= 7 ubyte = ord(fd.read(1)[0]) output |= ubyte return Integer(rffi.r_long(output * sign))
def read(argv): if len(argv) < 1: raise OldError(u"too few arguments to file.read()") self = argv[0] if not isinstance(self, File): raise OldError(u"expected file object") if len(argv) > 1: count_or_dst = argv[1] if isinstance(count_or_dst, Uint8Array): dst = count_or_dst data = rffi.cast(rffi.CCHARP, dst.uint8data) count = rfile.c_fread(data, 1, dst.length, self.fd._ll_file) return Integer(rffi.r_long(count)) else: count = to_int(count_or_dst) return to_uint8array(self.fd.read(int(count))) else: return to_uint8array(self.fd.read())
def getitem(self, index): index = space.cast(index, numbers.Integer, u"index not an integer") if not 0 <= index.value < self.length: raise space.unwind(space.LKeyError(self, index)) return numbers.Integer(rffi.r_long(self.uint8data[index.value]))
def interpret(pc, block, regv, frame): module = jit.promote(frame.module) unit = jit.promote(frame.unit) excs = jit.promote(frame.excs) try: while pc < len(block): try: jitdriver.jit_merge_point( pc=pc, block=block, module=module, unit=unit, excs=excs, regv=regv, frame=frame) opcode = rffi.r_ulong(block[pc])>>8 ix = pc+1 pc = ix+(rffi.r_ulong(block[pc])&255) #print optable.dec[opcode][0] if opcode == opcode_of('assert'): obj = regv.load(block[ix+0]) raise space.unwind(space.LAssertionError(obj)) elif opcode == opcode_of('raise'): obj = regv.load(block[ix+0]) traceback = obj.getattr(u"traceback") if traceback is space.null: traceback = space.List([]) obj.setattr(u"traceback", traceback) elif not isinstance(traceback, space.List): raise space.unwind(space.LError(u"Expected null or list as .traceback: %s" % obj.repr())) raise space.Unwinder(obj, traceback) elif opcode == opcode_of('constant'): regv.store(block[ix+0], unit.constants[block[ix+1]]) elif opcode == opcode_of('list'): contents = [] for i in range(ix+1, pc): contents.append(regv.load(block[i])) regv.store(block[ix], space.List(contents)) elif opcode == opcode_of('move'): regv.store(block[ix+0], regv.load(block[ix+1])) elif opcode == opcode_of('call'): op_call(regv, block, ix, pc) elif opcode == opcode_of('callv'): op_callv(regv, block, ix, pc) elif opcode == opcode_of('return'): return regv.load(block[ix+0]) elif opcode == opcode_of('jump'): pc = rffi.r_ulong(block[ix+0]) elif opcode == opcode_of('cond'): if space.is_false(regv.load(block[ix+0])): pc = rffi.r_ulong(block[ix+2]) else: pc = rffi.r_ulong(block[ix+1]) elif opcode == opcode_of('func'): regv.store(block[ix+0], Closure(frame, unit.functions[block[ix+1]])) elif opcode == opcode_of('iter'): regv.store(block[ix+0], regv.load(block[ix+1]).iter()) elif opcode == opcode_of('next'): try: regv.store(block[ix+0], regv.load(block[ix+1]).callattr(u'next', [])) except StopIteration as _: pc = rffi.r_ulong(block[ix+2]) # this is missing. #elif isinstance(op, Yield): # raise YieldIteration(op.block, loop_break, op.i, regv.load(op.value.i)) elif opcode == opcode_of('getattr'): name = get_string(unit, block, ix+2) obj = regv.load(block[ix+1]) regv.store(block[ix+0], obj.getattr(name)) elif opcode == opcode_of('setattr'): value = regv.load(block[ix+3]) name = get_string(unit, block, ix+2) obj = regv.load(block[ix+1]) regv.store(block[ix+0], obj.setattr(name, value)) elif opcode == opcode_of('getitem'): index = regv.load(block[ix+2]) obj = regv.load(block[ix+1]) regv.store(block[ix+0], obj.getitem(index)) elif opcode == opcode_of('setitem'): item = regv.load(block[ix+3]) index = regv.load(block[ix+2]) obj = regv.load(block[ix+1]) regv.store(block[ix+0], obj.setitem(index, item)) elif opcode == opcode_of('getloc'): regv.store(block[ix+0], frame.local[block[ix+1]]) elif opcode == opcode_of('setloc'): value = regv.load(block[ix+2]) frame.local[block[ix+1]] = value regv.store(block[ix+0], value) elif opcode == opcode_of('getupv'): value = get_upframe(frame, block[ix+1]).local[block[ix+2]] regv.store(block[ix+0], value) elif opcode == opcode_of('setupv'): value = regv.load(block[ix+3]) get_upframe(frame, block[ix+1]).local[block[ix+2]] = value regv.store(block[ix+0], value) elif opcode == opcode_of('getglob'): regv.store(block[ix+0], module.getattr(get_string(unit, block, ix+1))) elif opcode == opcode_of('setglob'): regv.store(block[ix+0], module.setattr( get_string(unit, block, ix+1), regv.load(block[ix+2]))) elif opcode == opcode_of('not'): if space.is_false(regv.load(block[ix+1])): regv.store(block[ix+0], space.true) else: regv.store(block[ix+0], space.false) elif opcode == opcode_of('contains'): v0 = regv.load(block[ix+1]) v1 = regv.load(block[ix+2]) if v0.contains(v1): regv.store(block[ix+0], space.true) else: regv.store(block[ix+0], space.false) else: raise space.unwind(space.LInstructionError( optable.names.get(opcode, str(opcode)).decode('utf-8'), opcode)) except space.Unwinder as unwinder: #print "exception detected, doing unwinds", pc, unwinder.exception.repr() for exc in excs: #print exc.start, exc.stop, exc.label, exc.reg if exc.start < pc <= exc.stop: regv.store(exc.reg, unwinder.exception) pc = exc.label #print "exception handler found" break else: raise except StopIteration as stop: unwinder = space.unwind(space.LUncatchedStopIteration()) unwinder.traceback.contents.append(TraceEntry(rffi.r_long(pc), unit.sources, frame.sourcemap)) raise unwinder except space.Unwinder as unwinder: unwinder.traceback.contents.append(TraceEntry(rffi.r_long(pc), unit.sources, frame.sourcemap)) raise return space.null
def fileno(self): return Integer(rffi.r_long(self.fd.fileno()))
def write(self, src): data = rffi.cast(rffi.CCHARP, src.uint8data) count = rfile.c_fwrite(data, 1, src.length, self.fd._ll_file) return Integer(rffi.r_long(count))
def nextbyte(sourcemap, i): if i < sourcemap.length: return i+1, rffi.r_long(sourcemap.uint8data[i]) return i, 0
def int2Integer(value): assert isinstance(value, int) result = Integer() c_mpz_set_si(result.value, rffi.r_long(value)) return result
def getitem(self, index): if not isinstance(index, numbers.Integer): raise space.OldError(u"index not an integer") if not 0 <= index.value < self.length: raise space.OldError(u"index out of range") return numbers.Integer(rffi.r_long(self.uint8data[index.value]))