[New-bugs-announce] [issue17976] file.write doesn't raise IOError when it should

Jaakko Moisio report at bugs.python.org
Tue May 14 17:10:27 CEST 2013


New submission from Jaakko Moisio:

file.write doesn't sometimes raise IOError when it should, e.g. writing to /dev/full in line buffered mode:

jaakko at jm-laptop:~$ python
Python 2.7.5+ (2.7:a32a3b79f5e8, May 14 2013, 14:20:11) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open('/dev/full','w',1)
>>> f.write('hello\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 28] No space left on device
>>> f.close()
>>> f=open('/dev/full','w',1)
>>> f.write('hello')
>>> f.write('\n')
>>> f.close()
>>> # No IOError!
... 
>>> 

The current implementation of file.write relies on comparing the return value of fwrite to the expected number of bytes written to detect errors. I haven't dug deep enough into the C standard to know for sure if fwrite return value == expected should always imply no error, but in my example it clearly doesn't (I assume my glibc and fwrite aren't broken though). However using ferror to detect the error works fine and IOError was raised as expected.

Python3 and io module use different implementation where this is no longer an issue. For us still using Python 2.7 I suggest the attached simple patch to fix the issue.

----------
components: Interpreter Core
files: fileobject-fix.patch
keywords: patch
messages: 189226
nosy: jasujm
priority: normal
severity: normal
status: open
title: file.write doesn't raise IOError when it should
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file30258/fileobject-fix.patch

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


More information about the New-bugs-announce mailing list