mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 15:17:00 +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``,
|
:attr:`REPLACED_BOT`. Currently, this handles objects of type ``list``, ``tuple``, ``set``,
|
||||||
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
||||||
``__slots__`` attribute, excluding classes and objects that can't be copied with
|
``__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:
|
Args:
|
||||||
obj (:obj:`object`): The object
|
obj (:obj:`object`): The object
|
||||||
|
@ -278,20 +279,30 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
||||||
return new_obj
|
return new_obj
|
||||||
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
||||||
# __dict__ case comes below
|
# __dict__ case comes below
|
||||||
if hasattr(obj, '__slots__'):
|
try:
|
||||||
for attr_name in new_obj.__slots__:
|
if hasattr(obj, '__slots__'):
|
||||||
setattr(
|
for attr_name in new_obj.__slots__:
|
||||||
new_obj,
|
setattr(
|
||||||
attr_name,
|
new_obj,
|
||||||
cls._replace_bot(cls._replace_bot(getattr(new_obj, attr_name), memo), memo),
|
attr_name,
|
||||||
)
|
cls._replace_bot(cls._replace_bot(getattr(new_obj, attr_name), memo), memo),
|
||||||
memo[obj_id] = new_obj
|
)
|
||||||
return new_obj
|
memo[obj_id] = new_obj
|
||||||
if hasattr(obj, '__dict__'):
|
return new_obj
|
||||||
for attr_name, attr in new_obj.__dict__.items():
|
if hasattr(obj, '__dict__'):
|
||||||
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
|
for attr_name, attr in new_obj.__dict__.items():
|
||||||
memo[obj_id] = new_obj
|
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
|
||||||
return new_obj
|
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
|
return obj
|
||||||
|
|
||||||
|
@ -301,7 +312,8 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
||||||
:attr:`bot`. Currently, this handles objects of type ``list``, ``tuple``, ``set``,
|
:attr:`bot`. Currently, this handles objects of type ``list``, ``tuple``, ``set``,
|
||||||
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
``frozenset``, ``dict``, ``defaultdict`` and objects that have a ``__dict__`` or
|
||||||
``__slots__`` attribute, excluding classes and objects that can't be copied with
|
``__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:
|
Args:
|
||||||
obj (:obj:`object`): The object
|
obj (:obj:`object`): The object
|
||||||
|
@ -368,20 +380,30 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
||||||
return new_obj
|
return new_obj
|
||||||
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
|
||||||
# __dict__ case comes below
|
# __dict__ case comes below
|
||||||
if hasattr(obj, '__slots__'):
|
try:
|
||||||
for attr_name in obj.__slots__:
|
if hasattr(obj, '__slots__'):
|
||||||
setattr(
|
for attr_name in obj.__slots__:
|
||||||
new_obj,
|
setattr(
|
||||||
attr_name,
|
new_obj,
|
||||||
self._insert_bot(self._insert_bot(getattr(new_obj, attr_name), memo), memo),
|
attr_name,
|
||||||
)
|
self._insert_bot(self._insert_bot(getattr(new_obj, attr_name), memo), memo),
|
||||||
memo[obj_id] = new_obj
|
)
|
||||||
return new_obj
|
memo[obj_id] = new_obj
|
||||||
if hasattr(obj, '__dict__'):
|
return new_obj
|
||||||
for attr_name, attr in new_obj.__dict__.items():
|
if hasattr(obj, '__dict__'):
|
||||||
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
|
for attr_name, attr in new_obj.__dict__.items():
|
||||||
memo[obj_id] = new_obj
|
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
|
||||||
return new_obj
|
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
|
return obj
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue