0

I am trying to pass a (large) integer from python to an extension module, but I am unable to parse pythons arbitrary precision integers to 256-bit unsigned integers uint256. Here is the C callee:

#include <Python.h>

typedef unsigned _ExtInt(256) uint256;

static PyObject* test(PyObject* self, PyObject* args)
{
    uint256 x;
    if(!PyArg_ParseTuple(args, "O", &x)) {
        puts("Could not parse the python arg");
        return NULL;
    }

    // simple addition
    x += (uint256) 1;    

    return Py_BuildValue("O", x);
}

// ... initalize extension module here ...

In python I run something like

import extension_module
extension_module.test(1)

And I get the error:

Bus error: 10

Or

Segmentation fault: 11

However, if I remove the simple addition x += (uint256) 1; it will atleast not throw any error and return the argument.


How do I parse extended-integer types in my C extension module?

Kevin
  • 3,096
  • 2
  • 8
  • 37
  • 1
    There is no support for extended integer types in cpython. You will need to write a converter from longobject to the desired representation. – ead Jul 03 '21 at 03:49
  • @ead Thanks, do you have any reference for writing my own converter? Is longobject the same as pythons arbitrary position integer? I read somewhere that python represents integers as ```bignum```https://levelup.gitconnected.com/how-python-represents-integers-using-bignum-f8f0574d0d6b – Kevin Jul 03 '21 at 08:08

0 Answers0