summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-09-04 14:01:20 +0200
committerAnthon van der Neut <anthon@mnt.org>2016-09-04 14:01:20 +0200
commitd8662dcf14f3a2a7ee1035830144edf6d94dc1bc (patch)
tree82e24ce1cba3948c291ff7d51f22b744476fd04c
parent3f7dc0c35e67f1435ba708921f88f075db824034 (diff)
downloadruamel.yaml-d8662dcf14f3a2a7ee1035830144edf6d94dc1bc.tar.gz
fix issue 54: space on empty lines break roundtrip0.12.7
-rw-r--r--README.rst3
-rw-r--r--__init__.py2
-rw-r--r--_test/test_comments.py22
-rw-r--r--scanner.py27
4 files changed, 51 insertions, 3 deletions
diff --git a/README.rst b/README.rst
index 49799c6..57071da 100644
--- a/README.rst
+++ b/README.rst
@@ -18,6 +18,9 @@ ChangeLog
::
+ 0.12.7 (2016-09-03):
+ - fixing issue 54 empty lines with spaces (reported by Alex Harvey)
+
0.12.6 (2016-09-03):
- fixing issue 46 empty lines between top-level keys were gobbled (but
not between sequence elements, nor between keys in netsted mappings
diff --git a/__init__.py b/__init__.py
index e880d11..ff4b33c 100644
--- a/__init__.py
+++ b/__init__.py
@@ -9,7 +9,7 @@ from __future__ import absolute_import
_package_data = dict(
full_package_name="ruamel.yaml",
- version_info=(0, 12, 6),
+ version_info=(0, 12, 7),
author="Anthon van der Neut",
author_email="a.van.der.neut@ruamel.eu",
description="ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order", # NOQA
diff --git a/_test/test_comments.py b/_test/test_comments.py
index 4bdaed1..63895cc 100644
--- a/_test/test_comments.py
+++ b/_test/test_comments.py
@@ -569,3 +569,25 @@ class TestEmptyLines:
- 3
""")
+
+ def test_line_with_only_spaces(self):
+ # issue 54
+ yaml_str = "---\n\na: 'x'\n \nb: y\n"
+ d = round_trip_load(yaml_str, preserve_quotes=True)
+ y = round_trip_dump(d, explicit_start=True)
+ stripped = ""
+ for line in yaml_str.splitlines():
+ stripped += line.rstrip() + '\n'
+ print(line + '$')
+ assert stripped == y
+
+ def test_some_eol_spaces(self):
+ # spaces after tokens and on empty lines
+ yaml_str = '--- \n \na: "x" \n \nb: y \n'
+ d = round_trip_load(yaml_str, preserve_quotes=True)
+ y = round_trip_dump(d, explicit_start=True)
+ stripped = ""
+ for line in yaml_str.splitlines():
+ stripped += line.rstrip() + '\n'
+ print(line + '$')
+ assert stripped == y
diff --git a/scanner.py b/scanner.py
index 5d50e27..ab78d51 100644
--- a/scanner.py
+++ b/scanner.py
@@ -1643,6 +1643,7 @@ class RoundTripScanner(Scanner):
self.allow_simple_key = True
return comment, start_mark, end_mark
if self.scan_line_break():
+ start_mark = self.get_mark()
if not self.flow_level:
self.allow_simple_key = True
ch = self.peek()
@@ -1650,14 +1651,36 @@ class RoundTripScanner(Scanner):
start_mark = self.get_mark()
comment = ''
while ch:
- ch = self.scan_line_break()
+ ch = self.scan_line_break(empty_line=True)
comment += ch
- # print('ch', repr(comment))
end_mark = self.get_mark()
return comment, start_mark, end_mark
else:
found = True
+ def scan_line_break(self, empty_line=False):
+ # Transforms:
+ # '\r\n' : '\n'
+ # '\r' : '\n'
+ # '\n' : '\n'
+ # '\x85' : '\n'
+ # '\u2028' : '\u2028'
+ # '\u2029 : '\u2029'
+ # default : ''
+ ch = self.peek()
+ if ch in u'\r\n\x85':
+ if self.prefix(2) == u'\r\n':
+ self.forward(2)
+ else:
+ self.forward()
+ return u'\n'
+ elif ch in u'\u2028\u2029':
+ self.forward()
+ return ch
+ elif empty_line and ch in '\t ':
+ return ch
+ return u''
+
# try:
# import psyco
# psyco.bind(Scanner)