PyBool_Check and explicit link does not return expected value

I try to load python3.dll and python3XX.dll explicitly.
Wrapper works for functions but import variable as PyBool_Type seems to not work (or I miss something)
Here part of code
PyBool_Check(obj) returns always false :frowning:


    PyTypeObject* boolType = reinterpret_cast<PyTypeObject*>(
        Nelson::get_function(python3_handle, "PyBool_Type"));
    PyObject* obj = PyBool_FromLongPtr(1);

    PyBool_Type = *boolType;
#define PyBool_Check(x) Py_IS_TYPE((x), &PyBool_Type)

    if (PyBool_Check(obj)) { 
      return true;
    } else {
      return false;

Did you try using the boolType pointer directly, rather than implicitly copying the PyTypeObject and referencing the copy? I wouldn’t be surprised if Py_IS_TYPE is implemented in terms of the pointer address. (Also, I don’t see a PyBool_FromLongPtr in the API, and you’re apparently passing a number, not a pointer. Shouldn’t that be PyBool_FromLong?) (Also, since you have “Nelson” in your username, I assume that Nelson::get_function is your own code in your own namespace. How should we be able to verify what it does?)

Thanks for you help!

if I replaces

#define PyBool_Check(x) Py_IS_TYPE((x), &boolType)


#define PyBool_Check(x) Py_IS_TYPE((x), boolType)

PyBool_Check returns 1 and not 0 when PyObject * is a boolean type

Any tips to not redefine PyBool_Check and use default in this case ?

My current “ugly” workaround is to define

    #define PyBool_Type *boolType

Yep, I agree, that’s ugly. But since you’re not really working inside the same system anyway, it’s not really necessary to bind yourself to the same #define-based API. What you really want is to simply define PyBool_Check in a correct way for your situation. (I’m a tad confused by PyBool_FromLongPtr, since that function doesn’t exist, only PyBool_FromLong - there’s no way that I’m aware of to construct a bool from a pointer. Anyhow.) So I’d be inclined to replace PyBool_Check with an actual inline function, or possibly just call PyObject_IsInstance directly (since that’s part of the Stable ABI). If you really truly need the performance, an inline PyBool_Check that does a simple pointer comparison between Py_TYPE(obj) and the saved pointer to the bool type should suffice.