passing uint64_t between python and c++

J

Jhy-Chun Wang

Hi,

I have a c++ extension that does thing like:

extern "C"
static PyObject *
nasUtil_access_cmd(PyObject *self, PyObject *args)
{
uint64_t paddr;
uint64_t data;


if (!PyArg_ParseTuple(args, (char*)"LL", &paddr, &data)) {
return NULL;
}
...
}

When I pass in a value larger than 0x7fffffff-ffffffffL from python
side, I always get "OverflowError: long too big to convert". I tried
using "int64_t paddr; int64_t data;", got the same error. How do I get
around this? I am using 32bit python 2.3.3 on Solaris 9.

Any help is very appreciated.

Thanks,

Jhychun
 
F

Fredrik Lundh

Jhy-Chun Wang said:
I have a c++ extension that does thing like:

extern "C"
static PyObject *
nasUtil_access_cmd(PyObject *self, PyObject *args)
{
uint64_t paddr;
uint64_t data;

if (!PyArg_ParseTuple(args, (char*)"LL", &paddr, &data)) {
return NULL;
}
...
}

from the documentation:

http://www.python.org/doc/current/api/arg-parsing.html

"L" (integer) [PY_LONG_LONG]
Convert a Python integer to a C long long. This format is only available
on platforms that support long long (or _int64 on Windows).

"K" (integer) [unsigned PY_LONG_LONG]
Convert a Python integer to a C unsigned long long without overflow
checking. This format is only available on platforms that support
unsigned long long (or unsigned _int64 on Windows). New in
version 2.3.

in other words, you should use "K" instead of "L", and you should use an
"unsigned PY_LONG_LONG" to hold the value (this may or may not be
the same thing as uint64_t, but why write non-portable code if you don't
have to)

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top