Skip to content

Commit

Permalink
[FIX] yaml: Specify loader to YAML constructors
Browse files Browse the repository at this point in the history
pyYAML 5.1 is now the distro version. This introduces the FullLoader as
the default loader used with yaml.load(…), however, the default loader for
yaml.add_constructor(…) has not been updated (this is a known issue:
yaml/pyyaml#274).
This commit specifies the loader to be used when adding constructors,
using the FullLoader if it exists and the older Loader if not (i.e.
depending on which version of pyYAML is installed).

Task: 3958

Signed-off-by: Samuel Searles-Bryant <samuel.searles-bryant@unipart.io>
  • Loading branch information
Samuel Searles-Bryant committed Apr 26, 2019
2 parents 1effab8 + 78598bc commit c3550f2
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions odoo/tools/yaml_tag.py
Expand Up @@ -149,16 +149,24 @@ def ref_constructor(loader, tag_suffix, node):
# Constructors are actually defined globally: do not redefined them in another
# class/file/package. This means that module recorder need import this file.
def add_constructors():
yaml.add_constructor(u"!assert", assert_constructor)
yaml.add_constructor(u"!record", record_constructor)
yaml.add_constructor(u"!python", python_constructor)
yaml.add_constructor(u"!menuitem", menuitem_constructor)
yaml.add_constructor(u"!act_window", act_window_constructor)
yaml.add_constructor(u"!function", function_constructor)
yaml.add_constructor(u"!report", report_constructor)
yaml.add_constructor(u"!context", context_constructor)
yaml.add_constructor(u"!delete", delete_constructor)
yaml.add_constructor(u"!url", url_constructor)
yaml.add_constructor(u"!eval", eval_constructor)
yaml.add_multi_constructor(u"!ref", ref_constructor)
constructors = [
("!assert", assert_constructor),
("!record", record_constructor),
("!python", python_constructor),
("!menuitem", menuitem_constructor),
("!act_window", act_window_constructor),
("!function", function_constructor),
("!report", report_constructor),
("!context", context_constructor),
("!delete", delete_constructor),
("!url", url_constructor),
("!eval", eval_constructor),
("!ref", ref_constructor),
]
try:
loader = yaml.FullLoader # default for pyYAML>=5.1. Does not exist in earlier versions.
except AttributeError:
loader = yaml.Loader # default for pyYAML<5.1
for tag, constructor in constructors:
yaml.add_constructor(tag, constructor, Loader=loader)
add_constructors()

0 comments on commit c3550f2

Please sign in to comment.