class SocketPlugin(Plugin): _attrs_ = ["fds", "sockets", "last_lookup"] def __init__(self): Plugin.__init__(self) self.last_lookup = Cell(None) @staticmethod def startup(space, argv): from rpython.rlib.rsocket import rsocket_startup rsocket_startup() # cannot overload call (plugins are PBCs) so we decorate the decorator @objectmodel.not_rpython def expose_primitive(self, wrap_func=None, **kwargs): if not self.is_enabled(): return lambda x: x # do not install primitives when disabled original_decorator = Plugin.expose_primitive(self, wrap_func=wrap_func, **kwargs) def decorator(func): original_decorator(func) wrapped = self.primitives[func.func_name] def catchall(interp, s_frame, argcount, w_method=None): try: return wrapped(interp, s_frame, argcount, w_method=w_method) except rsocket.SocketError as e: print "!!!SOCKET ERROR!!!" print e.get_msg() print "!!!SOCKET ERROR!!!" raise error.PrimitiveFailedError catchall.func_name = "catchall_" + wrapped.func_name self.primitives[func.func_name] = catchall return func return decorator def set_last_lookup(self, v): self.last_lookup.set(v) def get_last_lookup(self): return self.last_lookup.get() def is_socket(self, space, w_int): return isinstance(w_int, W_SocketHandle)
def __init__(self): Plugin.__init__(self) self.ascii_order = Cell(None)
class ForeignLanguagePlugin(Plugin): _attrs_ = ['_break_on_exceptions_during_sends'] def __init__(self): Plugin.__init__(self) self.register_default_primitives() self._break_on_exceptions_during_sends = Cell(False) @staticmethod def load_special_objects(space, language_name, process_cls, shadow_cls): process_cls.load_special_objects(process_cls, language_name, space) shadow_cls.load_special_objects(shadow_cls, language_name, space) # Abstract methods def is_operational(self): raise NotImplementedError @staticmethod def new_eval_process(space, args_w): raise NotImplementedError @staticmethod def new_send_process(space, w_rcvr, method_name, args_w, break_on_exceptions): raise NotImplementedError @staticmethod def w_object_class(): raise NotImplementedError @staticmethod def to_w_object(foreign_object): raise NotImplementedError @staticmethod def restart_specific_frame(space, args_w): raise NotImplementedError # Default primitives def register_default_primitives(self): @self.expose_primitive(result_is_new_frame=True) def eval(interp, s_frame, argcount): if not self.is_operational(): raise PrimitiveFailedError # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) language_process = self.new_eval_process(interp.space, args_w) # when we are here, the foreign language process has yielded frame = language_process.switch_to_smalltalk( interp, s_frame, first_call=True) s_frame.pop_n(argcount + 1) return frame @self.expose_primitive(unwrap_spec=[object], result_is_new_frame=True) def resume(interp, s_frame, language_process): # print 'Smalltalk yield' # import pdb; pdb.set_trace() if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError language_process.resume() if language_process.error_detected(): raise PrimitiveFailedError return language_process.switch_to_smalltalk(interp, s_frame) @self.expose_primitive(result_is_new_frame=True, compiled_method=True) def send(interp, s_frame, argcount, w_method): if not self.is_operational(): raise PrimitiveFailedError # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) w_rcvr = s_frame.peek(argcount) w_selector_name = w_method.literalat0(interp.space, 2) if not isinstance(w_selector_name, W_BytesObject): raise PrimitiveFailedError method_name = interp.space.unwrap_string(w_selector_name) idx = method_name.find(':') if idx > 0: method_name = method_name[0:idx] language_process = self.new_send_process( interp.space, w_rcvr, method_name, args_w, self._break_on_exceptions_during_sends.get()) frame = language_process.switch_to_smalltalk( interp, s_frame, first_call=True) s_frame.pop_n(argcount + 1) return frame @self.expose_primitive(unwrap_spec=[object]) def lastError(interp, s_frame, language_process): if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError w_error = language_process.get_error() if w_error is None: print 'w_error was None in lastError' raise PrimitiveFailedError return w_error @self.expose_primitive(unwrap_spec=[object]) def topFrame(interp, s_frame, language_process): if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError w_top_frame = language_process.w_top_frame() if w_top_frame is None: raise PrimitiveFailedError return w_top_frame @self.expose_primitive(unwrap_spec=[object]) def asSmalltalk(interp, s_frame, w_rcvr): if not isinstance(w_rcvr, self.w_object_class()): raise PrimitiveFailedError return self.to_w_object(interp.space, w_rcvr) @self.expose_primitive() def restartSpecificFrame(interp, s_frame, argcount): # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) result = self.restart_specific_frame(interp.space, args_w) s_frame.pop_n(argcount + 1) return interp.space.wrap_bool(result) @self.expose_primitive(unwrap_spec=[object, object]) def registerSpecificClass(interp, s_frame, w_rcvr, language_obj): if not isinstance(language_obj, W_ForeignLanguageObject): raise PrimitiveFailedError language_obj.class_shadow(interp.space).set_specific_class(w_rcvr) @self.expose_primitive(unwrap_spec=[object, bool]) def setBreakOnExceptionsDuringSends(interp, s_frame, w_rcvr, value): self._break_on_exceptions_during_sends.set(value) return interp.space.wrap_bool(value)
def __init__(self): Plugin.__init__(self) self.register_default_primitives() self._break_on_exceptions_during_sends = Cell(False)
class ForeignLanguagePlugin(Plugin): _attrs_ = ['_break_on_exceptions_during_sends'] def __init__(self): Plugin.__init__(self) self.register_default_primitives() self._break_on_exceptions_during_sends = Cell(False) @staticmethod def load_special_objects(space, language_name, process_cls, shadow_cls): process_cls.load_special_objects(process_cls, language_name, space) shadow_cls.load_special_objects(shadow_cls, language_name, space) # Abstract methods def is_operational(self): raise NotImplementedError @staticmethod def new_eval_process(space, args_w): raise NotImplementedError @staticmethod def new_send_process(space, w_rcvr, method_name, args_w, break_on_exceptions): raise NotImplementedError @staticmethod def w_object_class(): raise NotImplementedError @staticmethod def to_w_object(foreign_object): raise NotImplementedError @staticmethod def restart_specific_frame(space, args_w): raise NotImplementedError # Default primitives def register_default_primitives(self): @self.expose_primitive(result_is_new_frame=True) def eval(interp, s_frame, argcount): if not self.is_operational(): raise PrimitiveFailedError # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) language_process = self.new_eval_process(interp.space, args_w) # when we are here, the foreign language process has yielded frame = language_process.switch_to_smalltalk(interp, s_frame, first_call=True) s_frame.pop_n(argcount + 1) return frame @self.expose_primitive(unwrap_spec=[object], result_is_new_frame=True) def resume(interp, s_frame, language_process): # print 'Smalltalk yield' # import pdb; pdb.set_trace() if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError language_process.resume() if language_process.error_detected(): raise PrimitiveFailedError return language_process.switch_to_smalltalk(interp, s_frame) @self.expose_primitive(result_is_new_frame=True, compiled_method=True) def send(interp, s_frame, argcount, w_method): if not self.is_operational(): raise PrimitiveFailedError # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) w_rcvr = s_frame.peek(argcount) w_selector_name = w_method.literalat0(interp.space, 2) if not isinstance(w_selector_name, W_BytesObject): raise PrimitiveFailedError method_name = interp.space.unwrap_string(w_selector_name) idx = method_name.find(':') if idx > 0: method_name = method_name[0:idx] language_process = self.new_send_process( interp.space, w_rcvr, method_name, args_w, self._break_on_exceptions_during_sends.get()) frame = language_process.switch_to_smalltalk(interp, s_frame, first_call=True) s_frame.pop_n(argcount + 1) return frame @self.expose_primitive(unwrap_spec=[object]) def lastError(interp, s_frame, language_process): if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError w_error = language_process.get_error() if w_error is None: print 'w_error was None in lastError' raise PrimitiveFailedError return w_error @self.expose_primitive(unwrap_spec=[object]) def topFrame(interp, s_frame, language_process): if not isinstance(language_process, W_ForeignLanguageProcess): raise PrimitiveFailedError w_top_frame = language_process.w_top_frame() if w_top_frame is None: raise PrimitiveFailedError return w_top_frame @self.expose_primitive(unwrap_spec=[object]) def asSmalltalk(interp, s_frame, w_rcvr): if not isinstance(w_rcvr, self.w_object_class()): raise PrimitiveFailedError return self.to_w_object(interp.space, w_rcvr) @self.expose_primitive() def restartSpecificFrame(interp, s_frame, argcount): # import pdb; pdb.set_trace() args_w = s_frame.peek_n(argcount) result = self.restart_specific_frame(interp.space, args_w) s_frame.pop_n(argcount + 1) return interp.space.wrap_bool(result) @self.expose_primitive(unwrap_spec=[object, object]) def registerSpecificClass(interp, s_frame, w_rcvr, language_obj): if not isinstance(language_obj, W_ForeignLanguageObject): raise PrimitiveFailedError language_obj.class_shadow(interp.space).set_specific_class(w_rcvr) @self.expose_primitive(unwrap_spec=[object, bool]) def setBreakOnExceptionsDuringSends(interp, s_frame, w_rcvr, value): self._break_on_exceptions_during_sends.set(value) return interp.space.wrap_bool(value)
def __init__(self): Plugin.__init__(self) self.last_lookup = Cell(None)
class PythonPlugin(ForeignLanguagePlugin): language_name = 'Python' py_frame_restart_info = Cell(None, type=PyFrameRestartInfo) def is_optional(self): return True def is_enabled(self): if IMPORT_FAILED: return False return ForeignLanguagePlugin.is_enabled(self) def is_operational(self): return (W_PythonProcess.w_foreign_process_class.get() is not None and PythonClassShadow.w_foreign_class.get() is not None) def setup(self): translationconfig.set(thread=True) translationconfig.set(continuation=True) patch_pypy() @staticmethod def startup(space, argv): ForeignLanguagePlugin.load_special_objects( space, PythonPlugin.language_name, W_PythonProcess, PythonClassShadow) initialize_py_space(space, argv) py_space.startup() @staticmethod def new_eval_process(space, args_w): if len(args_w) != 4: raise PrimitiveFailedError source_w = args_w[0] filename_w = args_w[1] cmd_w = args_w[2] if (not isinstance(source_w, W_BytesObject) or not isinstance(filename_w, W_BytesObject) or not isinstance(cmd_w, W_BytesObject)): raise PrimitiveFailedError source = space.unwrap_string(source_w) filename = space.unwrap_string(filename_w) cmd = space.unwrap_string(cmd_w) break_on_exceptions = args_w[3] is space.w_true return W_PythonProcess( space, source=source, filename=filename, cmd=cmd, break_on_exceptions=break_on_exceptions) @staticmethod def new_send_process(space, w_rcvr, method_name, args_w, break_on_exceptions): return W_PythonProcess( space, is_send=True, w_rcvr=w_rcvr, method_name=method_name, args_w=args_w, break_on_exceptions=break_on_exceptions) @staticmethod def w_object_class(): return W_PythonObject @staticmethod def to_w_object(space, foreign_object): return utils.python_to_smalltalk(space, foreign_object.wp_object) @staticmethod def set_py_frame_restart_info(frame, py_code): PythonPlugin.py_frame_restart_info.set( PyFrameRestartInfo(frame, py_code)) @staticmethod def restart_specific_frame(space, args_w): frame_w = args_w[0] source_w = args_w[1] filename_w = args_w[2] cmd_w = args_w[3] if not (isinstance(frame_w, W_PythonObject) and isinstance(source_w, W_BytesObject) and isinstance(filename_w, W_BytesObject) and isinstance(cmd_w, W_BytesObject)): return False frame = frame_w.wp_object source = space.unwrap_string(source_w) filename = space.unwrap_string(filename_w) cmd = space.unwrap_string(cmd_w) py_code = None if source: py_code = utils.get_restart_pycode(source, filename, cmd) if py_code is None: return False PythonPlugin.set_py_frame_restart_info(frame, py_code) return True