]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15267: HTTPConnection.request() now is compatibile with old-style
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 16 May 2015 15:58:41 +0000 (18:58 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 16 May 2015 15:58:41 +0000 (18:58 +0300)
classes (such as TemporaryFile).  Original patch by Atsuo Ishimoto.

Lib/httplib.py
Lib/test/test_httplib.py
Misc/NEWS

index 9f1e088e8ed75c10cb025379e9bbb5dc8fc7d244..30fc5251931b33600d38516b3a64d2d765470cee 100644 (file)
@@ -1063,7 +1063,7 @@ class HTTPConnection:
         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:
index fc7c5710e46d89c1e06920c504d1dcead9349b20..dc986a9956e678933bcede0edd30ec6fad5cff42 100644 (file)
@@ -5,6 +5,7 @@ import StringIO
 import socket
 import errno
 import os
+import tempfile
 
 import unittest
 TestCase = unittest.TestCase
@@ -399,6 +400,22 @@ class BasicTest(TestCase):
         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'
index 5df7c9b39510b21fdaa890f8760a84264f7eb142..c30fd14982142881a4afca6b2bd2f975a9a70257 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 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.