curl-and-python

Re: postfields and unicode objects

From: <johansen_at_sun.com>
Date: Thu, 13 Aug 2009 09:27:23 -0700

On Thu, Aug 13, 2009 at 10:55:40AM -0400, Seth Vidal wrote:
> Hi,
> using pycurl 7.19.0 and curl 7.19.4. I've found if I submit a unicode
> object to the postfields option using setopt() that I get a type error
>
> TypeError: invalid arguments to setopt
>
> if I coerce the unicode object to a string using str(obj) then it works
> but that coercion will sometimes fail due to unicode decode problems.
>
> Now, my question: is there any reason this HAS to be a string object and
> can't be a unicode object and if so should I file it as an issue with
> pycurl?

It looks like unicode should work.

Based upon this description, either the object isn't a unicode object
that's a subclass of string, or you've found a bug in the Python C API.

Here's the relevant bit of pycurl.c (1635-1647):

        case CURLOPT_POSTFIELDS:
            if (PyString_AsStringAndSize(obj, &str, &len) != 0)
                return NULL;
            /* automatically set POSTFIELDSIZE */
            if (len <= INT_MAX) {
                res = curl_easy_setopt(self->handle, CURLOPT_POSTFIELDSIZE, (lon
            } else {
                res = curl_easy_setopt(self->handle, CURLOPT_POSTFIELDSIZE_LARGE
            }
            if (res != CURLE_OK) {
                CURLERROR_RETVAL();
            }
            break;

The only place this code is checking types is in the if statement.

At least for python 2.4.4, the C API says this about
PyString_AsStringAndSize():

        int PyString_AsStringAndSize(PyObject *obj, char **buffer, int *length)

        Return a NUL-terminated representation of the contents of the
        object obj through the output variables buffer and length.

        The function accepts both string and Unicode objects as input.
        For Unicode objects it returns the default encoded version of
        the object. If length is NULL, the resulting buffer may not
        contain NUL characters; if it does, the function returns -1 and
        a TypeError is raised.

        The buffer refers to an internal string buffer of obj, not a
        copy. The data must not be modified in any way, unless the
        string was just created using PyString_FromStringAndSize(NULL,
        size). It must not be deallocated. If string is a Unicode
        object, this function computes the default encoding of string
        and operates on that. If string is not a string object at all,
        PyString_AsStringAndSize() returns -1 and raises TypeError.

        (http://www.python.org/doc/2.4.4/api/stringObjects.html)

I'd double-check your arguments, just to make sure. If it is a unicode
object, this might be a Python bug.

HTH,

-j
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2009-08-13