From 87684e6eeab154bf7d8775b767e2558ddf659745 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Wed, 14 Mar 2012 18:08:13 -0700 Subject: [PATCH 1/2] Fix the wrong urllib exampls which use str for POST data. Closes Issue11261 --- Doc/howto/urllib2.rst | 4 +++- Doc/library/urllib.request.rst | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst index 76286bdc273..567c1b18204 100644 --- a/Doc/howto/urllib2.rst +++ b/Doc/howto/urllib2.rst @@ -108,6 +108,7 @@ library. :: 'language' : 'Python' } data = urllib.parse.urlencode(values) + data = data.encode('utf-8') # data should be bytes req = urllib.request.Request(url, data) response = urllib.request.urlopen(req) the_page = response.read() @@ -172,7 +173,8 @@ Explorer [#]_. :: 'language' : 'Python' } headers = { 'User-Agent' : user_agent } - data = urllib.parse.urlencode(values) + data = urllib.parse.urlencode(values) + data = data.encode('utf-8') req = urllib.request.Request(url, data, headers) response = urllib.request.urlopen(req) the_page = response.read() diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index b51ce3f3d8a..24e24443ab2 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -138,14 +138,13 @@ The following classes are provided: *url* should be a string containing a valid URL. - *data* may be a string specifying additional data to send to the - server, or ``None`` if no such data is needed. Currently HTTP - requests are the only ones that use *data*; the HTTP request will - be a POST instead of a GET when the *data* parameter is provided. - *data* should be a buffer in the standard - :mimetype:`application/x-www-form-urlencoded` format. The - :func:`urllib.parse.urlencode` function takes a mapping or sequence - of 2-tuples and returns a string in this format. + *data* may be a bytes object specifying additional data to send to the + server, or ``None`` if no such data is needed. Currently HTTP requests are + the only ones that use *data*; the HTTP request will be a POST instead of a + GET when the *data* parameter is provided. *data* should be a buffer in the + standard :mimetype:`application/x-www-form-urlencoded` format. The + :func:`urllib.parse.urlencode` function takes a mapping or sequence of + 2-tuples and returns a string in this format. *headers* should be a dictionary, and will be treated as if :meth:`add_header` was called with each key and value as arguments. @@ -1122,10 +1121,10 @@ some point in the future. size in response to a retrieval request. If the *url* uses the :file:`http:` scheme identifier, the optional *data* - argument may be given to specify a ``POST`` request (normally the request type - is ``GET``). The *data* argument must in standard - :mimetype:`application/x-www-form-urlencoded` format; see the :func:`urlencode` - function below. + argument may be given to specify a ``POST`` request (normally the request + type is ``GET``). The *data* argument must be a bytes object in standard + :mimetype:`application/x-www-form-urlencoded` format; see the + :func:`urlencode` function below. :func:`urlretrieve` will raise :exc:`ContentTooShortError` when it detects that the amount of data available was less than the expected amount (which is the From 2522771e4707cea379510d3196d877c92408f6d3 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Wed, 14 Mar 2012 18:10:37 -0700 Subject: [PATCH 2/2] Fixes Issue 14234: fix for the previous commit, keep compilation when using --with-system-expat working when the system expat does not have salted hash support. --- Modules/expat/expat.h | 2 ++ Modules/pyexpat.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Modules/expat/expat.h b/Modules/expat/expat.h index cffdb8f6299..89646d2bcee 100644 --- a/Modules/expat/expat.h +++ b/Modules/expat/expat.h @@ -892,6 +892,8 @@ XMLPARSEAPI(int) XML_SetHashSalt(XML_Parser parser, unsigned long hash_salt); +#define XML_HAS_SET_HASH_SALT /* Python Only: Defined for pyexpat.c. */ + /* If XML_Parse or XML_ParseBuffer have returned XML_STATUS_ERROR, then XML_GetErrorCode returns information about the error. */ diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index c1142de54dc..bf81e2af01a 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1150,8 +1150,13 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern) else { self->itself = XML_ParserCreate(encoding); } +#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT) + /* This feature was added upstream in libexpat 2.1.0. Our expat copy + * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT + * to indicate that we can still use it. */ XML_SetHashSalt(self->itself, (unsigned long)_Py_HashSecret.prefix); +#endif self->intern = intern; Py_XINCREF(self->intern); PyObject_GC_Track(self);