提问人:Nathan Tew 提问时间:5/14/2023 更新时间:5/14/2023 访问量:478
当我的 python-telegram-bot 执行run_polling时抛出的错误未记录到控制台
Errors thrown when my python-telegram-bot is doing run_polling are not logged to console
问:
我正在尝试测试我的电报机器人使用.问题是,在我这样做之后,此后引发的任何错误都只会导致控制台在这里冻结:python-telegram-bot
unittest
updater.run_polling()
EWARNING:telegram.ext.Application:Fetching updates got a asyncio.CancelledError. Ignoring as this task may onlybe closed via `Application.stop`.
DEBUG:telegram.ext.Updater:Network loop retry getting Updates was cancelled
在我执行 Ctrl + C 之后,它会显示此回溯,但未提及实际错误:
EWARNING:telegram.ext.Application:Fetching updates got a asyncio.CancelledError. Ignoring as this task may onlybe closed via `Application.stop`.
DEBUG:telegram.ext.Updater:Network loop retry getting Updates was cancelled
^CDEBUG:asyncio:Close <_UnixSelectorEventLoop running=False closed=False debug=True>
Traceback (most recent call last):
File "/Users/nathan/development/python/ptbtest/examples/test_my.py", line 32, in <module>
unittest.main()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/main.py", line 102, in __init__
self.runTests()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/main.py", line 274, in runTests
self.result = testRunner.run(self.test)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/runner.py", line 217, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 122, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 122, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/case.py", line 678, in __call__
return self.run(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/async_case.py", line 133, in run
self._tearDownAsyncioRunner()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/async_case.py", line 126, in _tearDownAsyncioRunner
runner.close()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/runners.py", line 71, in close
_cancel_all_tasks(loop)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/runners.py", line 201, in _cancel_all_tasks
loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True))
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
self.run_forever()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
self._run_once()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 1881, in _run_once
event_list = self._selector.select(timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/selectors.py", line 561, in select
kev_list = self._selector.control(None, max_ev, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
ERROR:asyncio:Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last):
File "/Users/nathan/development/python/ptbtest/examples/test_my.py", line 32, in <module>
unittest.main()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/main.py", line 102, in __init__
self.runTests()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/main.py", line 274, in runTests
self.result = testRunner.run(self.test)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/runner.py", line 217, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 122, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/suite.py", line 122, in run
test(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/case.py", line 678, in __call__
return self.run(*args, **kwds)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/async_case.py", line 131, in run
return super().run(result)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/case.py", line 623, in run
self._callTestMethod(testMethod)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/async_case.py", line 90, in _callTestMethod
if self._callMaybeAsync(method) is not None:
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/unittest/async_case.py", line 112, in _callMaybeAsync
return self._asyncioRunner.run(
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 640, in run_until_complete
self.run_forever()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
self._run_once()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 1911, in _run_once
handle._run()
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/Users/nathan/development/python/ptbtest/examples/test_my.py", line 23, in test
await application.start()
File "/Users/nathan/.pyenv/versions/venvptbtest-3.11.1/lib/python3.11/site-packages/telegram/ext/_application.py", line 566, in start
self.__update_fetcher_task = asyncio.create_task(
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/tasks.py", line 374, in create_task
task = loop.create_task(coro)
task: <Task cancelling name='Task-6' coro=<Application._update_fetcher() running at /Users/nathan/.pyenv/versions/venvptbtest-3.11.1/lib/python3.11/site-packages/telegram/ext/_application.py:1050> wait_for=<Future pending cb=[Task.task_wakeup()] created at /Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py:427> cb=[gather.<locals>._done_callback() at /Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/tasks.py:754] created at /Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/tasks.py:374>
Exception ignored in: <coroutine object Application._update_fetcher at 0x10e9b0c80>
Traceback (most recent call last):
File "/Users/nathan/.pyenv/versions/venvptbtest-3.11.1/lib/python3.11/site-packages/telegram/ext/_application.py", line 1050, in _update_fetcher
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/queues.py", line 160, in get
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 761, in call_soon
File "/Users/nathan/.pyenv/versions/3.11.1/lib/python3.11/asyncio/base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closed
如何让它向我显示错误?我尝试添加错误处理程序,但无济于事(它不打印任何内容):
def error_handler(update, context):
print('ERROR ERROR ERROR ERROR ERROR ERROR ERROR')
print(context)
...
application.add_error_handler(error_handler)
我的完整代码:
from __future__ import absolute_import
import unittest
import pytest
from telegram.ext import Application
import logging
def error_handler(update, context):
print('ERROR ERROR ERROR ERROR ERROR ERROR ERROR')
print(context)
class TestConversationbot2(unittest.IsolatedAsyncioTestCase):
@pytest.mark.asyncio
async def test(self):
logging.basicConfig(level=logging.DEBUG)
application = (
Application
.builder()
.token('MY_TELEGRAM_BOT_TOKEN')
.build()
)
application.add_error_handler(error_handler)
await application.initialize()
await application.start()
await application.updater.start_polling()
raise BaseException('my error')
await application.updater.stop()
await application.stop()
await application.shutdown()
return None
if __name__ == '__main__':
unittest.main()
答: 暂无答案
评论