def issubclass(cls, bases): """a version of issubclass that supports NetProxies""" if not orig_isinstance(bases, tuple): bases = (bases,) # is cls a proxy? if orig_isinstance(cls, NetProxy): return _remote_issubclass(cls, bases) # is one of the bases a proxy? for base in bases: if orig_isinstance(base, NetProxy): return _remote_issubclass(cls, bases) # plain old issubclass return orig_issubclass(cls, bases)
def issubclass(cls, bases): """a version of issubclass that supports NetProxies""" if not orig_isinstance(bases, tuple): bases = (bases, ) # is cls a proxy? if orig_isinstance(cls, NetProxy): return _remote_issubclass(cls, bases) # is one of the bases a proxy? for base in bases: if orig_isinstance(base, NetProxy): return _remote_issubclass(cls, bases) # plain old issubclass return orig_issubclass(cls, bases)
def dir(*obj): """a version of dir() that supports NetProxies""" if not obj: return sorted(inspect.stack()[1][0].f_locals.keys()) if not len(obj) == 1: raise TypeError("dir expected at most 1 arguments, got %d" % (len(obj),)) obj = obj[0] if orig_isinstance(obj, NetProxy): return _get_conn(obj).modules.__builtin__.dir(obj) else: return orig_dir(obj)
def __call__(self, obj = None): if orig_isinstance(obj, NetProxy): print "Help on NetProxy object for an instance of %r:" % (obj.__getattr__("__class__").__name__,) print print "Doc:" print obj.__getattr__("__doc__") print print "Members:" print dir(obj) else: orig_help(obj)
def dir(*obj): """a version of dir() that supports NetProxies""" if not obj: return sorted(inspect.stack()[1][0].f_locals.keys()) if not len(obj) == 1: raise TypeError("dir expected at most 1 arguments, got %d" % (len(obj), )) obj = obj[0] if orig_isinstance(obj, NetProxy): return _get_conn(obj).modules.__builtin__.dir(obj) else: return orig_dir(obj)
def __call__(self, obj=None): if orig_isinstance(obj, NetProxy): print "Help on NetProxy object for an instance of %r:" % ( obj.__getattr__("__class__").__name__, ) print print "Doc:" print obj.__getattr__("__doc__") print print "Members:" print dir(obj) else: orig_help(obj)
def getattr(obj, name, *default): """a version of getattr() that supports NetProxies""" if len(default) > 1: raise TypeError("getattr expected at most 3 arguments, got %d" % (2 + len(default),)) if orig_isinstance(obj, NetProxy): try: return obj.__getattr__(name) except AttributeError: if not default: raise return default[0] else: return orig_getattr(obj, name, *default)
def _get_fullname(cls): """ a heuristic to generate a unique identifier for classes, that is not machine-, platform-, or runtime-dependent """ if orig_isinstance(cls, NetProxy): modules = _get_conn(cls).modules.sys.modules else: modules = sys.modules try: filename = modules[cls.__module__].__file__ except (KeyError, AttributeError): filename = cls.__module__ return (filename, cls.__name__)
def getattr(obj, name, *default): """a version of getattr() that supports NetProxies""" if len(default) > 1: raise TypeError("getattr expected at most 3 arguments, got %d" % (2 + len(default), )) if orig_isinstance(obj, NetProxy): try: return obj.__getattr__(name) except AttributeError: if not default: raise return default[0] else: return orig_getattr(obj, name, *default)
def deliver(obj, conn): """ delivers a local object to the other side of the connection. the object can be a function or any picklable object. deliver objects creates a remote copy of the objectm so changes made to the remote copy are not reflected on the local one. keep this in mind. obj - the object to deliver conn - the connection which obtains the object returns a proxy to the delivered object """ if isproxy(obj): raise TypeError("can't deliver proxies") if orig_isinstance(obj, function): globals = conn.remote_conn._local_namespace dumped = _dump_function(obj) return conn.modules[__name__]._load_function(dumped, globals) else: return conn.modules.cPickle.loads(pickle.dumps(obj, pickle.HIGHEST_PROTOCOL))
def reload(module): """a version of reload() that supports NetProxies""" if orig_isinstance(module, NetProxy): return _get_conn(module).modules.__builtin__.reload(module) else: return orig_reload(module)
def isproxy(obj): """indicates whether the given object is a NetProxy""" return orig_isinstance(obj, NetProxy)