[New-bugs-announce] [issue23562] file.read() followed by file.write() result in incorrect behavior

Maxime S report at bugs.python.org
Mon Mar 2 15:37:41 CET 2015


New submission from Maxime S:

Observed Behavior:

$python3
Python 3.5.0a1+ (default, Mar  2 2015, 14:30:05) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("test", "w+")
>>> f.write("Hello World")
11
>>> f.seek(0)
0
>>> f.read(5)
'Hello'
>>> f.write(" people")
7
>>> f.seek(0)
0
>>> f.read()
'Hello World people'


Expected Behavior

According to POSIX, the last f.read() should return "Hello people", like in Python 2:


$ python2
Python 2.7.3 (default, Feb 27 2014, 20:00:17) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("test", "w+")
>>> f.write("Hello World")
>>> f.seek(0)
>>> f.read(5)
'Hello'
>>> f.write(" people")
>>> f.seek(0)
>>> f.read()
'Hello people'

Workaround:

f.seek(f.tell()) immediately after f.read(5) restore the correct behavior.

----------
components: IO
messages: 237046
nosy: Maxime S
priority: normal
severity: normal
status: open
title: file.read() followed by file.write() result in incorrect behavior
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23562>
_______________________________________


More information about the New-bugs-announce mailing list