Re: Are decorators supported?

Éric Piel <[email protected]> Wed, 12 Feb 2014 16:06:17 +0100
Newsgroups gmane.comp.python.pyro
Message-ID <[email protected]>
On 11/02/14 22:25, Richard van Bemmelen wrote:
> I am trying to add a function to an instance of a Pyro4 object, like so:
> 
> 
> 
> def addto(instance):
>     def decorator(f):
>         import types
>         f = types.MethodType(f, instance, instance.__class__)
>         setattr(instance, f.func_name, f)
>         return f
>     return decorator
> 
> cs = Pyro4.Proxy("PYRONAME:example.cserv")
> 
> @addto(cs)
> def getInit(self):
>     print self.initCsd
>     return self.initCsd
>  
> 
> print getInit()
> 
> This code is running in a client.
> It should print the content of self.initCsd on the server and on the client.
> Instead, I get this on the client (nothing on the server):
> 
> <Pyro4.core._RemoteMethod object at 0x105523dd0>
> <Pyro4.core._RemoteMethod object at 0x105523dd0>
> 
> So, is this supported?
Hi,
What you are doing is not supported. It's not related to decorators
though. What Pyro doesn't support is 1) adding an attribute to an object
remotely accessed (i.e., a Proxy), and 2) remote content access. In your
code, what happens is that the method is correctly added to the proxy,
and then the call displays .initCsd, which is a RemoteMethod, like
everything on a Proxy.

If you were doing the addto() on the server side, it would then work:
both the client and the server sides would see the new method, and the
execution would happen on the server so it would be able to access
.initCsd . I doubt there is any nice way to do what you want (adding
code to a remote object).

Best,
Éric


------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk