Commits

Kirill Simonov committed 567d807

Emit an explicit document end indicator when there is a possibility of ambiguous parsing.

Comments (0)

Files changed (1)

lib/yaml/emitter.py

         self.whitespace = True
         self.indention = True
 
+        # Whether the document requires an explicit document indicator
+        self.open_ended = False
+
         # Formatting details.
         self.canonical = canonical
         self.allow_unicode = allow_unicode
 
     def expect_document_start(self, first=False):
         if isinstance(self.event, DocumentStartEvent):
+            if (self.event.version or self.event.tags) and self.open_ended:
+                self.write_indicator(u'...', True)
+                self.write_indent()
             if self.event.version:
                 version_text = self.prepare_version(self.event.version)
                 self.write_version_directive(version_text)
                     self.write_indent()
             self.state = self.expect_document_root
         elif isinstance(self.event, StreamEndEvent):
+            if self.open_ended:
+                self.write_indicator(u'...', True)
+                self.write_indent()
             self.write_stream_end()
             self.state = self.expect_nothing
         else:
         self.whitespace = whitespace
         self.indention = self.indention and indention
         self.column += len(data)
+        self.open_ended = False
         if self.encoding:
             data = data.encode(self.encoding)
         self.stream.write(data)
     def write_folded(self, text):
         hints = self.determine_block_hints(text)
         self.write_indicator(u'>'+hints, True)
+        if hints[-1:] == u'+':
+            self.open_ended = True
         self.write_line_break()
         leading_space = True
         spaces = False
             end += 1
 
     def write_literal(self, text):
-        chomp = self.determine_block_hints(text)
-        self.write_indicator(u'|'+chomp, True)
+        hints = self.determine_block_hints(text)
+        self.write_indicator(u'|'+hints, True)
+        if hints[-1:] == u'+':
+            self.open_ended = True
         self.write_line_break()
         breaks = True
         start = end = 0
             end += 1
 
     def write_plain(self, text, split=True):
+        if self.root_context:
+            self.open_ended = True
         if not text:
             return
         if not self.whitespace: