Index: Doc/library/urllib.parse.rst =================================================================== --- Doc/library/urllib.parse.rst (revision 88386) +++ Doc/library/urllib.parse.rst (working copy) @@ -506,9 +506,9 @@ .. function:: urlencode(query, doseq=False, safe='', encoding=None, errors=None) Convert a mapping object or a sequence of two-element tuples, which may - either be a :class:`str` or a :class:`bytes`, to a "percent-encoded" string, - suitable to pass to :func:`urlopen` above as the optional *data* argument. - This is useful to pass a dictionary of form fields to a ``POST`` request. + either be a :class:`str` or a :class:`bytes`, to a "percent-encoded" string. + The resultant string must be converted to bytes before it is passed to + :func:`urlopen` above as the optional *data* argument. The resulting string is a series of ``key=value`` pairs separated by ``'&'`` characters, where both *key* and *value* are quoted using :func:`quote_plus` above. When a sequence of two-element tuples is used as the *query* Index: Lib/urllib/request.py =================================================================== --- Lib/urllib/request.py (revision 88386) +++ Lib/urllib/request.py (working copy) @@ -1048,6 +1048,8 @@ if request.data is not None: # POST data = request.data + if isinstance(data, str): + raise ValueError("POST data should be bytes") if not request.has_header('Content-type'): request.add_unredirected_header( 'Content-type', Index: Lib/test/test_urllib2.py =================================================================== --- Lib/test/test_urllib2.py (revision 88386) +++ Lib/test/test_urllib2.py (working copy) @@ -794,6 +794,10 @@ http.raise_on_endheaders = True self.assertRaises(urllib.error.URLError, h.do_open, http, req) + # Check for ValueError on Bad POST data. + req = Request("http://example.com/","badpost") + self.assertRaises(ValueError, h.do_request_, req) + # check adding of standard headers o.addheaders = [("Spam", "eggs")] for data in b"", None: # POST, GET @@ -837,10 +841,11 @@ 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 @@ 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