[Python-bugs-list] [ python-Bugs-768857 ] file readline() mishandles \032 char on Windows
SourceForge.net
noreply@sourceforge.net
Thu, 10 Jul 2003 07:46:54 -0700
Bugs item #768857, was opened at 2003-07-09 23:27
Message generated for change (Comment added) made by tim_one
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=768857&group_id=5470
Category: Windows
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Mark Bucciarelli (mbucc)
Assigned to: Tim Peters (tim_one)
Summary: file readline() mishandles \032 char on Windows
Initial Comment:
The following snippet behaves differently on Windows (2.2.2) and
Linux (2.2.1).
>>>s = 'ab\032cd'
>>>f = file('test', 'w')
>>>f.write(s+'\n')
>>>f.close()
>>>f = file('test')
>>>f.readline()
Linux returns: 'ab\x1a\cd\n'
Windows returns: 'ab'
\032 is ascii substitute and also ^z according to this page
(http://francis.courtois.free.fr/jc1/serial/Resources/ASCII.html).
Thank you.
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2003-07-10 10:46
Message:
Logged In: YES
user_id=31435
Because Python uses C I/O, the C standard's definition of
binary data is the relevant definition. If you don't have a
copy of the C standard, the "Text and Binary Streams"
section of
http://www-ccs.ucsd.edu/c/lib_file.html
is a good summary. It's correct in stating that the only chars
with ord below 32 that C guarantees can be written and read
in text-mode files are newline and tab (NL and HT). In
particular, on Windows chr(26) in a text mode file is taken as
an "end of file" indicator. That's been true since the earliest
DOS days, and is fine by the C standard.
----------------------------------------------------------------------
Comment By: Mark Bucciarelli (mbucc)
Date: 2003-07-10 10:33
Message:
Logged In: YES
user_id=35056
Why do you say chr(26) is binary data? It's an ASCII char. Aren't all ASCII
characters text?
I just built a unit test and all other ascii characters work as expected.
----------------------------------------------------------------------
Comment By: Tim Peters (tim_one)
Date: 2003-07-10 10:03
Message:
Logged In: YES
user_id=31435
If you want consistent cross-platform behavior when reading
or writing binary data, you have to open files in binary mode.
That is, do not do
f = file('test', 'w')
or
f = file('test')
Instead do
f = file('test', 'wb')
and
f = file('test', 'rb')
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=768857&group_id=5470