| 1 |
.. _unicode: |
| 2 |
|
| 3 |
String And Unicode Handling |
| 4 |
=========================== |
| 5 |
|
| 6 |
Generally speaking, libcurl does not perform data encoding or decoding. |
| 7 |
In particular, libcurl is not Unicode-aware, but operates on byte streams. |
| 8 |
libcurl leaves it up to the application - PycURL library or an application |
| 9 |
using PycURL in this case - to encode and decode Unicode data into byte streams. |
| 10 |
|
| 11 |
PycURL, being a thin wrapper around libcurl, generally does not perform |
| 12 |
this encoding and decoding either, leaving it up to the application. |
| 13 |
Specifically: |
| 14 |
|
| 15 |
- Data that PycURL passes to an application, such as via callback functions, |
| 16 |
is normally byte strings. The application must decode them to obtain text |
| 17 |
(Unicode) data. |
| 18 |
- Data that an application passes to PycURL, such as via ``setopt`` calls, |
| 19 |
must normally be byte strings appropriately encoded. For convenience and |
| 20 |
compatibility with existing code, PycURL will accept Unicode strings that |
| 21 |
contain ASCII code points only [#ascii]_, and transparently encode these to |
| 22 |
byte strings. |
| 23 |
|
| 24 |
Why doesn't PycURL automatically encode and decode, say, HTTP request or |
| 25 |
response data? The key to remember is that libcurl supports over 20 protocols, |
| 26 |
and PycURL generally has no knowledge of what protocol is being used by |
| 27 |
a particular request as PycURL does not track application state. Having |
| 28 |
to manually encode and decode data is unfortunately the price of libcurl's |
| 29 |
flexibility. |
| 30 |
|
| 31 |
|
| 32 |
Setting Options |
| 33 |
--------------- |
| 34 |
|
| 35 |
The ``bytes`` type holds arbitrary encoded byte strings. |
| 36 |
PycURL will accept ``bytes`` values for all options where libcurl specifies |
| 37 |
a "string" argument:: |
| 38 |
|
| 39 |
>>> import pycurl |
| 40 |
>>> c = pycurl.Curl() |
| 41 |
>>> c.setopt(c.USERAGENT, b'Foo\xa9') |
| 42 |
# ok |
| 43 |
|
| 44 |
The ``str`` type holds Unicode data. PycURL will accept ``str`` values |
| 45 |
containing ASCII code points only:: |
| 46 |
|
| 47 |
>>> c.setopt(c.USERAGENT, 'Foo') |
| 48 |
# ok |
| 49 |
|
| 50 |
>>> c.setopt(c.USERAGENT, 'Foo\xa9') |
| 51 |
Traceback (most recent call last): |
| 52 |
File "<stdin>", line 1, in <module> |
| 53 |
UnicodeEncodeError: 'ascii' codec can't encode character '\xa9' in position 3: ordinal not in range(128) |
| 54 |
|
| 55 |
>>> c.setopt(c.USERAGENT, 'Foo\xa9'.encode('iso-8859-1')) |
| 56 |
# ok |
| 57 |
|
| 58 |
|
| 59 |
Writing To Files |
| 60 |
---------------- |
| 61 |
|
| 62 |
PycURL returns all data read from the network as byte strings, |
| 63 |
meaning write callbacks will receive ``bytes`` objects. |
| 64 |
|
| 65 |
When using e.g. ``WRITEDATA`` or ``WRITEFUNCTION`` options, |
| 66 |
files being written to must be opened in binary mode for the writes to work:: |
| 67 |
|
| 68 |
import pycurl |
| 69 |
c = pycurl.Curl() |
| 70 |
c.setopt(c.URL,'http://pycurl.io') |
| 71 |
# File opened in binary mode. |
| 72 |
with open('/dev/null','wb') as f: |
| 73 |
c.setopt(c.WRITEDATA, f) |
| 74 |
# Same result if using WRITEFUNCTION instead: |
| 75 |
#c.setopt(c.WRITEFUNCTION, f.write) |
| 76 |
c.perform() |
| 77 |
# ok |
| 78 |
|
| 79 |
If a file is opened in text mode (``w`` instead of ``wb`` mode), an error |
| 80 |
similar to the following will result:: |
| 81 |
|
| 82 |
TypeError: must be str, not bytes |
| 83 |
Traceback (most recent call last): |
| 84 |
File "/tmp/test.py", line 8, in <module> |
| 85 |
c.perform() |
| 86 |
pycurl.error: (23, 'Failed writing body (0 != 168)') |
| 87 |
|
| 88 |
The TypeError is actually an exception raised by Python which will be printed, |
| 89 |
but not propagated, by PycURL. PycURL will raise a ``pycurl.error`` to |
| 90 |
signify operation failure. |
| 91 |
|
| 92 |
|
| 93 |
Writing To BytesIO |
| 94 |
------------------ |
| 95 |
|
| 96 |
As PycURL invokes the write callback with ``bytes`` argument, |
| 97 |
the response must be written to a ``BytesIO`` object:: |
| 98 |
|
| 99 |
import pycurl |
| 100 |
from io import BytesIO |
| 101 |
c = pycurl.Curl() |
| 102 |
c.setopt(c.URL,'http://pycurl.io') |
| 103 |
buffer = BytesIO() |
| 104 |
c.setopt(c.WRITEDATA, buffer) |
| 105 |
# Same result if using WRITEFUNCTION instead: |
| 106 |
#c.setopt(c.WRITEFUNCTION, buffer.write) |
| 107 |
c.perform() |
| 108 |
# ok |
| 109 |
|
| 110 |
Attempting to use a ``StringIO`` object will produce an error:: |
| 111 |
|
| 112 |
import pycurl |
| 113 |
from io import StringIO |
| 114 |
c = pycurl.Curl() |
| 115 |
c.setopt(c.URL,'http://pycurl.io') |
| 116 |
buffer = StringIO() |
| 117 |
c.setopt(c.WRITEDATA, buffer) |
| 118 |
c.perform() |
| 119 |
|
| 120 |
TypeError: string argument expected, got 'bytes' |
| 121 |
Traceback (most recent call last): |
| 122 |
File "/tmp/test.py", line 9, in <module> |
| 123 |
c.perform() |
| 124 |
pycurl.error: (23, 'Failed writing body (0 != 168)') |
| 125 |
|
| 126 |
To decode the response body:: |
| 127 |
|
| 128 |
import pycurl |
| 129 |
from io import BytesIO |
| 130 |
c = pycurl.Curl() |
| 131 |
c.setopt(c.URL,'http://pycurl.io') |
| 132 |
buffer = BytesIO() |
| 133 |
c.setopt(c.WRITEDATA, buffer) |
| 134 |
c.perform() |
| 135 |
# ok |
| 136 |
# Decode the response body: |
| 137 |
string_body = buffer.getvalue().decode('utf-8') |
| 138 |
|
| 139 |
|
| 140 |
Header Functions |
| 141 |
---------------- |
| 142 |
|
| 143 |
Although headers are often ASCII text, they are still returned as |
| 144 |
``bytes`` instances and thus require appropriate decoding. |
| 145 |
HTTP headers are encoded in ISO/IEC 8859-1 according to the standards. |
| 146 |
|
| 147 |
When using ``WRITEHEADER`` option to write headers to files, the files |
| 148 |
must be opened in binary mode, same as with ``WRITEDATA``. |
| 149 |
|
| 150 |
|
| 151 |
Read Functions |
| 152 |
-------------- |
| 153 |
|
| 154 |
Read functions are expected to provide data in the same fashion as |
| 155 |
string options expect it: |
| 156 |
|
| 157 |
- The data can be given as ``bytes`` instances. |
| 158 |
- The data can be given as ``str`` instances containing |
| 159 |
ASCII code points only. |
| 160 |
|
| 161 |
Caution: when using CURLOPT_READFUNCTION in tandem with CURLOPT_POSTFIELDSIZE, |
| 162 |
as would be done for HTTP for example, take care to pass the length of |
| 163 |
*encoded* data to CURLOPT_POSTFIELDSIZE if you are performing the encoding. |
| 164 |
If you pass the number of Unicode characters rather than |
| 165 |
encoded bytes to libcurl, the server will receive wrong Content-Length. |
| 166 |
Alternatively you can return Unicode strings from a CURLOPT_READFUNCTION |
| 167 |
function, if your data contains only ASCII code points, |
| 168 |
and let PycURL encode them for you. |
| 169 |
|
| 170 |
|
| 171 |
How PycURL Handles Unicode Strings |
| 172 |
---------------------------------- |
| 173 |
|
| 174 |
If PycURL is given a Unicode string which contains non-ASCII code points, |
| 175 |
and as such cannot be encoded to ASCII, PycURL will return an error to libcurl, |
| 176 |
and libcurl in turn will fail the request with an error like |
| 177 |
"read function error/data error". PycURL will then raise ``pycurl.error`` |
| 178 |
with this latter message. The encoding exception that was the |
| 179 |
underlying cause of the problem is stored as ``sys.last_value``. |
| 180 |
|
| 181 |
|
| 182 |
Figuring Out Correct Encoding |
| 183 |
----------------------------- |
| 184 |
|
| 185 |
What encoding should be used when is a complicated question. For example, |
| 186 |
when working with HTTP: |
| 187 |
|
| 188 |
- URLs and POSTFIELDS data must be URL-encoded. A URL-encoded string has |
| 189 |
only ASCII code points. |
| 190 |
- Headers must be ISO/IEC 8859-1 encoded. |
| 191 |
- Encoding for bodies is specified in Content-Type and Content-Encoding headers. |
| 192 |
|
| 193 |
|
| 194 |
Legacy PycURL Versions |
| 195 |
---------------------- |
| 196 |
|
| 197 |
The Unicode handling documented here was implemented in PycURL 7.19.3 |
| 198 |
along with Python 3 support. Prior to PycURL 7.19.3 Unicode data was not |
| 199 |
accepted at all:: |
| 200 |
|
| 201 |
>>> import pycurl |
| 202 |
>>> c = pycurl.Curl() |
| 203 |
>>> c.setopt(c.USERAGENT, u'Foo\xa9') |
| 204 |
Traceback (most recent call last): |
| 205 |
File "<stdin>", line 1, in <module> |
| 206 |
TypeError: invalid arguments to setopt |
| 207 |
|
| 208 |
Some GNU/Linux distributions provided Python 3 packages of PycURL prior to |
| 209 |
PycURL 7.19.3. These packages included unofficial patches |
| 210 |
([#patch1]_, [#patch2]_) which did not handle Unicode correctly, and did not behave |
| 211 |
as described in this document. Such unofficial versions of PycURL should |
| 212 |
be avoided. |
| 213 |
|
| 214 |
|
| 215 |
.. rubric:: Footnotes |
| 216 |
|
| 217 |
.. [#ascii] Only ASCII is accepted; ISO-8859-1/Latin 1, for example, will be |
| 218 |
rejected. |
| 219 |
.. [#patch1] http://sourceforge.net/p/pycurl/patches/5/ |
| 220 |
.. [#patch2] http://sourceforge.net/p/pycurl/patches/12/ |