<small id='JBgin'></small><noframes id='JBgin'>

    • <bdo id='JBgin'></bdo><ul id='JBgin'></ul>
    <tfoot id='JBgin'></tfoot>
  1. <i id='JBgin'><tr id='JBgin'><dt id='JBgin'><q id='JBgin'><span id='JBgin'><b id='JBgin'><form id='JBgin'><ins id='JBgin'></ins><ul id='JBgin'></ul><sub id='JBgin'></sub></form><legend id='JBgin'></legend><bdo id='JBgin'><pre id='JBgin'><center id='JBgin'></center></pre></bdo></b><th id='JBgin'></th></span></q></dt></tr></i><div id='JBgin'><tfoot id='JBgin'></tfoot><dl id='JBgin'><fieldset id='JBgin'></fieldset></dl></div>
    <legend id='JBgin'><style id='JBgin'><dir id='JBgin'><q id='JBgin'></q></dir></style></legend>

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

      时间:2023-09-13
        <bdo id='I2WMw'></bdo><ul id='I2WMw'></ul>

          <small id='I2WMw'></small><noframes id='I2WMw'>

        1. <legend id='I2WMw'><style id='I2WMw'><dir id='I2WMw'><q id='I2WMw'></q></dir></style></legend>

              <tbody id='I2WMw'></tbody>
              <i id='I2WMw'><tr id='I2WMw'><dt id='I2WMw'><q id='I2WMw'><span id='I2WMw'><b id='I2WMw'><form id='I2WMw'><ins id='I2WMw'></ins><ul id='I2WMw'></ul><sub id='I2WMw'></sub></form><legend id='I2WMw'></legend><bdo id='I2WMw'><pre id='I2WMw'><center id='I2WMw'></center></pre></bdo></b><th id='I2WMw'></th></span></q></dt></tr></i><div id='I2WMw'><tfoot id='I2WMw'></tfoot><dl id='I2WMw'><fieldset id='I2WMw'></fieldset></dl></div>

                <tfoot id='I2WMw'></tfoot>
                本文介绍了大多数自定义 python 对象的新 PyYAML 版本中断 - RepresenterError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                About 5 hours ago, version 4.1.0 was released. It is breaking my unit tests. Here is a clean MVCE displaying this:

                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
                - !!python/object/apply:numpy.dtype
                  args: [i8, 0, 1]
                  state: !!python/tuple [3, <, null, null, null, -1, -1, 0]
                - !!binary |
                  AgAAAAAAAAA=
                '
                

                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)
                

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

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

                推荐答案

                在 PyYAML 4.x 中,dumpsafe_dump 的别名,不会处理任意对象:

                In PyYAML 4.x, dump is an alias for safe_dump, which won't handle arbitrary objects:

                >>> yaml.dump is yaml.safe_dump
                True
                

                对旧的 3.x 行为使用 danger_dump.

                Use danger_dump for the old 3.x behaviour.

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

                load/safe_load 也是如此.找不到 4.1.0 的任何文档或发行说明,我只是通过挖掘提交才发现的 (这里).

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

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

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

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

                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.

                在当前的 PyYAML 5.x 中:您可以指定 loader/dumper 类作为参数,而不是使用不同的函数:

                In current PyYAML 5.x: instead of using different functions, you can specify the loader/dumper class as an argument:

                yaml.dump(x, Dumper=yaml.Dumper)      # like "danger dump"
                yaml.dump(x, Dumper=yaml.SafeDumper)  # like "safe_dump", won't dump python objs
                

                与 3.x 一样,危险"dump 仍然是 5.x 中的默认值:

                As with 3.x, the "danger" dump is still the default in 5.x:

                >>> yaml.dump(sys)
                "!!python/module:sys ''
                "
                >>> yaml.dump(sys, Dumper=yaml.SafeDumper)
                RepresenterError: ('cannot represent an object', <module 'sys' (built-in)>)
                

                这篇关于大多数自定义 python 对象的新 PyYAML 版本中断 - RepresenterError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 Python 中使用 ruamel.yaml 从 YAML 文件中获取 下一篇:在 ruamel.yaml 迭代期间获取评论

                相关文章

                最新文章

                1. <small id='dstEp'></small><noframes id='dstEp'>

                  <tfoot id='dstEp'></tfoot>

                    <bdo id='dstEp'></bdo><ul id='dstEp'></ul>
                  <legend id='dstEp'><style id='dstEp'><dir id='dstEp'><q id='dstEp'></q></dir></style></legend>
                  <i id='dstEp'><tr id='dstEp'><dt id='dstEp'><q id='dstEp'><span id='dstEp'><b id='dstEp'><form id='dstEp'><ins id='dstEp'></ins><ul id='dstEp'></ul><sub id='dstEp'></sub></form><legend id='dstEp'></legend><bdo id='dstEp'><pre id='dstEp'><center id='dstEp'></center></pre></bdo></b><th id='dstEp'></th></span></q></dt></tr></i><div id='dstEp'><tfoot id='dstEp'></tfoot><dl id='dstEp'><fieldset id='dstEp'></fieldset></dl></div>