Skip to content
This repository has been archived by the owner on Jun 4, 2023. It is now read-only.

Commit

Permalink
Fixed type of some __copy__ methods to correctly deal with possible s…
Browse files Browse the repository at this point in the history
…ubclassing (Proxy). Tweaked handling of methods on the async proxy adapter. This also fixed the problems with current_context on async proxy calls. Fixes #90
  • Loading branch information
irmen committed Oct 4, 2015
1 parent 7ac825c commit 7d4ce31
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/source/tipstricks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ With a syntax that is slightly different from normal method calls,
it provides the same asynchronous function calls as the async proxy has.
Note that Python itself has a similar thing in the standard library since version 3.2, see
http://docs.python.org/3/library/concurrent.futures.html#future-objects . However Pyro's Future
object is available on older Python versions too, and works slightly differently. It's
also a little bit easier to work with.
object is available on older Python versions too. It works slightly differently and perhaps
a little bit easier as well.

You create a ``Future`` object for a callable that you want to execute in the background,
and receive its results somewhere in the future::
Expand Down
7 changes: 7 additions & 0 deletions examples/callcontext/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def _pyroResponseAnnotations(self, annotations, msgtype):
p._pyroRelease()

with CustomAnnotationProxy(uri) as proxy:
# async
print("Async proxy message...")
asyncproxy = Pyro4.async(proxy)
result = asyncproxy.echo("hello-ASYNC")
_ = result.value

# oneway
print("Finally, sending a oneway message...")
proxy.oneway("hello-ONEWAY")

Expand Down
11 changes: 9 additions & 2 deletions src/Pyro4/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def __setstate__(self, state):

def __copy__(self):
uriCopy = URI(self._pyroUri)
p = Proxy(uriCopy)
p = type(self)(uriCopy)
p._pyroOneway = set(self._pyroOneway)
p._pyroMethods = set(self._pyroMethods)
p._pyroAttrs = set(self._pyroAttrs)
Expand Down Expand Up @@ -664,7 +664,9 @@ def __exit__(self, *args):
pass

def __copy__(self):
return self
copy = type(self)(self.__proxy)
copy.__calls = list(self.__calls)
return copy

def __resultsgenerator(self, results):
for result in results:
Expand Down Expand Up @@ -696,8 +698,13 @@ def __init__(self, proxy):
self.__proxy = proxy

def __getattr__(self, name):
if name in dir(self.__proxy):
return getattr(self.__proxy, name)
return _AsyncRemoteMethod(self.__proxy, name)

def __copy__(self):
return type(self)(self.__proxy)


class _AsyncRemoteMethod(object):
"""async method call abstraction (call will run in a background thread)"""
Expand Down
26 changes: 26 additions & 0 deletions tests/PyroTests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ def testUriCopy(self):
self.assertEqual(p1.object, p3.object)
self.assertEqual(p1, p3)

def testUriSubclassCopy(self):
class SubURI(Pyro4.core.URI):
pass
u = SubURI("PYRO:12345@hostname:9999")
u2 = copy.copy(u)
self.assertIsInstance(u2, SubURI)

def testUriEqual(self):
p1 = Pyro4.core.URI("PYRO:[email protected]:9999")
p2 = Pyro4.core.URI("PYRO:[email protected]:9999")
Expand Down Expand Up @@ -305,6 +312,25 @@ def testProxyCopy(self):
self.assertEqual(p1._pyroHmacKey, p2._pyroHmacKey)
self.assertEqual(p1._pyroHandshake, p2._pyroHandshake)

def testProxySubclassCopy(self):
class ProxySub(Pyro4.core.Proxy):
pass
p = ProxySub("PYRO:12345@hostname:9999")
p2 = copy.copy(p)
self.assertIsInstance(p2, ProxySub)

def testAsyncProxyAdapterCopy(self):
proxy = Pyro4.core.Proxy("PYRO:12345@hostname:9999")
asyncproxy = proxy._pyroAsync()
p2 = copy.copy(asyncproxy)
self.assertIsInstance(p2, Pyro4.core._AsyncProxyAdapter)

def testBatchProxyAdapterCopy(self):
proxy = Pyro4.core.Proxy("PYRO:12345@hostname:9999")
batchproxy = proxy._pyroBatch()
p2 = copy.copy(batchproxy)
self.assertIsInstance(p2, Pyro4.core._BatchProxyAdapter)

def testProxyOffline(self):
# only offline stuff here.
# online stuff needs a running daemon, so we do that in another test, to keep this one simple
Expand Down

0 comments on commit 7d4ce31

Please sign in to comment.