[Python-checkins] cpython (merge default -> default): Close #18978: Merge changes.

jason.coombs python-checkins at python.org
Sun Sep 22 16:11:23 CEST 2013


http://hg.python.org/cpython/rev/8620aea9bbca
changeset:   85786:8620aea9bbca
parent:      85780:2e1335245f8f
parent:      85785:473a662a2309
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Sun Sep 22 10:06:24 2013 -0400
summary:
  Close #18978: Merge changes.

files:
  Lib/test/test_urllib2.py |   9 +++++++++
  Lib/urllib/request.py    |  11 ++++-------
  Misc/NEWS                |   3 +++
  3 files changed, 16 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1466,16 +1466,25 @@
         self.assertEqual(str(err), expected_errmsg)
 
 class RequestTests(unittest.TestCase):
+    class PutRequest(Request):
+        method='PUT'
 
     def setUp(self):
         self.get = Request("http://www.python.org/~jeremy/")
         self.post = Request("http://www.python.org/~jeremy/",
                             "data",
                             headers={"X-Test": "test"})
+        self.head = Request("http://www.python.org/~jeremy/", method='HEAD')
+        self.put = self.PutRequest("http://www.python.org/~jeremy/")
+        self.force_post = self.PutRequest("http://www.python.org/~jeremy/",
+            method="POST")
 
     def test_method(self):
         self.assertEqual("POST", self.post.get_method())
         self.assertEqual("GET", self.get.get_method())
+        self.assertEqual("HEAD", self.head.get_method())
+        self.assertEqual("PUT", self.put.get_method())
+        self.assertEqual("POST", self.force_post.get_method())
 
     def test_data(self):
         self.assertFalse(self.get.data)
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -271,7 +271,8 @@
             origin_req_host = request_host(self)
         self.origin_req_host = origin_req_host
         self.unverifiable = unverifiable
-        self.method = method
+        if method:
+            self.method = method
 
     @property
     def full_url(self):
@@ -320,12 +321,8 @@
 
     def get_method(self):
         """Return a string indicating the HTTP request method."""
-        if self.method is not None:
-            return self.method
-        elif self.data is not None:
-            return "POST"
-        else:
-            return "GET"
+        default_method = "POST" if self.data is not None else "GET"
+        return getattr(self, 'method', default_method)
 
     def get_full_url(self):
         return self.full_url
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Library
 -------
 
+- Issue #18978: ``urllib.request.Request`` now allows the method to be
+  indicated on the class and no longer sets it to None in ``__init__``.
+
 - Issue #18626: the inspect module now offers a basic command line
   introspection interface (Initial patch by Claudiu Popa)
 

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


More information about the Python-checkins mailing list