Skip to content

Commit

Permalink
[FIX] yaml: fix default loader bug in pyYAML 5.1
Browse files Browse the repository at this point in the history
pyYAML 5.1 is now the distro version. This introduces the requirement to
specify a loader when loading yaml. There is a known issue ( yaml/pyyaml#274 )
with the add_constructor method not assignign the correct default
loader. This commit assigns the default FullLoader, allowing the
constructors to be used again.

Task: 3761

Signed-off-by: Samuel Searles-Bryant <[email protected]>
  • Loading branch information
Samuel Searles-Bryant committed Apr 4, 2019
1 parent 1effab8 commit e4fdfbf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion odoo/tools/yaml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def process(self, yaml_string):
yaml_tag.add_constructors()

is_preceded_by_comment = False
for node in yaml.load(yaml_string):
for node in yaml.load(yaml_string, Loader=yaml.FullLoader):
is_preceded_by_comment = self._log_node(node, is_preceded_by_comment)
try:
self._process_node(node)
Expand Down
34 changes: 17 additions & 17 deletions odoo/tools/yaml_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, model, id=None, severity=logging.WARNING, string="NONAME", **
self.severity = severity
self.string = string
super(Assert, self).__init__(**kwargs)

class Record(YamlTag):
def __init__(self, model, id, use='id', view=True, **kwargs):
self.model = model
Expand Down Expand Up @@ -83,14 +83,14 @@ def __init__(self, expression):
super(Eval, self).__init__()
def __str__(self):
return '!eval %s' % str(self.expression)

class Ref(YamlTag):
def __init__(self, expr="False", *args, **kwargs):
self.expr = expr
super(Ref, self).__init__(*args, **kwargs)
def __str__(self):
return 'ref(%s)' % repr(self.expr)

def assert_constructor(loader, node):
kwargs = loader.construct_mapping(node)
return Assert(**kwargs)
Expand Down Expand Up @@ -137,28 +137,28 @@ def url_constructor(loader, node):
def eval_constructor(loader, node):
expression = loader.construct_scalar(node)
return Eval(expression)

def ref_constructor(loader, tag_suffix, node):
if tag_suffix == "id":
kwargs = {"id": loader.construct_scalar(node)}
else:
kwargs = loader.construct_mapping(node)
return Ref(**kwargs)

# Registers constructors for custom tags.
# 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)
yaml.add_constructor(u"!assert", assert_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!record", record_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!python", python_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!menuitem", menuitem_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!act_window", act_window_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!function", function_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!report", report_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!context", context_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!delete", delete_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!url", url_constructor, Loader=yaml.FullLoader)
yaml.add_constructor(u"!eval", eval_constructor, Loader=yaml.FullLoader)
yaml.add_multi_constructor(u"!ref", ref_constructor, Loader=yaml.FullLoader)
add_constructors()

0 comments on commit e4fdfbf

Please sign in to comment.