summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--_test/test_issues.py23
-rw-r--r--main.py21
-rw-r--r--parser.py6
3 files changed, 49 insertions, 1 deletions
diff --git a/_test/test_issues.py b/_test/test_issues.py
index e73dc06..1793edb 100644
--- a/_test/test_issues.py
+++ b/_test/test_issues.py
@@ -37,3 +37,26 @@ class TestIssues:
""")
x = round_trip(s, block_seq_indent=4, preserve_quotes=True)
assert x['bar'] == 32
+
+ def test_issue_161(self):
+ yaml_str = dedent("""\
+ mapping-A:
+ key-A:{}
+ mapping-B:
+ """)
+ for comment in ['', ' # no-newline', ' # some comment\n', '\n', ]:
+ s = yaml_str.format(comment)
+ res = round_trip(s)
+
+ def test_issue_161a(self):
+ yaml_str = dedent("""\
+ mapping-A:
+ key-A:{}
+ mapping-B:
+ """)
+ for comment in ['\n# between']:
+ s = yaml_str.format(comment)
+ res = round_trip(s)
+ # print(s)
+ # print('------------')
+ # assert False
diff --git a/main.py b/main.py
index ce607bf..fa9db38 100644
--- a/main.py
+++ b/main.py
@@ -639,6 +639,27 @@ class YAML(object):
self.constructor.add_constructor(tag, f_y)
return cls
+
+ def parse(self, stream):
+ # type: (StreamTextType, Any) -> Any
+ """
+ Parse a YAML stream and produce parsing events.
+ """
+ _, parser = self.get_constructor_parser(stream)
+ try:
+ while parser.check_event():
+ yield parser.get_event()
+ finally:
+ parser.dispose()
+ try:
+ self._reader.reset_reader() # type: ignore
+ except AttributeError:
+ pass
+ try:
+ self._scanner.reset_scanner() # type: ignore
+ except AttributeError:
+ pass
+
# ### context manager
def __enter__(self):
diff --git a/parser.py b/parser.py
index 9c6041d..0707761 100644
--- a/parser.py
+++ b/parser.py
@@ -598,7 +598,11 @@ class Parser(object):
self.state = self.parse_block_mapping_key
comment = token.comment
if comment is None:
- comment = self.scanner.peek_token().comment
+ token = self.scanner.peek_token()
+ comment = token.comment
+ if comment:
+ token._comment = [None, comment[1]]
+ comment = [comment[0], None]
return self.process_empty_scalar(token.end_mark, comment=comment)
else:
self.state = self.parse_block_mapping_key