Modify

Ticket #236 (closed defect: invalid)

Opened 2 years ago

Last modified 2 years ago

PyYAML strips \n in the end of documents

Reported by: Matěj Cepl <mcepl@…> Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description

When running the attached script the test fails:

mitmanek:yamlish (master) $ python failing_test.py 
F
======================================================================
FAIL: test_file_output (__main__.TestOuptut)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "failing_test.py", line 52, in test_file_output
    """ % (self._expected, got))
AssertionError: Result matches
              expected = {'bill-to': {'address': {'lines': '458 Walkman Dr. Suite #292 '}}, 'comments': 'Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338'}

              observed = {'bill-to': {'address': {'lines': '458 Walkman Dr.\nSuite #292\n'}}, 'comments': 'Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n'}
              

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (failures=1)
mitmanek:yamlish (master) $ 

Attachments

failing_test.py Download (1.2 KB) - added by Matěj Cepl <mcepl@…> 2 years ago.
simple test case
failing_test.2.py Download (1.1 KB) - added by Matěj Cepl <mcepl@…> 2 years ago.
simple test case

Change History

Changed 2 years ago by Matěj Cepl <mcepl@…>

simple test case

Changed 2 years ago by Matěj Cepl <mcepl@…>

simple test case

comment:1 Changed 2 years ago by Matěj Cepl <mcepl@…>

  • Summary changed from PyYAML doesn't round-trip \n to PyYAML strips \n in the end of documents

New version of the test case show cleanly that only \n in the end of document are killed (why? What's different about them?). Running the test case I get:

mitmanek:yamlish (master) $ unit2 -v failing_test
test_file_output (failing_test.TestOuptut) ... FAIL

======================================================================
FAIL: test_file_output (failing_test.TestOuptut)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/matej/archiv/2012/RedHat/projekty/yamlish/failing_test.py", line 48, in test_file_output
    self.assertEqual(self._expected, got, "Result matches")
AssertionError: {'bill-to': {'address': {'lines': '458 Walkman Dr.\nSuite #292\n'}}, 'comments': [truncated]... != {'bill-to': {'address': {'lines': '458 Walkman Dr.\nSuite #292\n'}}, 'comments': [truncated]...
  {'bill-to': {'address': {'lines': '458 Walkman Dr.\nSuite #292\n'}},
-  'comments': 'Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338'}
+  'comments': 'Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n'}
?                                                                                  ++
 : Result matches

----------------------------------------------------------------------
Ran 1 test in 0.009s

FAILED (failures=1)
mitmanek:yamlish (master) $ 

comment:2 Changed 2 years ago by xi

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

PyYAML interpretation is correct: a single newline character in a double-quoted scalar is converted to a space; any trailing newline characters at the end of an unquoted scalar are ignored.

You are sure you understand how \n works in Python strings?

View

Add a comment

Modify Ticket

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


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

 
Note: See TracTickets for help on using tickets.