Ticket #17 (closed defect: fixed)

Opened 8 years ago

Last modified 3 days ago

Single quote character can break emitter output

Reported by: apopheniac.reply.pyyaml@… Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:


If a string contains a space followed by a single quote character, PyYaml?'s emitter fails to duplicate the single quote. The resulting YAML output yields a parse error:

>>> map = {'key': " 'single quoted text'"}
>>> yaml.dump(map)
"{key: ' 'single quoted text'''}\n"
>>> yaml.load(yaml.dump(map))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/apopheniac/lib/python/yaml/", line 61, in load
    return loader.get_data()
  File "/home/apopheniac/lib/python/yaml/", line 41, in get_data
    return self.construct_document(self.get_node())
  File "/home/apopheniac/lib/python/yaml/", line 23, in get_node
    return self.compose_document()
  File "/home/apopheniac/lib/python/yaml/", line 40, in compose_document
    node = self.compose_node(None, None)
  File "/home/apopheniac/lib/python/yaml/", line 69, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/home/apopheniac/lib/python/yaml/", line 112, in compose_mapping_node
    while not self.check_event(MappingEndEvent):
  File "/home/apopheniac/lib/python/yaml/", line 78, in check_event
    self.current_event =
  File "/home/apopheniac/lib/python/yaml/", line 129, in parse_stream
    for event in self.parse_block_node():
  File "/home/apopheniac/lib/python/yaml/", line 323, in parse_node
    for event in collection_events:
  File "/home/apopheniac/lib/python/yaml/", line 472, in parse_flow_mapping
    "expected ',' or '}', but got %r" %, token.start_mark)
yaml.parser.ParserError: while scanning a flow mapping
  in "<string>", line 1, column 1:
    {key: ' 'single quoted text'''}
expected ',' or '}', but got '<scalar>'
  in "<string>", line 1, column 10:
    {key: ' 'single quoted text'''}

If each single quote is preceded by a non-space character, then the error disappears:

>>> map = {"key": " foo'single quoted text'"}
>>> yaml.dump(map)
"{key: ' foo''single quoted text'''}\n"
>>> yaml.load(yaml.dump(map))
{'key': " foo'single quoted text'"}

The error seems to lie in the write_single_quoted function of module I've only encountered this problem with scalars represented in single quoted style.


Change History

comment:1 Changed 8 years ago by xi

  • Status changed from new to closed
  • Resolution set to fixed

Thanks for the report. Fixed in [197].

comment:2 Changed 3 days ago by maskodok <galihadiputro87@…>

The only thing more I could hope for is documentation of all these features (other than reading through the code).  Cipto Junaedy Is this in process? Can I help? About  Unit Link Terbaik di Indonesia Commonwealth Life Investra Link


Add a comment

Modify Ticket

Change Properties
<Author field>
as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.