diff options
| author | Amir Karimi <amk9978@gmail.com> | 2024-09-12 10:56:18 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-09-17 11:00:49 +0200 |
| commit | ee5147cfd7de2add74a285537a8968ec074e70cd (patch) | |
| tree | 64129a4de2722fb78dc486c83656f9d907aab296 /django/core/serializers/pyyaml.py | |
| parent | a060a22ee2dde7aa29a5a29120087c4864887325 (diff) | |
Fixed #29522 -- Refactored the Deserializer functions to classes.
Co-authored-by: Emad Mokhtar <emad.mokhtar@veneficus.nl>
Diffstat (limited to 'django/core/serializers/pyyaml.py')
| -rw-r--r-- | django/core/serializers/pyyaml.py | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index 9a20b6658f..ed6e4b3895 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -6,7 +6,6 @@ Requires PyYaml (https://pyyaml.org/), but that's checked for in __init__. import collections import decimal -from io import StringIO import yaml @@ -66,17 +65,23 @@ class Serializer(PythonSerializer): return super(PythonSerializer, self).getvalue() -def Deserializer(stream_or_string, **options): +class Deserializer(PythonDeserializer): """Deserialize a stream or string of YAML data.""" - if isinstance(stream_or_string, bytes): - stream_or_string = stream_or_string.decode() - if isinstance(stream_or_string, str): - stream = StringIO(stream_or_string) - else: + + def __init__(self, stream_or_string, **options): stream = stream_or_string - try: - yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options) - except (GeneratorExit, DeserializationError): - raise - except Exception as exc: - raise DeserializationError() from exc + if isinstance(stream_or_string, bytes): + stream = stream_or_string.decode() + try: + objects = yaml.load(stream, Loader=SafeLoader) + except Exception as exc: + raise DeserializationError() from exc + super().__init__(objects, **options) + + def _handle_object(self, obj): + try: + yield from super()._handle_object(obj) + except (GeneratorExit, DeserializationError): + raise + except Exception as exc: + raise DeserializationError(f"Error deserializing object: {exc}") from exc |
