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 <rep...@bugs.python.org>
<http://bugs.python.org/issue17870>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to