cpyext: PyString_AsString() doesn't work

Create issue
Issue #2499 closed
Victor Stinner created an issue

The following fuction returns '\x01\x02' on CPytohn, but '\x00\x00' on PyPy.

static PyObject *py_apply_delta(PyObject *self, PyObject *args)
{
    uint8_t *out;
    PyObject *ret;

    ret = PyString_FromStringAndSize(NULL, 2);
    if (ret == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    out = (uint8_t *)PyString_AsString(ret);
        out[0] = 1;
        out[1] = 2;
        return ret;
}

If you replace PyString_AsString() with PyString_AS_STRING(), it works as expected.

It seems like PyString_AS_STRING() behaves differently.

Comments (3)

  1. Victor Stinner reporter

    Oh, I forgot to mention that I'm using Fedora 25 with PyPy 5.4:

    Python 2.7.10 (77392ad263504df011ccfcabf6a62e21d04086d0, Sep 12 2016, 15:21:10)
    [PyPy 5.4.0 with GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    

    I found this bug while working on a dulwich bug: https://github.com/jelmer/dulwich/issues/509

  2. Log in to comment