From fd003083b6954ec1d5827984abb8ed61de400915 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Thu, 16 May 2019 00:31:23 +0200 Subject: fix comments on anchored nodes in block sequence fixes issue #288 --- .appveyor.yaml | 3 --- CHANGES | 7 +++++++ Dockerfile | 2 +- README.rst | 11 +++++++++-- __init__.py | 7 +++---- _doc/_static/pypi.svg | 2 +- _test/test_issues.py | 31 +++++++++++++++++++++++++++++++ emitter.py | 5 +++-- scanner.py | 22 +++++++++++++++++++++- tox.ini | 2 +- 10 files changed, 77 insertions(+), 15 deletions(-) diff --git a/.appveyor.yaml b/.appveyor.yaml index fcc4086..4f53ba4 100644 --- a/.appveyor.yaml +++ b/.appveyor.yaml @@ -4,9 +4,6 @@ environment: matrix: - PYTHON: C:\Python27 - PYTHON: C:\Python27-x64 - - PYTHON: C:\Python34 - - PYTHON: C:\Python34-x64 - DISTUTILS_USE_SDK: '1' - PYTHON: C:\Python35 - PYTHON: C:\Python35-x64 - PYTHON: C:\Python36 diff --git a/CHANGES b/CHANGES index cbbbf1d..c7b0587 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +[0, 15, 95]: 2019-05-16 + - fix failure to round-trip anchored scalars in block sequence + (reported by `William Kimball + `__) + - wheel files for Python 3.4 no longer provided (`Python 3.4 EOL 2019-03-18 + `__) + [0, 15, 94]: 2019-04-23 - fix missing line-break after end-of-file comments not ending in line-break (reported by `Philip Thompson diff --git a/Dockerfile b/Dockerfile index bf837e6..ef42f24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,6 @@ RUN echo 'done' >> /usr/bin/makewheel RUN chmod 755 /usr/bin/makewheel -CMD /usr/bin/makewheel 27 34 35 36 37 +CMD /usr/bin/makewheel 27 35 36 37 # cp27-cp27m p27-cp27mu cp34-cp34m cp35-cp35m cp36-cp36m cp37-cp37m diff --git a/README.rst b/README.rst index 368f536..e2049de 100644 --- a/README.rst +++ b/README.rst @@ -4,8 +4,8 @@ ruamel.yaml ``ruamel.yaml`` is a YAML 1.2 loader/dumper package for Python. -:version: 0.15.94 -:updated: 2019-04-23 +:version: 0.15.95 +:updated: 2019-05-16 :documentation: http://yaml.readthedocs.io :repository: https://bitbucket.org/ruamel/ :pypi: https://pypi.org/project/ruamel.yaml/ @@ -54,6 +54,13 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key (with empty line) +0.15.95 (2019-05-16): + - fix failure to round-trip anchored scalars in block sequence + (reported by `William Kimball + `__) + - wheel files for Python 3.4 no longer provided (`Python 3.4 EOL 2019-03-18 + `__) + 0.15.94 (2019-04-23): - fix missing line-break after end-of-file comments not ending in line-break (reported by `Philip Thompson diff --git a/__init__.py b/__init__.py index c7aa6c8..82769bb 100644 --- a/__init__.py +++ b/__init__.py @@ -7,8 +7,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 15, 94), - __version__='0.15.94', + version_info=(0, 15, 95), + __version__='0.15.95', 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 @@ -49,7 +49,6 @@ _package_data = dict( # test='#include "ext/yaml.h"\n\nint main(int argc, char* argv[])\n{\nyaml_parser_t parser;\nparser = parser; /* prevent warning */\nreturn 0;\n}\n', # NOQA classifiers=[ 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', @@ -62,7 +61,7 @@ _package_data = dict( keywords='yaml 1.2 parser round-trip preserve quotes order config', wheels=dict(windows='appveyor', linux='libyaml-devel', macos='builder@macos'), read_the_docs='yaml', - supported=[(2, 7), (3, 4)], # minimum + supported=[(2, 7), (3, 5)], # minimum tox=dict( env='*pn', # also test narrow Python 2.7.15 for unicode patterns deps='ruamel.std.pathlib', diff --git a/_doc/_static/pypi.svg b/_doc/_static/pypi.svg index 10d8154..1c1ffd2 100644 --- a/_doc/_static/pypi.svg +++ b/_doc/_static/pypi.svg @@ -1 +1 @@ - pypipypi0.15.940.15.94 + pypipypi0.15.950.15.95 diff --git a/_test/test_issues.py b/_test/test_issues.py index dd534ca..ec7d95d 100644 --- a/_test/test_issues.py +++ b/_test/test_issues.py @@ -664,6 +664,37 @@ class TestIssues: yaml.dump(a, buf) assert buf.getvalue().endswith('xxx\nnew_key: new_value\n') + def test_issue_288(self): + import sys + from ruamel.yaml.compat import StringIO + from ruamel.yaml import YAML + + yamldoc = dedent("""\ + --- + # Reusable values + aliases: + # First-element comment + - &firstEntry First entry + # Second-element comment + - &secondEntry Second entry + + # Third-element comment is + # a multi-line value + - &thirdEntry Third entry + + # EOF Comment + """) + + yaml = YAML() + yaml.indent(mapping=2, sequence=4, offset=2) + yaml.explicit_start = True + yaml.preserve_quotes = True + yaml.width = sys.maxsize + data = yaml.load(yamldoc) + buf = StringIO() + yaml.dump(data, buf) + assert buf.getvalue() == yamldoc + # @pytest.mark.xfail(strict=True, reason='bla bla', raises=AssertionError) # def test_issue_ xxx(self): # inp = """ diff --git a/emitter.py b/emitter.py index d4884bd..0ed1932 100644 --- a/emitter.py +++ b/emitter.py @@ -421,7 +421,6 @@ class Emitter(object): self.indention = False self.no_newline = True if self.write_pre_comment(self.event): - pass self.indention = i2 self.no_newline = not self.indention if ( @@ -780,6 +779,8 @@ class Emitter(object): self.prepared_anchor = self.prepare_anchor(self.event.anchor) if self.prepared_anchor: self.write_indicator(indicator + self.prepared_anchor, True) + # issue 288 + self.no_newline = False self.prepared_anchor = None return True @@ -1631,7 +1632,7 @@ class Emitter(object): # at least one space if the current column >= the start column of the comment # but not at the start of a line nr_spaces = col - self.column - if self.column and value.strip() and nr_spaces < 1: + if self.column and value.strip() and nr_spaces < 1 and value[0] != '\n': nr_spaces = 1 value = ' ' * nr_spaces + value try: diff --git a/scanner.py b/scanner.py index 46247f1..9ed711a 100644 --- a/scanner.py +++ b/scanner.py @@ -1794,7 +1794,7 @@ class RoundTripScanner(Scanner): if isinstance(self.tokens[0], CommentToken): self.tokens_taken += 1 comment = self.tokens.pop(0) - # print 'dropping2', comment + # nprint('dropping2', comment) comments.append(comment) if len(comments) >= 1: self.tokens[0].add_pre_comments(comments) @@ -1825,6 +1825,26 @@ class RoundTripScanner(Scanner): ): self.tokens_taken += 1 self.tokens[0].add_post_comment(self.tokens.pop(1)) + elif ( + len(self.tokens) > 1 + and isinstance(self.tokens[0], ScalarToken) + and isinstance(self.tokens[1], CommentToken) + and self.tokens[0].end_mark.line != self.tokens[1].start_mark.line + ): + self.tokens_taken += 1 + c = self.tokens.pop(1) + c.value = ( + '\n' * (c.start_mark.line - self.tokens[0].end_mark.line) + + (' ' * c.start_mark.column) + + c.value + ) + self.tokens[0].add_post_comment(c) + self.fetch_more_tokens() + while len(self.tokens) > 1 and isinstance(self.tokens[1], CommentToken): + self.tokens_taken += 1 + c1 = self.tokens.pop(1) + c.value = c.value + (' ' * c.start_mark.column) + c1.value + self.fetch_more_tokens() self.tokens_taken += 1 return self.tokens.pop(0) return None diff --git a/tox.ini b/tox.ini index 86baf60..56fb982 100755 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] # toxworkdir = /data1/DATA/tox/ruamel.yaml -envlist = cs,py37,py27,py36,py35,py34,py38,pypy,py27m +envlist = cs,py37,py27,py36,py35,py38,pypy,py27m [testenv] commands = -- cgit v1.2.1