Pycxx Support

In Src/Python3/cxx_extensions.cxx

PythonType &PythonType::supportClass()
{
    table->tp_flags |= Py_TPFLAGS_BASETYPE;
    return *this;
}

#ifdef PYCXX_PYTHON_2TO3
PythonType &PythonType::supportPrint()
{
    table->tp_print = print_handler;
    return *this;
}
#endif

PythonType &PythonType::supportGetattr()
{
    table->tp_getattr = getattr_handler;
    return *this;
}

PythonType &PythonType::supportSetattr()
{
    table->tp_setattr = setattr_handler;
    return *this;
}

PythonType &PythonType::supportGetattro()
{
    table->tp_getattro = getattro_handler;
    return *this;
}

PythonType &PythonType::supportSetattro()
{
    table->tp_setattro = setattro_handler;
    return *this;
}

#ifdef PYCXX_PYTHON_2TO3
PythonType &PythonType::supportCompare( void )
{
    return *this;
}
#endif

PythonType &PythonType::supportRichCompare()
{
    table->tp_richcompare = rich_compare_handler;
    return *this;
}

PythonType &PythonType::supportRepr()
{
    table->tp_repr = repr_handler;
    return *this;
}

PythonType &PythonType::supportStr()
{
    table->tp_str = str_handler;
    return *this;
}

PythonType &PythonType::supportHash()
{
    table->tp_hash = hash_handler;
    return *this;
}

PythonType &PythonType::supportCall()
{
    table->tp_call = call_handler;
    return *this;
}

PythonType &PythonType::supportIter()
{
    table->tp_iter = iter_handler;
    table->tp_iternext = iternext_handler;
    return *this;
}

Again, this can be macro-d;

#define SUPPORT( _Foo_, _statement_ ) \
    PythonType &PythonType::support##_Foo_() \
    { \
        _statement_ \
        return *this; \
    }

SUPPORT( Class,         table->tp_flags |= Py_TPFLAGS_BASETYPE; )

#ifdef PYCXX_PYTHON_2TO3
SUPPORT( Print,         table->tp_print = print_handler; )
#endif

SUPPORT( Getattr,       table->tp_getattr       = getattr_handler; )
SUPPORT( Setattr,       table->tp_setattr       = setattr_handler; )
SUPPORT( Getattro,      table->tp_getattro      = getattro_handler; )
SUPPORT( Setattro,      table->tp_setattro      = setattro_handler; )

#ifdef PYCXX_PYTHON_2TO3
SUPPORT( Compare, )
#endif

SUPPORT( RichCompare,   table->tp_richcompare   = rich_compare_handler; )
SUPPORT( Repr,          table->tp_repr          = repr_handler; )
SUPPORT( Str,           table->tp_str           = str_handler; )
SUPPORT( Hash,          table->tp_hash          = hash_handler; )
SUPPORT( Call,          table->tp_call          = call_handler; )
SUPPORT( Iter,          table->tp_iter          = iter_handler;
                        table->tp_iternext      = iternext_handler; )
#undef SUPPORT

Actually this is bad practice because if you come across 'supportClass' being used somewhere and try to find its definition, the IDE won't be able to find it, as the string 'supportClass' doesn't exist until the preprocessor assembles it.

So it's better to do:

#define S( _Foo_, _statement_ ) \
    PythonType &PythonType::_Foo_() \
    { \
        _statement_ \
        return *this; \
    }

S( supportClass,         table->tp_flags |= Py_TPFLAGS_BASETYPE; )
#ifdef PYCXX_PYTHON_2TO3
S( supportPrint,         table->tp_print = print_handler; )
#endif

S( supportGetattr,       table->tp_getattr       = getattr_handler; )
S( supportSetattr,       table->tp_setattr       = setattr_handler; )
S( supportGetattro,      table->tp_getattro      = getattro_handler; )
S( supportSetattro,      table->tp_setattro      = setattro_handler; )
#ifdef PYCXX_PYTHON_2TO3
S( supportCompare, )
#endif
S( supportRichCompare,   table->tp_richcompare   = rich_compare_handler; )
S( supportRepr,          table->tp_repr          = repr_handler; )
S( supportStr,           table->tp_str           = str_handler; )
S( supportHash,          table->tp_hash          = hash_handler; )
S( supportCall,          table->tp_call          = call_handler; )
S( supportIter,          table->tp_iter          = iter_handler;
                        table->tp_iternext      = iternext_handler; )
#undef S
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License