irmen / Pyrolite

Java and .NET client interface for Pyro5 protocol
MIT License
178 stars 47 forks source link

Json serializer customer Lua #15

Closed alexsilva closed 9 years ago

alexsilva commented 9 years ago

I am developing a client on the Lua 3.2 programming language. As will be used in a legacy project, the Lua version being used is 3.2

By defining a simple API like this

class Math(object):
    def sum(self, a, b):
        return a + b

When calling the API client in the Lua

local px = proxy('PYRO:obj_5d871322110a4062b25d7a21bc34ae8e@localhost:59249')

px:start_connection()

local a, b = 2, 3
local result = px.sum(2, 3)

if type(result) == 'number' then
    println(format("%d + %d = %d", a, b, result))
else
    tprint(result)
end

Using json serializer, I have to pass parameters of type 'kwargs' for remote function. Otherwise I get an error

exceptions.KeyError
...
self.loadsCall(data)
4: File "C:\Python27\lib\site-packages\Pyro4\util.py", line 494, in loadsCall kwargs = self.recreate_classes(data["kwargs"])
5: KeyError: 'kwargs'
__exception__: true
args: {
n: 1
1: kwargs
}

When calling the api with any parameter: https://github.com/alexsilva/Pyrolite/blob/lua-dev/lua32/core.lua#L55

local data = serializer:dumps({
        object= self.uri.objectid,
        method = methodname,
        params = args,
        kwargs = { x =1 }
    })

I get a new error:

__exception__: true
args: {
n: 1
1: sum() got an unexpected keyword argument 'x'
}

You can see that there is a problem in json serializer here: https://github.com/irmen/Pyro4/blob/master/src/Pyro4/util.py#L494

kwargs = self.recreate_classes(data["kwargs"])

This could be written this way:

if  "kwargs" in data:
    kwargs = self.recreate_classes(data["kwargs"])
else:
   kwargs = {}

Because even without declaring 'kwargs' in function, is valid for the python:

>>> def x():
...     pass
...     
>>> d = {}
>>> x(**d)
alexsilva commented 9 years ago

The problem is not related to this project, but the Pyro4