Menu

#275 Fails to parse flow mapping values starting with a colon

closed
nobody
None
minor
bug
2020-01-23
2020-01-23
No
In [1]: from ruamel.yaml import YAML

In [2]: yaml = YAML()

In [3]: yaml.load("{x: :=}")
---------------------------------------------------------------------------
ParserError                               Traceback (most recent call last)


ParserError: while parsing a flow node
expected the node content, but found ':'
  in "<unicode string>", line 1, column 5:
    {x: :=}
        ^ (line: 1)
Full traceback
ParserError                               Traceback (most recent call last)
<ipython-input-3-e4ed3ab4a388> in <module>
----> 1 yaml.load("{x: :=}")

/usr/lib/python3.7/site-packages/ruamel/yaml/main.py in load(self, stream)
    329         constructor, parser = self.get_constructor_parser(stream)
    330         try:
--> 331             return constructor.get_single_data()
    332         finally:
    333             parser.dispose()

/usr/lib/python3.7/site-packages/ruamel/yaml/constructor.py in get_single_data(self)
    105         # type: () -> Any
    106         # Ensure that the stream contains a single document and construct it.
--> 107         node = self.composer.get_single_node()
    108         if node is not None:
    109             return self.construct_document(node)

/usr/lib/python3.7/site-packages/ruamel/yaml/composer.py in get_single_node(self)
     76         document = None  # type: Any
     77         if not self.parser.check_event(StreamEndEvent):
---> 78             document = self.compose_document()
     79
     80         # Ensure that the stream contains no more documents.

/usr/lib/python3.7/site-packages/ruamel/yaml/composer.py in compose_document(self)
     99
    100         # Compose the root node.
--> 101         node = self.compose_node(None, None)
    102
    103         # Drop the DOCUMENT-END event.

/usr/lib/python3.7/site-packages/ruamel/yaml/composer.py in compose_node(self, parent, index)
    136             node = self.compose_sequence_node(anchor)
    137         elif self.parser.check_event(MappingStartEvent):
--> 138             node = self.compose_mapping_node(anchor)
    139         self.resolver.ascend_resolver()
    140         return node

/usr/lib/python3.7/site-packages/ruamel/yaml/composer.py in compose_mapping_node(self, anchor)
    216             #             start_event.start_mark,
    217             #             "found duplicate key", key_event.start_mark)
--> 218             item_value = self.compose_node(node, item_key)
    219             # node.value[item_key] = item_value
    220             node.value.append((item_key, item_value))

/usr/lib/python3.7/site-packages/ruamel/yaml/composer.py in compose_node(self, parent, index)
    109     def compose_node(self, parent, index):
    110         # type: (Any, Any) -> Any
--> 111         if self.parser.check_event(AliasEvent):
    112             event = self.parser.get_event()
    113             alias = event.anchor

/usr/lib/python3.7/site-packages/ruamel/yaml/parser.py in check_event(self, *choices)
    139         if self.current_event is None:
    140             if self.state:
--> 141                 self.current_event = self.state()
    142         if self.current_event is not None:
    143             if not choices:

/usr/lib/python3.7/site-packages/ruamel/yaml/parser.py in parse_flow_mapping_value(self)
    744             if not self.scanner.check_token(FlowEntryToken, FlowMappingEndToken):
    745                 self.states.append(self.parse_flow_mapping_key)
--> 746                 return self.parse_flow_node()
    747             else:
    748                 self.state = self.parse_flow_mapping_key

/usr/lib/python3.7/site-packages/ruamel/yaml/parser.py in parse_flow_node(self)
    323     def parse_flow_node(self):
    324         # type: () -> Any
--> 325         return self.parse_node()
    326
    327     def parse_block_node_or_indentless_sequence(self):

/usr/lib/python3.7/site-packages/ruamel/yaml/parser.py in parse_node(self, block, indentless_sequence)
    483                 start_mark,
    484                 'expected the node content, but found %r' % token.id,
--> 485                 token.start_mark,
    486             )
    487         return event

ParserError: while parsing a flow node
expected the node content, but found ':'
  in "<unicode string>", line 1, column 5:
    {x: :=}
        ^ (line: 1)

It works for block mappings and flow collections:

In [6]: yaml.load("x: :=")
Out[6]: ordereddict([('x', ':=')])

In [7]: yaml.load("[:=]")
Out[7]: [':=']

According to the ns-plain-first rule, which is also used for flow mapping values, this is valid, since the colon is followed by a safe char (=).

First noticed in https://github.com/sublimehq/Packages/pull/1828.

(Looks like the details tag isn't supported on bitbucket.)

(originally posted on 2019-01-08 at 14:06:34 by FichteFoll <FichteFoll@bitbucket>)

Discussion

  • Anthon van der Neut

    None
    (originally posted on 2019-01-08 at 14:08:02 by FichteFoll <FichteFoll@bitbucket>)

     
  • Anthon van der Neut

    None
    (originally posted on 2019-01-08 at 14:08:24 by FichteFoll <FichteFoll@bitbucket>)

     
  • Anthon van der Neut

    I'll try to fix this. I already did handle some (try loading '[:=]' PyYAML), but it is rather difficult to do this correctly without ripping everything apart (and breaking things).

    ruamel.yaml is based on PyYAML and that only had to deal with YAML 1.1 which is much more easy to parse as the value indicator (:) is always followed by a space. In YAML 1.2 this is of course not the case, so you need to check on all kinds of context in the scanner.

    (originally posted on 2019-01-08 at 16:43:26)

     
  • Anthon van der Neut

    • status set to resolved

    Should be fixed in 0.15.86, if so please close this issue

    (originally posted on 2019-01-08 at 17:45:36)

     
  • Anthon van der Neut

    Confirmed fixed on master.

    (originally posted on 2019-01-09 at 01:11:57 by FichteFoll <FichteFoll@bitbucket>)

     
  • Anthon van der Neut

    • status set to closed

    (originally posted on 2019-01-09 at 01:12:08 by FichteFoll <FichteFoll@bitbucket>)

     
  • Anthon van der Neut

    Isn't "resolved" better than closed? I don't really use bitbucket or Jira.

    (originally posted on 2019-01-09 at 01:13:08 by FichteFoll <FichteFoll@bitbucket>)

     

Log in to post a comment.

MongoDB Logo MongoDB