| 1 |
File Handling |
| 2 |
============= |
| 3 |
|
| 4 |
In PycURL 7.19.0.3 and below, ``CURLOPT_READDATA``, ``CURLOPT_WRITEDATA`` and |
| 5 |
``CURLOPT_WRITEHEADER`` options accepted file objects and directly passed |
| 6 |
the underlying C library ``FILE`` pointers to libcurl. |
| 7 |
|
| 8 |
Python 3 no longer implements files as C library ``FILE`` objects. |
| 9 |
In PycURL 7.19.3 and above, when running on Python 3, these options |
| 10 |
are implemented as calls to ``CURLOPT_READFUNCTION``, ``CURLOPT_WRITEFUNCTION`` |
| 11 |
and ``CURLOPT_HEADERFUNCTION``, respectively, with the write method of the |
| 12 |
Python file object as the parameter. As a result, any Python file-like |
| 13 |
object implementing a ``read`` method can be passed to ``CURLOPT_READDATA``, |
| 14 |
and any Python file-like object implementing a ``write`` method can be |
| 15 |
passed to ``CURLOPT_WRITEDATA`` or ``CURLOPT_WRITEHEADER`` options. |
| 16 |
|
| 17 |
Files given to PycURL as arguments to ``CURLOPT_READDATA``, |
| 18 |
``CURLOPT_WRITEDATA`` or ``CURLOPT_WRITEHEADER`` must be opened for reading or |
| 19 |
writing in binary mode. Files opened in text mode (without ``"b"`` flag to |
| 20 |
``open()``) expect string objects and reading from or writing to them from |
| 21 |
PycURL will fail. Similarly when passing ``f.write`` method of an open file to |
| 22 |
``CURLOPT_WRITEFUNCTION`` or ``CURLOPT_HEADERFUNCTION``, or ``f.read`` to |
| 23 |
``CURLOPT_READFUNCTION``, the file must have been be opened in binary mode. |