LTD-Beget / LTD-Beget/beget_msgpack

msgpack и нестандартные типы

Open
#8 1 comment 0 reactions 1 assignee View on GitHub

@ConConovaloff is already working on this.

Since Jan 30, 2015.

bug
Dominant language
Python
Stars
1
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Если мы пакуем в msgpack не стандартный класс, то получаем ошибку.
Когда это происходит в ответе от msgpack сервера, то вызов повисает

Надо на выходе из Action рекурсивно проверять возвращаемый тип, и если он не входит в белый список, то выбрасывать исключение

https://github.com/msgpack-rpc/msgpack-rpc-python/blob/8cb1eebb65024294b2558dac1a0bb36652926a04/msgpackrpc/server.py

```
class AsyncResult:
def set_result(self, value, error=None):
if self._responder is not None:
self._responder.set_result(value, error) <----
else:
self._result = [value, error]
```

Воспроизведение ошибки:

```
Python 2.7.8 (default, Oct 30 2014, 17:51:12)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import msgpack
>>> packer = msgpack.Packer(default=lambda x: x.to_msgpack())
>>> packer.pack( {'some': 'wow'} )
'\x81\xa4some\xa3wow'
>>>
>>> packer.pack( 3 )
'\x03'
>>>
>>> from decimal import *
>>>
>>> Decimal(1)
Decimal('1')
>>> e = Decimal(1)
>>> packer.pack( e )
Traceback (most recent call last):
File "", line 1, in
File "msgpack/_packer.pyx", line 223, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:223)
File "msgpack/_packer.pyx", line 225, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:225)
File "msgpack/_packer.pyx", line 216, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:216)
File "", line 1, in
AttributeError: 'Decimal' object has no attribute 'to_msgpack'
>>>
>>> type(e)

>>> type(3)

>>>

>>> con = MySQLdb.connect(...)
>>> curs = con.cursor(MySQLdb.cursors.DictCursor)
>>> curs.execute('SELECT 0.01 AS test')
1L
>>> result = curs.fetchall()
>>> type(result[0]['test'])

```

Трасировка

```

Traceback (most recent call last):
File "/opt/python/pythons/Python-2.7.8/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()

File "/opt/python/pythons/Python-2.7.8/lib/python2.7/site-packages/beget_msgpack/controller.py", line 62, in run
self._result.set_result(self._response.dump()) <---

File "/opt/python/pythons/Python-2.7.8/lib/python2.7/site-packages/msgpackrpc/server.py", line 67, in set_result
self._responder.set_result(value, error)

File "/opt/python/pythons/Python-2.7.8/lib/python2.7/site-packages/msgpackrpc/server.py", line 89, in set_result
self._sendable.send_message([message.RESPONSE, self._msgid, error, value])

File "/opt/python/pythons/Python-2.7.8/lib/python2.7/site-packages/msgpackrpc/transport/tcp.py", line 19, in send_message
self._stream.write(self._packer.pack(message), callback=callback)

File "msgpack/_packer.pyx", line 223, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:223)
File "msgpack/_packer.pyx", line 225, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:225)
File "msgpack/_packer.pyx", line 213, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:213)
File "msgpack/_packer.pyx", line 184, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:184)
File "msgpack/_packer.pyx", line 184, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:184)
File "msgpack/_packer.pyx", line 213, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:213)
File "msgpack/_packer.pyx", line 184, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:184)
File "msgpack/_packer.pyx", line 216, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:216)

File "/opt/python/pythons/Python-2.7.8/lib/python2.7/site-packages/msgpackrpc/transport/tcp.py", line 12, in
self._packer = msgpack.Packer(encoding=encodings[0], default=lambda x: x.to_msgpack())

AttributeError: 'Decimal' object has no attribute 'to_msgpack'
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.