Skip to content

Commit

Permalink
Switch YAML formatter to safe_dump and safe_load
Browse files Browse the repository at this point in the history
This ensures that people only use this default formatter
for data structures that we can trust.
  • Loading branch information
timj committed Nov 4, 2020
1 parent 80d5751 commit 8e9ebec
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions python/lsst/daf/butler/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _readFile(self, path: str, pytype: Type[Any] = None) -> Any:
Notes
-----
The `~yaml.UnsafeLoader` is used when parsing the YAML file.
The `~yaml.SafeLoader` is used when parsing the YAML file.
"""
try:
with open(path, "rb") as fd:
Expand All @@ -90,8 +90,12 @@ def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = Non
inMemoryDataset : `object`
The requested data as an object, or None if the string could
not be read.
Notes
-----
The `~yaml.SafeLoader` is used when parsing the YAML.
"""
data = yaml.load(serializedDataset, Loader=yaml.FullLoader)
data = yaml.safe_load(serializedDataset)

try:
data = data.exportAsDict()
Expand All @@ -115,6 +119,12 @@ def _writeFile(self, inMemoryDataset: Any) -> None:
------
Exception
The file could not be written.
Notes
-----
The `~yaml.SafeDumper` is used when generating the YAML serialization.
This will fail for data structures that have complex python classes
without a registered YAML representer.
"""
with open(self.fileDescriptor.location.path, "wb") as fd:
fd.write(self._toBytes(inMemoryDataset))
Expand All @@ -140,10 +150,16 @@ def _toBytes(self, inMemoryDataset: Any) -> bytes:
------
Exception
The object could not be serialized.
Notes
-----
The `~yaml.SafeDumper` is used when generating the YAML serialization.
This will fail for data structures that have complex python classes
without a registered YAML representer.
"""
if hasattr(inMemoryDataset, "_asdict"):
inMemoryDataset = inMemoryDataset._asdict()
return yaml.dump(inMemoryDataset).encode()
return yaml.safe_dump(inMemoryDataset).encode()

def _coerceType(self, inMemoryDataset: Any, storageClass: StorageClass,
pytype: Optional[Type[Any]] = None) -> Any:
Expand Down

0 comments on commit 8e9ebec

Please sign in to comment.