Fix CallbackQueryHandler Not Handling Non-String Data Correctly With Regex Patterns (#3252)

This commit is contained in:
Bibo-Joshi 2022-09-22 20:30:30 +02:00 committed by GitHub
parent fdfbcdf51e
commit c6721a799d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -130,6 +130,7 @@ class CallbackQueryHandler(BaseHandler[Update, CCT]):
:obj:`bool`
"""
# pylint: disable=too-many-return-statements
if isinstance(update, Update) and update.callback_query:
callback_data = update.callback_query.data
if self.pattern:
@ -139,6 +140,8 @@ class CallbackQueryHandler(BaseHandler[Update, CCT]):
return isinstance(callback_data, self.pattern)
if callable(self.pattern):
return self.pattern(callback_data)
if not isinstance(callback_data, str):
return False
match = re.match(self.pattern, callback_data)
if match:
return match

View file

@ -32,7 +32,7 @@ from telegram import (
Update,
User,
)
from telegram.ext import CallbackContext, CallbackQueryHandler, JobQueue
from telegram.ext import CallbackContext, CallbackQueryHandler, InvalidCallbackData, JobQueue
message = Message(1, None, Chat(1, ""), from_user=User(1, "", False), text="Text")
@ -136,6 +136,11 @@ class TestCallbackQueryHandler:
callback_query.callback_query.game_short_name = "this is a short game name"
assert not handler.check_update(callback_query)
callback_query.callback_query.data = object()
assert not handler.check_update(callback_query)
callback_query.callback_query.data = InvalidCallbackData()
assert not handler.check_update(callback_query)
def test_with_callable_pattern(self, callback_query):
class CallbackData:
pass