summaryrefslogtreecommitdiffstats
path: root/Lib/nntplib.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-21 07:40:26 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-21 07:40:26 (GMT)
commit52027c301a0b3675bb5db23d33eede3f6b19395f (patch)
tree1d43b46d6863740f5b776465f459c493bd0bca52 /Lib/nntplib.py
parent63998a3520ac4c2217946baf99574d9e6a6a959d (diff)
downloadcpython-52027c301a0b3675bb5db23d33eede3f6b19395f.zip
cpython-52027c301a0b3675bb5db23d33eede3f6b19395f.tar.gz
cpython-52027c301a0b3675bb5db23d33eede3f6b19395f.tar.bz2
Issue #22351: The nntplib.NNTP constructor no longer leaves the connection
and socket open until the garbage collector cleans them up. Patch by Martin Panter.
Diffstat (limited to 'Lib/nntplib.py')
-rw-r--r--Lib/nntplib.py36
1 files changed, 25 insertions, 11 deletions
diff --git a/Lib/nntplib.py b/Lib/nntplib.py
index bcf7d1b..3413610 100644
--- a/Lib/nntplib.py
+++ b/Lib/nntplib.py
@@ -1041,11 +1041,18 @@ class NNTP(_NNTPBase):
self.host = host
self.port = port
self.sock = socket.create_connection((host, port), timeout)
- file = self.sock.makefile("rwb")
- _NNTPBase.__init__(self, file, host,
- readermode, timeout)
- if user or usenetrc:
- self.login(user, password, usenetrc)
+ file = None
+ try:
+ file = self.sock.makefile("rwb")
+ _NNTPBase.__init__(self, file, host,
+ readermode, timeout)
+ if user or usenetrc:
+ self.login(user, password, usenetrc)
+ except:
+ if file:
+ file.close()
+ self.sock.close()
+ raise
def _close(self):
try:
@@ -1065,12 +1072,19 @@ if _have_ssl:
in default port and the `ssl_context` argument for SSL connections.
"""
self.sock = socket.create_connection((host, port), timeout)
- self.sock = _encrypt_on(self.sock, ssl_context, host)
- file = self.sock.makefile("rwb")
- _NNTPBase.__init__(self, file, host,
- readermode=readermode, timeout=timeout)
- if user or usenetrc:
- self.login(user, password, usenetrc)
+ file = None
+ try:
+ self.sock = _encrypt_on(self.sock, ssl_context, host)
+ file = self.sock.makefile("rwb")
+ _NNTPBase.__init__(self, file, host,
+ readermode=readermode, timeout=timeout)
+ if user or usenetrc:
+ self.login(user, password, usenetrc)
+ except:
+ if file:
+ file.close()
+ self.sock.close()
+ raise
def _close(self):
try: