def _sendExceptionResponse(self, connection, seq, serializer_id, exc_value, tbinfo): """send an exception back including the local traceback info""" exc_value._pyroTraceback = tbinfo if sys.platform == "cli": util.fixIronPythonExceptionForPickle(exc_value, True) # piggyback attributes serializer = util.get_serializer_by_id(serializer_id) try: data, compressed = serializer.serializeData(exc_value) except: # the exception object couldn't be serialized, use a generic PyroError instead xt, xv, tb = sys.exc_info() msg = "Error serializing exception: %s. Original exception: %s: %s" % ( str(xv), type(exc_value), str(exc_value)) exc_value = errors.PyroError(msg) exc_value._pyroTraceback = tbinfo if sys.platform == "cli": util.fixIronPythonExceptionForPickle( exc_value, True) # piggyback attributes data, compressed = serializer.serializeData(exc_value) flags = Pyro4.message.FLAGS_EXCEPTION if compressed: flags |= Pyro4.message.FLAGS_COMPRESSED if Pyro4.config.LOGWIRE: log.debug( "daemon wiredata sending (error response): msgtype=%d flags=0x%x ser=%d seq=%d data=%r" % (Pyro4.message.MSG_RESULT, flags, serializer.serializer_id, seq, data)) msg = Message(Pyro4.message.MSG_RESULT, data, serializer.serializer_id, flags, seq) connection.send(msg.to_bytes())
def __init__(self, host=None, port=0, unixsocket=None, nathost=None, natport=None): _check_hmac() # check if hmac secret key is set if host is None: host = Pyro4.config.HOST if nathost is None: nathost = Pyro4.config.NATHOST if natport is None: natport = Pyro4.config.NATPORT or None if nathost and unixsocket: raise ValueError("cannot use nathost together with unixsocket") if (nathost is None) ^ (natport is None): raise ValueError("must provide natport with nathost") if Pyro4.config.SERVERTYPE == "thread": self.transportServer = SocketServer_Threadpool() elif Pyro4.config.SERVERTYPE == "multiplex": # choose the 'best' multiplexing implementation if os.name == "java": raise NotImplementedError( "select or poll-based server is not supported for jython, use thread server instead" ) self.transportServer = SocketServer_Poll( ) if socketutil.hasPoll else SocketServer_Select() else: raise errors.PyroError("invalid server type '%s'" % Pyro4.config.SERVERTYPE) self.transportServer.init(self, host, port, unixsocket) #: The location (str of the form ``host:portnumber``) on which the Daemon is listening self.locationStr = self.transportServer.locationStr log.debug("created daemon on %s", self.locationStr) natport_for_loc = natport if natport == 0: # expose internal port number as NAT port as well. (don't use port because it could be 0 and will be chosen by the OS) natport_for_loc = int(self.locationStr.split(":")[1]) #: The NAT-location (str of the form ``nathost:natportnumber``) on which the Daemon is exposed for use with NAT-routing self.natLocationStr = "%s:%d" % (nathost, natport_for_loc) if nathost else None if self.natLocationStr: log.debug("NAT address is %s", self.natLocationStr) pyroObject = DaemonObject(self) pyroObject._pyroId = constants.DAEMON_NAME #: Dictionary from Pyro object id to the actual Pyro object registered by this id self.objectsById = {pyroObject._pyroId: pyroObject} self.__mustshutdown = threadutil.Event() self.__loopstopped = threadutil.Event() self.__loopstopped.set() # assert that the configured serializers are available, and remember their ids: self.__serializer_ids = set([ util.get_serializer(ser_name).serializer_id for ser_name in Pyro4.config.SERIALIZERS_ACCEPTED ]) log.debug("accepted serializers: %s" % Pyro4.config.SERIALIZERS_ACCEPTED)
def _parseLocation(self, location, defaultPort): if not location: return if location.startswith("./u:"): self.sockname=location[4:] if (not self.sockname) or ':' in self.sockname: raise errors.PyroError("invalid uri (location)") else: if location.startswith("["): # ipv6 if location.startswith("[["): # possible mistake: double-bracketing raise errors.PyroError("invalid ipv6 address: enclosed in too many brackets") self.host, _, self.port = re.match(r"\[([0-9a-fA-F:%]+)](:(\d+))?", location).groups() else: self.host, _, self.port = location.partition(":") if not self.port: self.port=defaultPort try: self.port=int(self.port) except (ValueError, TypeError): raise errors.PyroError("invalid port in uri, port="+str(self.port))
def __init__(self, uri): if isinstance(uri, URI): state = uri.__getstate__() self.__setstate__(state) return if not isinstance(uri, basestring): raise TypeError("uri parameter object is of wrong type") self.sockname = self.host = self.port = None match = self.uriRegEx.match(uri) if not match: raise errors.PyroError("invalid uri") self.protocol = match.group("protocol").upper() self.object = match.group("object") location = match.group("location") if self.protocol == "PYRONAME": self._parseLocation(location, Pyro4.config.NS_PORT) return if self.protocol == "PYRO": if not location: raise errors.PyroError("invalid uri") self._parseLocation(location, None) else: raise errors.PyroError("invalid uri (protocol)")
def on_future_completion(f): try: client_future.set_result(f.result()) except cfutures.CancelledError: client_future.set_cancelled() except Exception as ex: try: client_future.set_exception(ex) except: # exception cannot be sent => simplify logging.info("Failed to send full exception, will send summary") msg = "Exception %s %s (Error serializing exception)" % (type(ex), str(ex)) exc_value = errors.PyroError(msg) client_future.set_exception(exc_value) finally: del self._uriToFuture[uri] # that should be the only ref, so kill connection
def _sendExceptionResponse(self, connection, seq, exc_value, tbinfo): """send an exception back including the local traceback info""" exc_value._pyroTraceback=tbinfo if sys.platform=="cli": util.fixIronPythonExceptionForPickle(exc_value, True) # piggyback attributes try: data, _=self.serializer.serialize(exc_value) except: # the exception object couldn't be serialized, use a generic PyroError instead xt, xv, tb = sys.exc_info() msg = "Error serializing exception: %s. Original exception: %s: %s" % (str(xv), type(exc_value), str(exc_value)) exc_value = errors.PyroError(msg) exc_value._pyroTraceback=tbinfo if sys.platform=="cli": util.fixIronPythonExceptionForPickle(exc_value, True) # piggyback attributes data, _=self.serializer.serialize(exc_value) msg=MessageFactory.createMessage(MessageFactory.MSG_RESULT, data, MessageFactory.FLAGS_EXCEPTION, seq) del data connection.send(msg)
class _BatchProxyAdapter(object): """Helper class that lets you batch multiple method calls into one. It is constructed with a reference to the normal proxy that will carry out the batched calls. Call methods on this object thatyou want to batch, and finally call the batch proxy itself. That call will return a generator for the results of every method call in the batch (in sequence).""" def __init__(self, proxy): self.__proxy = proxy self.__calls = [] def __getattr__(self, name): return _BatchedRemoteMethod(self.__calls, name) def __enter__(self): return self def __exit__(self, *args): pass def __copy__(self): return self def __resultsgenerator(self, results): for result in results: if isinstance(result, futures._ExceptionWrapper): result.raiseIt() # re-raise the remote exception locally. else: yield result # it is a regular result object, yield that and continue. def __call__(self, oneway=False, async=False): if oneway and async: raise errors.PyroError("async oneway calls make no sense") if async: return _AsyncRemoteMethod(self, "<asyncbatch>")() else: results = self.__proxy._pyroInvokeBatch(self.__calls, oneway) self.__calls = [] # clear for re-use if not oneway: return self.__resultsgenerator(results)
def events(self, eventsockets): raise errors.PyroError( "cannot combine events when using user-supplied connected socket")
def combine_loop(self, server): raise errors.PyroError( "cannot combine servers when using user-supplied connected socket")
def __enter__(self): if not self.transportServer: raise errors.PyroError("cannot reuse this object") return self
def _check_hmac(): if Pyro4.config.HMAC_KEY: if sys.version_info >= (3, 0) and type( Pyro4.config.HMAC_KEY) is not bytes: raise errors.PyroError("HMAC_KEY must be bytes type")
def _check_hmac(): if Pyro4.config.HMAC_KEY is None or len(Pyro4.config.HMAC_KEY)==0: import warnings # warnings.warn("HMAC_KEY not set, protocol data may not be secure") elif sys.version_info>=(3,0) and type(Pyro4.config.HMAC_KEY) is not bytes: raise errors.PyroError("HMAC_KEY must be bytes type")