eternnoir / pyTelegramBotAPI

Python Telegram bot api.
GNU General Public License v2.0
8.12k stars 2.03k forks source link

Deepcopy of handlers brings "cannot serialize"/"can't pickle" errors #559

Closed Badiboy closed 6 years ago

Badiboy commented 6 years ago

1. What version of pyTelegramBotAPI are you using? 3.6.5 2. What OS are you using? Win10 / Ubuntu 14.x 3. What version of python are you using? 3.5.3

When migrated from version 3.6.3 to 3.6.5 I got the following error:

2018-08-13 23:18:02,280 (__init__.py:313 PollingThread) DEBUG - TeleBot: "Received 1 new updates"
2018-08-13 23:18:02,298 (util.py:65 PollingThread) ERROR - TeleBot: "TypeError occurred, args=("cannot serialize '_io.TextIOWrapper' object",)
Traceback (most recent call last):
  File "C:\Python3\lib\site-packages\telebot\util.py", line 59, in run
    task(*args, **kwargs)
  File "C:\Python3\lib\site-packages\telebot\__init__.py", line 278, in __retrieve_updates
    self.process_new_updates(updates)
  File "C:\Python3\lib\site-packages\telebot\__init__.py", line 315, in process_new_updates
    self.process_new_messages(new_messages)
  File "C:\Python3\lib\site-packages\telebot\__init__.py", line 334, in process_new_messages
    self._notify_next_handlers(new_messages)
  File "C:\Python3\lib\site-packages\telebot\__init__.py", line 1291, in _notify_next_handlers
    handlers = copy.deepcopy(self.next_step_handlers[chat_id])
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 218, in _deepcopy_list
    y.append(deepcopy(a, memo))
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 250, in _deepcopy_method
    return type(x)(x.__func__, deepcopy(x.__self__, memo))
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 218, in _deepcopy_list
    y.append(deepcopy(a, memo))
  File "C:\Python3\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python3\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python3\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python3\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python3\lib\copy.py", line 174, in deepcopy
    rv = reductor(4)
TypeError: cannot serialize '_io.TextIOWrapper' object

The same error for thread.lock.

2018-08-13 23:07:34,291 (__init__.py:313 PollingThread) DEBUG - TeleBot: "Received 1 new updates"
2018-08-13 23:07:34,305 (util.py:65 PollingThread) ERROR - TeleBot: "TypeError occurred, args=("can't pickle _thread.lock objects",)
Traceback (most recent call last):
  File "C:\Python3\lib\site-packages\telebot\util.py", line 59, in run
    task(*args, **kwargs)
...
TypeError: can't pickle _thread.lock objects

The problem is strange for me. I use the thread.lock (the only place) in the method used in some message processing methods (including some used in register_next_step ) in the next way:

def processing_method
  call static_class_method(my_user_class)
    my_user_class.locker.acquire()
    ...
    my_user_class.locker.release()

And for '_io.TextIOWrapper' I have no ideas where it happens.

Badiboy commented 6 years ago

PS: I know that deepcopy appears in commit https://github.com/eternnoir/pyTelegramBotAPI/commit/494b535a9127c769401791c8acb7a1d1443560a0 deveoped by @Andru1999 . So I kindly ask him to come to this thread.

Badiboy commented 6 years ago

I found the patch to this here: https://github.com/eternnoir/pyTelegramBotAPI/commit/a46975d0383c012aae1bc294b047a39059761a77 I think it should work now but cannot try until release...