]> granicus.if.org Git - python/commitdiff
Merge: #10883: Fix socket leaks in urllib.request.
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 23 Jul 2011 12:25:45 +0000 (14:25 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 23 Jul 2011 12:25:45 +0000 (14:25 +0200)
* ftpwrapper now uses reference counting to ensure that the underlying socket
  is closed when the ftpwrapper object is no longer in use
* ftplib.FTP.ntransfercmd() now closes the socket if an error occurs

Initial patch by Victor Stinner.

1  2 
Lib/ftplib.py
Lib/test/test_urllib2.py
Lib/urllib/request.py

diff --cc Lib/ftplib.py
index eaaa6fd69c0938949a33050e47a88b3782808545,8e5302327030837432321de73026ee7aed41d858..f87f6909c63799cbe56bb8eeac81af037fc771a5
@@@ -341,35 -335,40 +341,41 @@@ class FTP
          size = None
          if self.passiveserver:
              host, port = self.makepasv()
 -            conn = socket.create_connection((host, port), self.timeout)
 +            conn = socket.create_connection((host, port), self.timeout,
 +                                            source_address=self.source_address)
-             if rest is not None:
-                 self.sendcmd("REST %s" % rest)
-             resp = self.sendcmd(cmd)
-             # Some servers apparently send a 200 reply to
-             # a LIST or STOR command, before the 150 reply
-             # (and way before the 226 reply). This seems to
-             # be in violation of the protocol (which only allows
-             # 1xx or error messages for LIST), so we just discard
-             # this response.
-             if resp[0] == '2':
-                 resp = self.getresp()
-             if resp[0] != '1':
-                 raise error_reply(resp)
+             try:
+                 if rest is not None:
+                     self.sendcmd("REST %s" % rest)
+                 resp = self.sendcmd(cmd)
+                 # Some servers apparently send a 200 reply to
+                 # a LIST or STOR command, before the 150 reply
+                 # (and way before the 226 reply). This seems to
+                 # be in violation of the protocol (which only allows
+                 # 1xx or error messages for LIST), so we just discard
+                 # this response.
+                 if resp[0] == '2':
+                     resp = self.getresp()
+                 if resp[0] != '1':
+                     raise error_reply(resp)
+             except:
+                 conn.close()
+                 raise
          else:
              sock = self.makeport()
-             if rest is not None:
-                 self.sendcmd("REST %s" % rest)
-             resp = self.sendcmd(cmd)
-             # See above.
-             if resp[0] == '2':
-                 resp = self.getresp()
-             if resp[0] != '1':
-                 raise error_reply(resp)
-             conn, sockaddr = sock.accept()
-             if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
-                 conn.settimeout(self.timeout)
-             sock.close()
+             try:
+                 if rest is not None:
+                     self.sendcmd("REST %s" % rest)
+                 resp = self.sendcmd(cmd)
+                 # See above.
+                 if resp[0] == '2':
+                     resp = self.getresp()
+                 if resp[0] != '1':
+                     raise error_reply(resp)
+                 conn, sockaddr = sock.accept()
+                 if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
+                     conn.settimeout(self.timeout)
+             finally:
+                 sock.close()
          if resp[:3] == '150':
              # this is conditional in case we received a 125
              size = parse150(resp)
Simple merge
Simple merge