aio-pika
aio-pika copied to clipboard
adding a Python3 fix to JsonRPC serializer
JsonRPC is throwing and error encoding error "string argument without an encoding"
without this fix.
It's probably a leftover from Python 2
Very strange fix. When your right that's not working right now, but tests says isn't this.
There were no tests for JsonRPC I think
It’s wrong
@cloud-rocket please give an example code which reproduces error.
I am working on tests and the following code reproduces the error:
def rpc_func(*, foo, bar):
assert not foo
assert not bar
return {"foo": "bar"}
async def test_jsonrpc_simple(self, channel: aio_pika.Channel):
rpc = await JsonRPC.create(channel, auto_delete=True)
await rpc.register("test.rpc", rpc_func, auto_delete=True)
result = await rpc.proxy.test.rpc(foo=None, bar=None)
assert result == {"foo": "bar"}
await rpc.unregister(rpc_func)
await rpc.close()
# Close already closed
await rpc.close()
This code works with my fix.
But my fix is not dealing with the case when an exception is thrown. So the fix is only partly solving the problem. Trying to find what's wrong with the exception serialization....
Everything fixed (including exception handling) and tests are added.
@mosquito, what do you think is wrong? The tests I added reproduce the problem in the original code and it works with the fix.
@decaz, what do you think?
Why this PR is ignored?
@cloud-rocket why have the fallen tests have been ignored?
@mosquito - fixed, sorry. Please run the workflow. Also added proper documentation here - https://github.com/mosquito/aio-pika/issues/425
@mosquito -
Are you open to consider this version of deserialize
?
def deserialize(self, data: Any) -> bytes:
res = RPC.deserialize(self, data)
if isinstance(res, dict) and "error" in res:
cls = locate(res['error']['type'])
if cls:
res = cls(res['error']['message'], res['error']['args'])
else:
res = JsonRPCError(res['error']['type'],
res['error']['message'],
res['error']['args'])
return res
I found myself anyway using it, because known exceptions are easier to handle and there is no vulnerability here as far as I understand.
@mosquito, if this PR is OK, would you merge it, because right now we have to make nasty tricks to make this functionality work.
@screech since bump aio-pika>7.0.0
, the JsonRPC
-related classes was refactored and now you may change theirs behaviour with inheritance.