Issue 10819: ValueError on repr(closed_socket_file) (original) (raw)

Created on 2011-01-03 23:51 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue10819.patch vstinner,2011-01-03 23:52
socketio_name.patch vstinner,2011-01-04 00:17
Messages (5)
msg125253 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-03 23:51
The following code raise a ValueError('I/O operation on closed file.'): --- import socket socket.socket(socket.SOCK_STREAM, socket.AF_INET) s=socket.socket(socket.SOCK_STREAM, socket.AF_INET) f=s.makefile("rb") f.close() print(repr(f)) --- io.BufferedReader.__repr__() reads self.name (self.buffer.name), but self.buffer is closed. io.BufferedReader.__repr__() catchs AttributeError when reading self.name. It should also maybe catch ValueError: attached patch does that. socket.repr(x) returns a string with "[closed]". BufferedReader.repr() should maybe do the same. Note: TextIOWrapper has the same issue.
msg125256 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-04 00:17
Antoine suggested me to patch SocketIO.name property instead of the BufferedReader.__repr__() method: socketio_name.patch implements this idea.
msg125301 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-04 11:00
Fixed by r87730.
msg348390 - (view) Author: Nir Soffer (nirs) * Date: 2019-07-24 15:16
I find this new behavior a usability regression. Before this change, code (e.g python 2 code ported to python 3) could do: fd = sock.fileno() Without handling errors, since closed socket would raise (good). Now such code need to check the return value (bad): fd = sock.fileno() if fd == -1: fail... This is also not consistent with other objects: >>> f = open("Makefile") >>> f.fileno() 3 >>> f.close() >>> f.fileno() Traceback (most recent call last): File "", line 1, in ValueError: I/O operation on closed file >>> repr(f) "<_io.TextIOWrapper name='Makefile' mode='r' encoding='UTF-8'>" The issue with repr() on closed socket can be mitigated easily inside __repr__, handling closed sockets without affecting code using file descriptors. Can we return the old safe behavior?
msg348672 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-07-29 15:53
This issue is closed. Please open a new issue.
History
Date User Action Args
2022-04-11 14:57:10 admin set github: 55028
2019-07-29 15:53:31 vstinner set messages: +
2019-07-24 15:16:38 nirs set nosy: + nirsmessages: +
2011-01-04 11:00:59 vstinner set status: open -> closedmessages: + resolution: fixednosy:pitrou, vstinner
2011-01-04 00:17:39 vstinner set files: + socketio_name.patchnosy:pitrou, vstinnermessages: +
2011-01-03 23:52:10 vstinner set files: + issue10819.patchnosy:pitrou, vstinnerkeywords: + patch
2011-01-03 23:51:31 vstinner create