cppyy

error: use of undeclared identifier 'LP_c_uint'


I am using cppyy in my project to call C APIs.

I get below error log captured by capfd plugin in pytest, when an exception happens:

input_line_33:2:11: error: use of undeclared identifier 'LP_c_uint'
 (sizeof (LP_c_uint))

It is coming from below code block, specifically logger.error() call:

try:
....
except Exception as e:
    out, err = capfd.readouterr()
    if err:
        logger.error(err)

Now, if I grep my python source code, I don't any hits to 'LP_c_uint'. Any pointers on how to debug this (like what may be causing this)?

Edit:

simple reproducer:

from ctypes import c_uint32, pointer, byref
import cppyy

from cppyy import sizeof
cppyy.cppdef(
    """
    void func(uint32_t *param) {
        std::cout << "param: " << *param << std::endl;
    }
    """
)

if __name__ == "__main__":
    param = pointer(c_uint32(17))
    cppyy.gbl.func(param)
    print(sizeof(param))

Output:

param: 17
input_line_22:2:11: error: use of undeclared identifier 'LP_c_uint'
 (sizeof (LP_c_uint))
          ^
0

Solution

  • Yes, using ctypes.sizeof would solve it. I see the need for using ctypes.c_uint32 here (although using ctypes.pointer is not necessary) as there is no way of creating a pointer type from cppyy.gbl.uint32_t (which is basically Python's int, so internals to take a pointer to are not exposed).

    cppyy.sizeof is now changed to forward to ctypes.sizeof as necessary. That'll make your code work and will be part of next release. As a current workaround, you can do something similar. For example:

    def my_sizeof(tt):
        try:
            return ctypes.sizeof(tt)
        except TypeError:
            pass
        return cppyy.sizeof(tt)