mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 07:06:26 +01:00
Catch exceptions raised while copying __dict__/__slots__ in BasePersistence.replace/insert_bot()
Also updated the docstrings to reflect the changes in behavior with unexpected errors
This commit is contained in:
parent
52ce03929b
commit
6c61c4bac4
1 changed files with 52 additions and 30 deletions
|
@ -212,7 +212,8 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
|||
:attr:`REPLACED_BOT`. Currently, this handles objects of type ``list``, ``tuple``, ``set``,
|
||||
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
||||
``__slots__`` attribute, excluding classes and objects that can't be copied with
|
||||
``copy.copy``.
|
||||
``copy.copy``. If the parsing of an object fails, the object will be returned unchanged and the
|
||||
error will be logged.
|
||||
|
||||
Args:
|
||||
obj (:obj:`object`): The object
|
||||
|
@ -278,20 +279,30 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
|||
return new_obj
|
||||
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
||||
# __dict__ case comes below
|
||||
if hasattr(obj, '__slots__'):
|
||||
for attr_name in new_obj.__slots__:
|
||||
setattr(
|
||||
new_obj,
|
||||
attr_name,
|
||||
cls._replace_bot(cls._replace_bot(getattr(new_obj, attr_name), memo), memo),
|
||||
)
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
if hasattr(obj, '__dict__'):
|
||||
for attr_name, attr in new_obj.__dict__.items():
|
||||
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
try:
|
||||
if hasattr(obj, '__slots__'):
|
||||
for attr_name in new_obj.__slots__:
|
||||
setattr(
|
||||
new_obj,
|
||||
attr_name,
|
||||
cls._replace_bot(cls._replace_bot(getattr(new_obj, attr_name), memo), memo),
|
||||
)
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
if hasattr(obj, '__dict__'):
|
||||
for attr_name, attr in new_obj.__dict__.items():
|
||||
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
except Exception as e:
|
||||
exception_description = str(e)
|
||||
warnings.warn(
|
||||
f'Parsing of an object failed with the following exception: {exception_description}. '
|
||||
f'See the docs of BasePersistence.insert_bot for more information.',
|
||||
RuntimeWarning,
|
||||
)
|
||||
memo[obj_id] = obj
|
||||
return obj
|
||||
|
||||
return obj
|
||||
|
||||
|
@ -301,7 +312,8 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
|||
:attr:`bot`. Currently, this handles objects of type ``list``, ``tuple``, ``set``,
|
||||
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
||||
``__slots__`` attribute, excluding classes and objects that can't be copied with
|
||||
``copy.copy``.
|
||||
``copy.copy``. If the parsing of an object fails, the object will be returned unchanged and the
|
||||
error will be logged.
|
||||
|
||||
Args:
|
||||
obj (:obj:`object`): The object
|
||||
|
@ -368,20 +380,30 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
|||
return new_obj
|
||||
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
||||
# __dict__ case comes below
|
||||
if hasattr(obj, '__slots__'):
|
||||
for attr_name in obj.__slots__:
|
||||
setattr(
|
||||
new_obj,
|
||||
attr_name,
|
||||
self._insert_bot(self._insert_bot(getattr(new_obj, attr_name), memo), memo),
|
||||
)
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
if hasattr(obj, '__dict__'):
|
||||
for attr_name, attr in new_obj.__dict__.items():
|
||||
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
try:
|
||||
if hasattr(obj, '__slots__'):
|
||||
for attr_name in obj.__slots__:
|
||||
setattr(
|
||||
new_obj,
|
||||
attr_name,
|
||||
self._insert_bot(self._insert_bot(getattr(new_obj, attr_name), memo), memo),
|
||||
)
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
if hasattr(obj, '__dict__'):
|
||||
for attr_name, attr in new_obj.__dict__.items():
|
||||
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
|
||||
memo[obj_id] = new_obj
|
||||
return new_obj
|
||||
except Exception as e:
|
||||
exception_description = str(e)
|
||||
warnings.warn(
|
||||
f'Parsing of an object failed with the following exception: {exception_description}. '
|
||||
f'See the docs of BasePersistence.insert_bot for more information.',
|
||||
RuntimeWarning,
|
||||
)
|
||||
memo[obj_id] = obj
|
||||
return obj
|
||||
|
||||
return obj
|
||||
|
||||
|
|
Loading…
Reference in a new issue