You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that Create File and Append To File convert \n to \r\n on Windows starting from RF 3.1 #3097
Since upgrading to version 3.1.1 of robot framework (from version 3.0.4) I encounter the following.
When I create a file and then write lines to that file - which I end with ${\n} because I want each on a new line - an extra new line character (CR actually) gets added. This did not happen before and this is unwanted behaviour.
Example test to reproduce
`
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Test Me
create file C:/Users/Testfile.txt
append to file C:/Users/Testfile.txt Line1${\n}
append to file C:/Users/Testfile.txt Line2${\n}
append to file C:/Users/Testfile.txt Line3${\n}
`
Output
Expected output
System details
OS: Windows 10 Enterprise 1703
Robot Framework version: 3.1.1
Python version: 2.7.12
Perhaps the problem is in the handling of built-in variable ${\n}?
What I see upon further testing is that if I use '\n' instead of '${\n}' in my 'append to file', my result is like the expected output again, both on the old and new version of Robot Framework.
Here's a potentially relevant difference in OperatingSystem.py between versions:
# 3.1.1def_write_to_file(self, path, content, encoding=None, mode='w'):
path=self._absnorm(path)
parent=os.path.dirname(path)
ifnotos.path.exists(parent):
os.makedirs(parent)
# io.open() only accepts Unicode, not byte-strings, in text mode.# We expect possible byte-strings to be all ASCII.ifPY2andisinstance(content, str) and'b'notinmode:
content=unicode(content)
withio.open(path, mode, encoding=encoding) asf:
f.write(content)
returnpath
First of all, ${\n} is either CRLF or LF, depending on the OS, when \n is always just LF. It seems that earlier files were opened in binary mode in this particular case and that meant that all characters/bytes were written to files as-is. Nowadays files are opened in text mode, which means that on Windows LF is automatically converted to CRLF (and CRLF is turned into CRCRLF). That explains why using ${\n} on Windows causes problems with RF 3.1 but I don't understand how \n works with earlier versions. Could you @MichielYKamp test this again?
This change wasn't actually planned, but I still consider the current behavior better than the old one. I'd thus prefer keeping the current behavior but the change should still be documented somewhere.
I understand your explanation. I can see how opening a file in text mode by default may be better then opening it in binary mode, but the fact that CRLF is than converted into CRCRLF on Windows I still find strange. If I am on Windows and already giving CRLF in the append to file, I think it shouldn't convert my CRLF again because it is not needed. It should only convert LF to CRLF.
I tested \n on the old version of RF and, as expected, I get just LF in my lines there.
If nothing will be changed then I agree that making this behaviour clear in the documentation, for both ${\n} and append to file (and similar keywords) would be good.
I don't think I quite understand. From the documentation of the io.open method:
On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller
In the 3.1.1 code snippet I provided above, io.open is called such that newline is None, so by the quoted explanation I would expect the ${\n} (as well as any other line ending characters due to the universal newlines being enabled) to ultimately be converted to \n (or LF).
I would appreciate it if someone could clear this up for me, thanks.
@bartkl "On input" isn't very clear, but it seems to mean reading in this case. What it means in practice is that if you have a file with content first\r\nsecond\r\n, you'll by default get first\nsecond\n when you read it with Python. When writing, Python will automatically convert \n to \r\n. Newline handling is also affected by mode argument. In text mode (default) conversion happens but in binary mode it doesn't:
With RF 3.1.x \n is converted to \r\n and ${\n} is converted to \r\r\n.
With RF 3.0.x (and earlier) \n results to \n and ${\n} yields \r\n.
That would be logical based on the code and seems to be what your results are as well. Such a change definitely wouldn't be good without an explicit note in the release notes.
Somewhat strange nobody else has reported this issue. I would assume most people have used \n and tools reading files generally handle the resulting \n just fine. With newer RF versions \n yields \r\n which tools handle even better.
Something needs to be done for this in RF 3.1.2. Alternatives include.
Switch to the old behavior. I don't like this too much because the current one is, in my opinion, better and also consistent with how Python handles files.
Just document the current behavior and mention also that there have been changes in RF 3.1.
Add a new argument to handle newline conversion similarly as Python has. Would allow creating files with \n as a separator also on Windows. Documentation enhancements would still be needed.
Regardless what is done, should also add a note to RF 3.1 release notes about this change.
Been thinking this a bit more and here are decisions:
Keep the behavior as-is. It's more convenient in general and being consistent with Python is a plus too. Based on there not being more issues about this, the change doesn't seem to affect that many users.
Document the change. This includes mentioning it in RF 3.1 release notes and updating keyword documentation.
Add explicit tests.
Don't add new argument to control newline behavior. Because old versions wouldn't have this argument, it wouldn't help handling the backwards-incompatible change anyway. If such an argument is considered useful otherwise, a separate issue can be submitted.
changed the title [-]Extra new line inserted with 'OperatingSystem.append to file'[/-][+]Document that `Create File` and `Append To File` convert `\n` to `\r\n` on Windows starting from RF 3.1[/+]on Apr 16, 2019
Forgot to mention that with Create File a good workaround is using Create Binary File that doesn't do newline conversion with any version. If content contains non-ASCII characters, Encode String To Bytes needs to be used first to handle them, though.
Activity
mytzenka commentedon Feb 20, 2019
Perhaps the problem is in the handling of built-in variable ${\n}?
What I see upon further testing is that if I use '\n' instead of '${\n}' in my 'append to file', my result is like the expected output again, both on the old and new version of Robot Framework.
bartkl commentedon Feb 26, 2019
Here's a potentially relevant difference in
OperatingSystem.pybetween versions:pekkaklarck commentedon Feb 26, 2019
First of all,
${\n}is eitherCRLForLF, depending on the OS, when\nis always justLF. It seems that earlier files were opened in binary mode in this particular case and that meant that all characters/bytes were written to files as-is. Nowadays files are opened in text mode, which means that on WindowsLFis automatically converted toCRLF(andCRLFis turned intoCRCRLF). That explains why using${\n}on Windows causes problems with RF 3.1 but I don't understand how\nworks with earlier versions. Could you @MichielYKamp test this again?This change wasn't actually planned, but I still consider the current behavior better than the old one. I'd thus prefer keeping the current behavior but the change should still be documented somewhere.
mytzenka commentedon Feb 27, 2019
Thank you for your reply @pekkaklarck .
I understand your explanation. I can see how opening a file in text mode by default may be better then opening it in binary mode, but the fact that
CRLFis than converted intoCRCRLFon Windows I still find strange. If I am on Windows and already giving CRLF in theappend to file, I think it shouldn't convert myCRLFagain because it is not needed. It should only convertLFtoCRLF.I tested

\non the old version of RF and, as expected, I get justLFin my lines there.If nothing will be changed then I agree that making this behaviour clear in the documentation, for both
${\n}andappend to file(and similar keywords) would be good.bartkl commentedon Feb 27, 2019
I don't think I quite understand. From the documentation of the
io.openmethod:In the 3.1.1 code snippet I provided above,
io.openis called such thatnewlineisNone, so by the quoted explanation I would expect the${\n}(as well as any other line ending characters due to the universal newlines being enabled) to ultimately be converted to\n(orLF).I would appreciate it if someone could clear this up for me, thanks.
pekkaklarck commentedon Feb 27, 2019
@bartkl "On input" isn't very clear, but it seems to mean reading in this case. What it means in practice is that if you have a file with content
first\r\nsecond\r\n, you'll by default getfirst\nsecond\nwhen you read it with Python. When writing, Python will automatically convert\nto\r\n. Newline handling is also affected bymodeargument. In text mode (default) conversion happens but in binary mode it doesn't:pekkaklarck commentedon Feb 27, 2019
@MichielYKamp Can you confirm the following?
\nis converted to\r\nand${\n}is converted to\r\r\n.\nresults to\nand${\n}yields\r\n.That would be logical based on the code and seems to be what your results are as well. Such a change definitely wouldn't be good without an explicit note in the release notes.
Somewhat strange nobody else has reported this issue. I would assume most people have used
\nand tools reading files generally handle the resulting\njust fine. With newer RF versions\nyields\r\nwhich tools handle even better.Something needs to be done for this in RF 3.1.2. Alternatives include.
\nas a separator also on Windows. Documentation enhancements would still be needed.Regardless what is done, should also add a note to RF 3.1 release notes about this change.
bartkl commentedon Feb 27, 2019
Oh, of course, it's opened for writing. Then the documentation indeed does state exactly what's happening:
Also, to extend on your example with a case that reflects the original problem:
mytzenka commentedon Feb 27, 2019
Yes, I can confirm this. Thank you.
pekkaklarck commentedon Apr 16, 2019
Thanks for confirmation @MichielYKamp.
Been thinking this a bit more and here are decisions:
Keep the behavior as-is. It's more convenient in general and being consistent with Python is a plus too. Based on there not being more issues about this, the change doesn't seem to affect that many users.
Document the change. This includes mentioning it in RF 3.1 release notes and updating keyword documentation.
Add explicit tests.
Don't add new argument to control newline behavior. Because old versions wouldn't have this argument, it wouldn't help handling the backwards-incompatible change anyway. If such an argument is considered useful otherwise, a separate issue can be submitted.
Mention Create/Append File changes in RF 3.1 notes
[-]Extra new line inserted with 'OperatingSystem.append to file'[/-][+]Document that `Create File` and `Append To File` convert `\n` to `\r\n` on Windows starting from RF 3.1[/+]pekkaklarck commentedon Apr 16, 2019
Forgot to mention that with
Create Filea good workaround is usingCreate Binary Filethat doesn't do newline conversion with any version. If content contains non-ASCII characters,Encode String To Bytesneeds to be used first to handle them, though.Test newline handling with Create Binary File.