Skip to content

Commit

Permalink
Client proxy handle RPC with no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mdavidsaver committed Sep 17, 2024
1 parent eb49a5d commit 4f6b9e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/p4p/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,28 @@ class RPCProxyBase(object):
scheme = None # set to override automatic


def _wrapMethod(K, V):
pv, req = V._call_PV, V._call_Request
def _wrapMethod(name, meth):
pv, req = meth._call_PV, meth._call_Request
if sys.version_info >= (3, 0):
S = inspect.getfullargspec(V)
S = inspect.getfullargspec(meth)
keywords = S.varkw
else:
S = inspect.getargspec(V)
S = inspect.getargspec(meth)
keywords = S.keywords
defaults = S.defaults or [] # for getfullargspec().defaults can be None

if S.varargs is not None or keywords is not None:
raise TypeError("vararg not supported for proxy method %s" % K)
raise TypeError("vararg not supported for proxy method %s" % name)

if len(S.args) != len(S.defaults):
raise TypeError("proxy method %s must specify types for all arguments" % K)
if len(S.args) != len(defaults):
raise TypeError("proxy method %s must specify types for all arguments" % name)

try:
NT = NTURI(zip(S.args, S.defaults))
NT = NTURI(zip(S.args, defaults))
except Exception as e:
raise TypeError("%s : failed to build method from %s, %s" % (e, S.args, S.defaults))
raise TypeError("%s : failed to build method from %s, %s" % (e, S.args, defaults))

@wraps(V)
@wraps(meth)
def mcall(self, *args, **kws):
pvname = pv % self.format
try:
Expand Down
27 changes: 27 additions & 0 deletions src/p4p/test/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class TestService(object):
def add(self, lhs, rhs):
return float(lhs) + float(rhs)

@rpc(NTScalar('d'))
def magicnum(self):
return 42

class TestRPCFull(RefTestCase):

Expand Down Expand Up @@ -110,6 +113,13 @@ def testAdd3(self):
sum = ctxt.rpc(self.prefix + 'add', args)
self.assertEqual(sum.value, 3.0)

def testMagic(self):
args = NTURI([
]).wrap(self.prefix + 'magicnum', kws={
}, scheme='pva')
with Context(self.provider, useenv=False, conf=self.getconfig(), unwrap=False) as ctxt:
num = ctxt.rpc(self.prefix + 'magicnum', args)
self.assertEqual(num.value, 42)

#class TestRPCProvider(TestRPCFull):
#
Expand Down Expand Up @@ -142,13 +152,30 @@ def bar(A='i', B='s'):
def another(X='s', Y='i'):
pass

@rpccall('%smagicnum')
def magicnum():
pass

def setUp(self):
super(TestProxy, self).setUp()
ctxt = self.MockContext()
self.proxy = self.MyProxy(myarg=3, context=ctxt, format='pv:')
self.assertEqual(self.proxy.myarg, 3)
self.assertIs(self.proxy.context, ctxt)

def test_call0(self):
args, kws = self.proxy.magicnum()

self.assertEqual(args[0], 'pv:magicnum')
self.assertListEqual(args[1].tolist(), [
('scheme', u'fake'),
('authority', u''),
('path', u'pv:magicnum'),
('query', [])
])
self.assertEqual(len(args), 2)
self.assertDictEqual(kws, {'request': None, 'throw': True, 'timeout': 3.0})

def test_call1(self):
args, kws = self.proxy.bar(4, 'one')

Expand Down

0 comments on commit 4f6b9e9

Please sign in to comment.