mirror of
https://github.com/python/cpython.git
synced 2025-08-22 01:35:16 +00:00
Patch #1627441: close sockets properly in urllib2.
(backport from rev. 53511)
This commit is contained in:
parent
8e932e7d68
commit
962e9165aa
5 changed files with 63 additions and 6 deletions
|
@ -204,9 +204,10 @@ class _fileobject(object):
|
|||
|
||||
__slots__ = ["mode", "bufsize", "softspace",
|
||||
# "closed" is a property, see below
|
||||
"_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"]
|
||||
"_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf",
|
||||
"_close"]
|
||||
|
||||
def __init__(self, sock, mode='rb', bufsize=-1):
|
||||
def __init__(self, sock, mode='rb', bufsize=-1, close=False):
|
||||
self._sock = sock
|
||||
self.mode = mode # Not actually used in this version
|
||||
if bufsize < 0:
|
||||
|
@ -222,6 +223,7 @@ class _fileobject(object):
|
|||
self._wbufsize = bufsize
|
||||
self._rbuf = "" # A string
|
||||
self._wbuf = [] # A list of strings
|
||||
self._close = close
|
||||
|
||||
def _getclosed(self):
|
||||
return self._sock is None
|
||||
|
@ -232,6 +234,8 @@ class _fileobject(object):
|
|||
if self._sock:
|
||||
self.flush()
|
||||
finally:
|
||||
if self._close:
|
||||
self._sock.close()
|
||||
self._sock = None
|
||||
|
||||
def __del__(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue