From 7dc4f4bbab494f8d9200678bc7df2de89079317c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 8 Sep 2013 12:47:07 -0400 Subject: [PATCH 1/5] Issue #18978: Allow Request.method to be defined at the class level. --- Lib/urllib/request.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 4765a942886..049f48d5323 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -271,7 +271,8 @@ def __init__(self, url, data=None, headers={}, 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,7 +321,7 @@ def _parse(self): def get_method(self): """Return a string indicating the HTTP request method.""" - if self.method is not None: + if getattr(self, 'method', None) is not None: return self.method elif self.data is not None: return "POST" From aae6a1d76fd1336137981cd66acded01d3c36f09 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 8 Sep 2013 12:54:33 -0400 Subject: [PATCH 2/5] Issue #18978: A more elegant technique for resolving the method --- Lib/urllib/request.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 049f48d5323..bceb3297c8e 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -321,12 +321,8 @@ def _parse(self): def get_method(self): """Return a string indicating the HTTP request method.""" - if getattr(self, 'method', None) 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 From 4a6524295d2c3b4ded90bc33365306f89967dc0f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 8 Sep 2013 13:03:40 -0400 Subject: [PATCH 3/5] Issue #18978: Add tests to capture expected behavior for class-level method overrides. --- Lib/test/test_urllib2.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index b3659f442e4..9c71abfae4a 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1466,16 +1466,25 @@ def test_HTTPError_interface(self): 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.assertEquil("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) From 0b5463fef20a2575a3cf7d7f1be3e1bd77502d00 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Mon, 9 Sep 2013 23:13:06 -0700 Subject: [PATCH 4/5] Fix typo --- Lib/test/test_urllib2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9c71abfae4a..dbd1c60ec24 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1482,7 +1482,7 @@ def setUp(self): def test_method(self): self.assertEqual("POST", self.post.get_method()) self.assertEqual("GET", self.get.get_method()) - self.assertEquil("HEAD", self.head.get_method()) + self.assertEqual("HEAD", self.head.get_method()) self.assertEqual("PUT", self.put.get_method()) self.assertEqual("POST", self.force_post.get_method()) From ea9e0974646c045274d3c94c2be9ab019ba2ec8d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 22 Sep 2013 09:40:06 -0400 Subject: [PATCH 5/5] Update NEWS --- Misc/NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 128e740b746..7a9016cf331 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Projected Release date: 2013-10-XX 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__``. + - The :envvar:`PYTHONFAULTHANDLER` environment variable now only enables the faulthandler module if the variable is non-empty. Same behaviour than other variables like :envvar:`PYTHONDONTWRITEBYTECODE`.