Skip to content

Handle non-string data correctly with regex pattern in CQH #3252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions telegram/ext/_callbackqueryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def check_update(self, update: object) -> Optional[Union[bool, object]]:
: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:
Expand All @@ -139,6 +140,8 @@ def check_update(self, update: object) -> Optional[Union[bool, object]]:
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
Expand Down
7 changes: 6 additions & 1 deletion tests/test_callbackqueryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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")

Expand Down Expand Up @@ -136,6 +136,11 @@ def test_with_pattern(self, callback_query):
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
Expand Down