Skip to content

LHAPDF Build failure with python 3.10

When building with python 3.10.2, there is the following build failure:

     464          |       ^~~~~~~~~
  >> 465    /opt/spackages/spack/opt/spack/linux-archrolling-broadwell/gcc-11.1.0/python-3.10.2-gbsljatawseh7fzcmy75jx5lbs5ak3jp/include/python3.10/object.h:133:33: error: lvalue required as decrement operand
     466      133 | #define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST_CONST(ob))
     467          |                       ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
     468    lhapdf.cpp:19141:7: note: in expansion of macro 'Py_REFCNT'
     469    19141 |     --Py_REFCNT(o);
     470          |       ^~~~~~~~~

which is due to this change in python 3.10: https://github.com/python/cpython/pull/20429

Looking at the docs, this should be fixable by using the new Py_SET_REFCNT macro in wrappers/python/lhapdf.cpp, probably with a replacement like:

s/++Py_REFCNT(o)/Py_SET_REFCNT(o, Py_REFCNT(o)+1)/
s/--Py_REFCNT(o)/Py_SET_REFCNT(o, Py_REFCNT(o)-1)/

but will need some check for the python version.