New PyYAML version breaks on most custom python objects - RepresenterError

偶尔善良 提交于 2019-12-01 23:42:47

dump is now safe_dump, which won't handle arbitrary objects:

>>> yaml.dump is yaml.safe_dump
True

Use danger_dump for the old behaviour.

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

The same goes for load/safe_load. Can't find the docs or release notes for 4.1.0, I only found out by digging through the commits (here).

Is there a clear reason for why PyYAML no longer supports these object types?

Yes. yaml.load was allowing arbitrary code execution, and such a dangerous feature should be opt-in only - not possible to use by accident. Arguably, it should have been this way from the beginning, and I'm glad the new maintainers of PyYAML have corrected that.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!