Skip to content
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

Use deepcopy instead of a shallow copy for middlewares, #2349 #2354

Merged
merged 2 commits into from
Aug 5, 2024
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: 2 additions & 1 deletion telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# random module to generate random string
import random
import string
import copy

import ssl

Expand Down Expand Up @@ -8749,7 +8750,7 @@ def _run_middlewares_and_handler(self, message, handlers, middlewares, update_ty
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
return
else:
data_copy = data.copy()
data_copy = copy.deepcopy(data)
for key in list(data_copy):
# remove data from data_copy if handler does not accept it
if key not in params:
Expand Down
4 changes: 2 additions & 2 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import string
import random
import ssl

import copy

"""
Module : telebot
Expand Down Expand Up @@ -558,7 +558,7 @@ async def _run_middlewares_and_handlers(self, message, handlers, middlewares, up
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
return
else:
data_copy = data.copy()
data_copy = copy.deepcopy(data)
Copy link

@Tishka17 Tishka17 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should no copy values inside data. It is required to pass original objects retrieved from middlewares to handlers. They can be mutated and it is expected behavior. That is how you pass objectes to handlers instead of global variables

for key in list(data_copy):
# remove data from data_copy if handler does not accept it
if key not in params:
Expand Down
Loading