新的 PyYAML 版本在大多数自定义 python 对象上中断 - RepresenterError

One*_*Day 4 python yaml pyyaml

大约 5 小时前,版本4.1.0发布。它打破了我的单元测试。这是一个干净的 MVCE 显示:

3.12 版:

>>> import numpy as np
>>> import yaml
>>> x = np.int64(2)
>>> yaml.dump(x, Dumper=yaml.Dumper)
'!!python/object/apply:numpy.core.multiarray.scalar\n- !!python/object/apply:numpy.dtype\n  args: [i8, 0, 1]\n  state: !!python/tuple [3, <, null, null, null, -1, -1, 0]\n- !!binary |\n  AgAAAAAAAAA=\n'
Run Code Online (Sandbox Code Playgroud)

4.1.0 版

>>> import numpy as np
>>> import yaml
>>> x = np.int64(2)
>>> yaml.dump(x, Dumper=yaml.Dumper)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/foo/anaconda3/envs/bar/lib/python3.6/site-packages/yaml/__init__.py", line 217, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/foo/anaconda3/envs/bar/lib/python3.6/site-packages/yaml/__init__.py", line 196, in dump_all
    dumper.represent(data)
  File "/foo/anaconda3/envs/bar/lib/python3.6/site-packages/yaml/representer.py", line 26, in represent
    node = self.represent_data(data)
  File "/foo/anaconda3/envs/bar/lib/python3.6/site-packages/yaml/representer.py", line 57, in represent_data
    node = self.yaml_representers[None](self, data)
  File "/foo/anaconda3/envs/bar/lib/python3.6/site-packages/yaml/representer.py", line 229, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', 2)
Run Code Online (Sandbox Code Playgroud)

为什么PyYAML不再支持这些对象类型有明确的原因吗?

wim*_*wim 5

在 PyYAML 4.x 中,dump是 的别名safe_dump,它不会处理任意对象:

>>> yaml.dump is yaml.safe_dump
True
Run Code Online (Sandbox Code Playgroud)

使用danger_dump旧3.x的行为。

>>> yaml.danger_dump(x)
'!!python/object/apply:numpy.core.multiarray.scalar\n- !!python/object/apply:numpy.dtype\n  args: [i8, 0, 1]\n  state: !!python/tuple [3, <, null, null, null, -1, -1, 0]\n- !!binary |\n  AgAAAAAAAAA=\n'
Run Code Online (Sandbox Code Playgroud)

这同样适用于load/ safe_load。找不到 4.1.0 的任何文档或发行说明,我只是通过挖掘提交才发现的(这里)。

为什么 PyYAML 不再支持这些对象类型有明确的原因吗?

是的。yaml.load允许任意代码执行,这样一个危险的功能应该只选择加入,不能意外使用。可以说,从一开始就应该是这样。