[New-bugs-announce] [issue17870] Hard to create python longs from arbitrary C integers

Devin Jeanpierre report at bugs.python.org
Mon Apr 29 22:57:05 CEST 2013


New submission from Devin Jeanpierre:

As far as I can tell, the only safe and correct way to convert a (for example) intptr_t to a python long is something akin to the following:

    size_t repsize = sizeof(intmax_t)*8 + 2;
    char i_buf[repsize]; // enough to fit base 2 with sign, let alone base 1
    snprintf(i_buf, repsize, "%ij", (intmax_t) myinteger);
    return PyLong_FromString(i_buf, NULL, 10);

Does this not seem absurd?

PyLong_FromIntMax_t(myinteger) would be great. Or maybe even better would be PyLong_FromBytes(&myinteger, sizeof(myinteger)) ?

This is problematic because many things that can interact with the Python C-API do not use the C types like "long long" or "int". Instead they might use the equivalents of intptr_t and int32_t, which are more reliably sized.

----------
messages: 188103
nosy: Devin Jeanpierre
priority: normal
severity: normal
status: open
title: Hard to create python longs from arbitrary C integers
versions: Python 3.4

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


More information about the New-bugs-announce mailing list