diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2011-02-11 11:25:47 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2011-02-11 11:25:47 (GMT) |
commit | 2933312fe71f15d127009c872cd1e5f98a993999 (patch) | |
tree | 03f8751cfc59abde72fc3379ebfd14a82d6718d0 /Lib | |
parent | 44028d866336743dc2e3d62daa02d4a8ea318d81 (diff) | |
download | cpython-2933312fe71f15d127009c872cd1e5f98a993999.zip cpython-2933312fe71f15d127009c872cd1e5f98a993999.tar.gz cpython-2933312fe71f15d127009c872cd1e5f98a993999.tar.bz2 |
Fixed issue11082 - Reject str for POST data with a TypeError. Document the need to explicitly encode to bytes when using urlencode.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urllib2.py | 12 | ||||
-rw-r--r-- | Lib/urllib/request.py | 3 |
2 files changed, 11 insertions, 4 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 17323d5..1433670 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -794,6 +794,10 @@ class HandlerTests(unittest.TestCase): http.raise_on_endheaders = True self.assertRaises(urllib.error.URLError, h.do_open, http, req) + # Check for TypeError on POST data which is str. + req = Request("http://example.com/","badpost") + self.assertRaises(TypeError, h.do_request_, req) + # check adding of standard headers o.addheaders = [("Spam", "eggs")] for data in b"", None: # POST, GET @@ -837,10 +841,11 @@ class HandlerTests(unittest.TestCase): else: newreq = h.do_request_(req) - # A file object + # A file object. + # Test only Content-Length attribute of request. - file_obj = io.StringIO() - file_obj.write("Something\nSomething\nSomething\n") + file_obj = io.BytesIO() + file_obj.write(b"Something\nSomething\nSomething\n") for headers in {}, {"Content-Length": 30}: req = Request("http://example.com/", file_obj, headers) @@ -863,7 +868,6 @@ class HandlerTests(unittest.TestCase): newreq = h.do_request_(req) self.assertEqual(int(newreq.get_header('Content-length')),16) - def test_http_doubleslash(self): # Checks the presence of any unnecessary double slash in url does not # break anything. Previously, a double slash directly after the host diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index a188393..dfdbdec 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1048,6 +1048,9 @@ class AbstractHTTPHandler(BaseHandler): if request.data is not None: # POST data = request.data + if isinstance(data, str): + raise TypeError("POST data should be bytes" + " or an iterable of bytes. It cannot be str.") if not request.has_header('Content-type'): request.add_unredirected_header( 'Content-type', |