[issue17870] Hard to create python longs from arbitrary C integers

STINNER Victor report at bugs.python.org
Tue Apr 30 00:00:07 CEST 2013


STINNER Victor added the comment:

"PyLong_FromVoidPtr works for uintptr_t, but not intptr_t."

Ok correct. You should use something like:

PyObject*
PyLong_FromIntptr_t(intptr_t value)
{
    if (sizeof(intptr_t) == sizeof(long))
        return PyLong_FromLong(value);
    else {
        assert(sizeof(intptr_t) <= sizeof(PY_LONG_LONG));
        return PyLong_FromLongLong((PY_LONG_LONG)value);
    }
}

The "if (sizeof(intptr_t) == sizeof(long))" should be optimized by your compiler.

I don't know if Python should provide such function. What is your use case for intptr_t?

It looks like intptr_t is only really used in the implementation of os.spawnv() and os.spawnve(). Extract:

#if SIZEOF_LONG == SIZEOF_VOID_P
        return Py_BuildValue("l", (long) spawnval);
#else
        return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
#endif

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17870>
_______________________________________


More information about the Python-bugs-list mailing list