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:
zeroone2numeral2 2021-06-17 11:46:44 +02:00
parent 52ce03929b
commit 6c61c4bac4

View file

@ -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