1File Handling 2============= 3 4In PycURL 7.19.0.3 and below, ``CURLOPT_READDATA``, ``CURLOPT_WRITEDATA`` and 5``CURLOPT_WRITEHEADER`` options accepted file objects and directly passed 6the underlying C library ``FILE`` pointers to libcurl. 7 8Python 3 no longer implements files as C library ``FILE`` objects. 9In PycURL 7.19.3 and above, when running on Python 3, these options 10are implemented as calls to ``CURLOPT_READFUNCTION``, ``CURLOPT_WRITEFUNCTION`` 11and ``CURLOPT_HEADERFUNCTION``, respectively, with the write method of the 12Python file object as the parameter. As a result, any Python file-like 13object implementing a ``read`` method can be passed to ``CURLOPT_READDATA``, 14and any Python file-like object implementing a ``write`` method can be 15passed to ``CURLOPT_WRITEDATA`` or ``CURLOPT_WRITEHEADER`` options. 16 17When running PycURL 7.19.3 and above on Python 2, the old behavior of 18passing ``FILE`` pointers to libcurl remains when a true file object is given 19to ``CURLOPT_READDATA``, ``CURLOPT_WRITEDATA`` and ``CURLOPT_WRITEHEADER`` 20options. For consistency with Python 3 behavior these options also accept 21file-like objects implementing a ``read`` or ``write`` method, as appropriate, 22as arguments, in which case the Python 3 code path is used converting these 23options to ``CURLOPT_*FUNCTION`` option calls. 24 25Files given to PycURL as arguments to ``CURLOPT_READDATA``, 26``CURLOPT_WRITEDATA`` or ``CURLOPT_WRITEHEADER`` must be opened for reading or 27writing in binary mode. Files opened in text mode (without ``"b"`` flag to 28``open()``) expect string objects and reading from or writing to them from 29PycURL will fail. Similarly when passing ``f.write`` method of an open file to 30``CURLOPT_WRITEFUNCTION`` or ``CURLOPT_HEADERFUNCTION``, or ``f.read`` to 31``CURLOPT_READFUNCTION``, the file must have been be opened in binary mode. 32