[Python-checkins] cpython (2.7): Issue #15267: HTTPConnection.request() now is compatibile with old-style

serhiy.storchaka python-checkins at python.org
Sat May 16 17:59:14 CEST 2015


https://hg.python.org/cpython/rev/c34513c2a894
changeset:   96089:c34513c2a894
branch:      2.7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat May 16 18:58:41 2015 +0300
summary:
  Issue #15267: HTTPConnection.request() now is compatibile with old-style
classes (such as TemporaryFile).  Original patch by Atsuo Ishimoto.

files:
  Lib/httplib.py           |   2 +-
  Lib/test/test_httplib.py |  17 +++++++++++++++++
  Misc/NEWS                |   3 +++
  3 files changed, 21 insertions(+), 1 deletions(-)


diff --git a/Lib/httplib.py b/Lib/httplib.py
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -1063,7 +1063,7 @@
         elif body is not None:
             try:
                 thelen = str(len(body))
-            except TypeError:
+            except (TypeError, AttributeError):
                 # If this is a file-like object, try to
                 # fstat its file descriptor
                 try:
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -5,6 +5,7 @@
 import socket
 import errno
 import os
+import tempfile
 
 import unittest
 TestCase = unittest.TestCase
@@ -399,6 +400,22 @@
         conn.sock = sock
         conn.request('GET', '/foo', body)
         self.assertTrue(sock.data.startswith(expected))
+        self.assertIn('def test_send_file', sock.data)
+
+    def test_send_tempfile(self):
+        expected = ('GET /foo HTTP/1.1\r\nHost: example.com\r\n'
+                    'Accept-Encoding: identity\r\nContent-Length: 9\r\n\r\n'
+                    'fake\ndata')
+
+        with tempfile.TemporaryFile() as body:
+            body.write('fake\ndata')
+            body.seek(0)
+
+            conn = httplib.HTTPConnection('example.com')
+            sock = FakeSocket(body)
+            conn.sock = sock
+            conn.request('GET', '/foo', body)
+        self.assertEqual(sock.data, expected)
 
     def test_send(self):
         expected = 'this is a test this is only a test'
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 Library
 -------
 
+- Issue #15267: HTTPConnection.request() now is compatibile with old-style
+  classes (such as TemporaryFile).  Original patch by Atsuo Ishimoto.
+
 - Issue #20014: array.array() now accepts unicode typecodes.  Based on patch by
   Vajrasky Kok.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list