This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: newlines attribute does not get set after calling readline()
Type: behavior Stage: resolved
Components: IO Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: arekfu, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-05-05 09:21 by arekfu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg242593 - (view) Author: Davide Mancusi (arekfu) Date: 2015-05-05 09:21
I have a text file with Windows-style line terminators (\r\n) which I open in universal newlines mode. I would expect the newlines attribute to be set after the first call to the readline() method, but apparently this is not the case:

>>> f=open('test_crlf', 'rU')
>>> f.newlines
>>> f.readline()
'foo\n'
>>> f.newlines
>>> f.readline()
'bar\n'
>>> f.newlines
'\r\n'

On the other hand, the newlines attribute gets set after the first call to readline() on a file with Unix-style line endings.

Also, surprisingly, calling tell() after the first readline() is enough to update the newlines attribute:

>>> f=open('test_crlf', 'rU')
>>> f.newlines
>>> f.readline()
'foo\n'
>>> f.newlines
>>> f.tell()
77
>>> f.newlines
'\r\n'

Are these behaviours intended? If so, they should probably be documented.
msg242596 - (view) Author: Davide Mancusi (arekfu) Date: 2015-05-05 10:56
For completeness, here is some versioning information:

$ python
Python 2.7 (r27:82500, Mar 23 2015, 16:46:39) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> print sysconfig.get_config_var('CONFIG_ARGS')                                                                                                                                                                                              
'--prefix=/opt/python2.7' '--enable-shared'
msg370478 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-31 15:06
Python 2.7 is no longer supported.
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68314
2020-05-31 15:06:30serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg370478

resolution: out of date
stage: resolved
2015-05-05 10:56:53arekfusetmessages: + msg242596
2015-05-05 09:21:48arekfucreate