mbr / tinyrpc

A compact, modular transport and protocol agnostic RPC library. Does jsonrpc v2.
https://tinyrpc.readthedocs.org
MIT License
156 stars 53 forks source link

TypeError: bytes expected #57

Closed goohuu closed 6 years ago

goohuu commented 6 years ago

We have been using tinryrpc since version 0.6 . Our code (which is very similar to the http_server_example.py) fails in version 0.7 and above with the following traceback:

 Fri Jun  1 08:12:21 2018 <Greenlet at 0x7f011de5c2a8: handle_message(<Queue at 0x7f011de054e0 getters[1]>, b'{"jsonrpc": "2.0", "id": 1, "method": "get_activ)> failed with TypeError
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/gevent/greenlet.py", line 536, in run 
    result = self._run(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tinyrpc/server/__init__.py", line 90, in handle_message
    self.transport.send_reply(context, result)
  File "/usr/local/lib/python3.5/dist-packages/tinyrpc/transports/wsgi.py", line 52, in send_reply
    raise TypeError('bytes expected')
TypeError: bytes expected

We reproduced the above both in a Debian8 and a Debian9 environment. lib versions:

At your disposal for further tests or information

lnoor commented 6 years ago

Hi, Thank you for reporting. This only happens with python3 and is a consequence of the changed meaning of the str type in python3.

Quick fixes: change your code to send bytes instead of string or change line 51 of transport/wsgi.py to test for str instead of bytes. I realize neither option is very attractive.

I may have made the wrong choice doing it this way. Later this year, during my summer vacation, I plan to do some extensive work on tinyrpc. This will include a breaking change. With that in mind, do you care to make a recommendation on how to deal with strings vs bytes in tinyrpc?

regards, Leo

goohuu commented 6 years ago

Hi,

Thanks for your feedback. We will keep using 0.6 which suits our need for the time being. Unfortunately, we are not knowledgeable enough in Python to give proper recommendations on how to deal with strings vs bytes. What we will do however is to continue testing future tinyrpc versions. Don't hesitate to reach me if you need some testing/validation.

sojala commented 6 years ago

I have the same issue when using python3.

First tried to change my code to return bytes instead of string, but that results in another error:

File "/usr/lib/python3.6/json/encoder.py", line 180, in default o.__class__.__name__) TypeError: Object of type 'bytes' is not JSON serializable

Changing line 51 of transport/wsgi.py worked.

lnoor commented 6 years ago

Thank you for bringing this to my attention. I will soon start work on v1.0 which will be Python 3 only. With the changes that trouble you I tried to improve Python2/3 interoperability. I consider just reverting this.

lnoor commented 6 years ago

reverted to old behaviour and made available on pypi as 0.9.2