INCF / MUSIC

MUSIC, the MUltiSimulation Coordinator
GNU General Public License v3.0
37 stars 37 forks source link

Cython 3 compatbility #77

Open sanjayankur31 opened 1 year ago

sanjayankur31 commented 1 year ago

Cython 3 has been released, and the MUSIC sources need to be updated to work with it. Examples of errors when trying to build MUSIC with Cython 3:

warning: ./pyconfig.pxi:1:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
warning: music/pybuffer.pxd:8:0: The 'IF' statement is deprecated and will be removed in a future Cython version. Consider using runtime conditions or C macros instead. See https://github.com/cython/cython/issues/4310
make[1]: *** [Makefile:444: all-recursive] Error 1
make: *** [Makefile:372: all] Error 2

Here's the migration guide: https://cython.readthedocs.io/en/latest/src/userguide/migrating_to_cy30.html

hroncok commented 10 months ago

In the Fedora RPM package I see:

Error compiling Cython file:
------------------------------------------------------------
...
            raise TypeError("object does not present buffer interface")

        cdef Py_buffer* pybuf = &self.pybuf
        PyObject_GetBuffer(data, pybuf, bufflags)
        self.dtype = TypeDict[getformat(pybuf)]
        self.items = pybuf.len / pybuf.itemsize
                               ^
------------------------------------------------------------

pybuffer.pyx:63:31: Cannot assign type 'double' to 'Py_ssize_t'

Cython 3 assumes Python 3 / semantics. This should be fixable by using //. Let me have a look.

hroncok commented 10 months ago

https://github.com/INCF/MUSIC/commit/f007845154d21613935186d0bbd1557ad6c23728 (committed 2 years ago)

hroncok commented 10 months ago

And then I get:

pymusic.cpp: In function 'PyObject* __pyx_pf_7pymusic_5Setup_6config(__pyx_obj_7pymusic_Setup*, std::string)':
pymusic.cpp:13679:45: error: invalid cast from type 'std::string' {aka 'std::__cxx11::basic_string<char>'} to type 'PyObject*' {aka '_object*'}
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                                             ^~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:1512:43: note: in definition of macro 'likely'
 1512 |   #define likely(x)   __builtin_expect(!!(x), 1)
      |                                           ^
/usr/include/python3.12/object.h:194:28: note: in expansion of macro '_Py_CAST'
  194 | #define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
      |                            ^~~~~~~~
/usr/include/python3.12/object.h:254:43: note: in expansion of macro '_PyObject_CAST'
  254 | #  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
      |                                           ^~~~~~~~~~~~~~
/usr/include/python3.12/floatobject.h:17:32: note: in expansion of macro 'Py_IS_TYPE'
   17 | #define PyFloat_CheckExact(op) Py_IS_TYPE((op), &PyFloat_Type)
      |                                ^~~~~~~~~~
pymusic.cpp:2870:10: note: in expansion of macro 'PyFloat_CheckExact'
 2870 | ((likely(PyFloat_CheckExact(obj))) ?  PyFloat_AS_DOUBLE(obj) :\
      |          ^~~~~~~~~~~~~~~~~~
pymusic.cpp:13679:20: note: in expansion of macro '__Pyx_PyObject_AsDouble'
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                    ^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/python3.12/Python.h:38,
                 from pymusic.cpp:34:
pymusic.cpp:13679:45: error: invalid cast from type 'std::string' {aka 'std::__cxx11::basic_string<char>'} to type 'PyObject*' {aka '_object*'}
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                                             ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/python3.12/pyport.h:24:38: note: in definition of macro '_Py_CAST'
   24 | #define _Py_CAST(type, expr) ((type)(expr))
      |                                      ^~~~
/usr/include/python3.12/cpython/floatobject.h:18:49: note: in expansion of macro '_PyObject_CAST'
   18 | #define PyFloat_AS_DOUBLE(op) PyFloat_AS_DOUBLE(_PyObject_CAST(op))
      |                                                 ^~~~~~~~~~~~~~
pymusic.cpp:2870:39: note: in expansion of macro 'PyFloat_AS_DOUBLE'
 2870 | ((likely(PyFloat_CheckExact(obj))) ?  PyFloat_AS_DOUBLE(obj) :\
      |                                       ^~~~~~~~~~~~~~~~~
pymusic.cpp:13679:20: note: in expansion of macro '__Pyx_PyObject_AsDouble'
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                    ^~~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:13679:45: error: invalid cast from type 'std::string' {aka 'std::__cxx11::basic_string<char>'} to type 'PyObject*' {aka '_object*'}
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                                             ^~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:1512:43: note: in definition of macro 'likely'
 1512 |   #define likely(x)   __builtin_expect(!!(x), 1)
      |                                           ^
/usr/include/python3.12/object.h:194:28: note: in expansion of macro '_Py_CAST'
  194 | #define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
      |                            ^~~~~~~~
/usr/include/python3.12/object.h:254:43: note: in expansion of macro '_PyObject_CAST'
  254 | #  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
      |                                           ^~~~~~~~~~~~~~
/usr/include/python3.12/longobject.h:14:31: note: in expansion of macro 'Py_IS_TYPE'
   14 | #define PyLong_CheckExact(op) Py_IS_TYPE((op), &PyLong_Type)
      |                               ^~~~~~~~~~
pymusic.cpp:2871:9: note: in expansion of macro 'PyLong_CheckExact'
 2871 |  likely(PyLong_CheckExact(obj)) ?\
      |         ^~~~~~~~~~~~~~~~~
pymusic.cpp:13679:20: note: in expansion of macro '__Pyx_PyObject_AsDouble'
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                    ^~~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:13679:45: error: invalid cast from type 'std::string' {aka 'std::__cxx11::basic_string<char>'} to type 'PyObject*' {aka '_object*'}
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                                             ^~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:2872:18: note: in definition of macro '__Pyx_PyObject_AsDouble'
 2872 |  PyLong_AsDouble(obj) : __Pyx__PyObject_AsDouble(obj))
      |                  ^~~
pymusic.cpp:13679:45: error: invalid cast from type 'std::string' {aka 'std::__cxx11::basic_string<char>'} to type 'PyObject*' {aka '_object*'}
13679 |       __pyx_t_10 = __Pyx_PyObject_AsDouble(((PyObject *)__pyx_v_vs)); if (unlikely(__pyx_t_10 == ((double)((double)-1)) && PyErr_Occurred())) __PYX_ERR(0, 441, __pyx_L10_error)
      |                                             ^~~~~~~~~~~~~~~~~~~~~~
pymusic.cpp:2872:50: note: in definition of macro '__Pyx_PyObject_AsDouble'
 2872 |  PyLong_AsDouble(obj) : __Pyx__PyObject_AsDouble(obj))
      |                                                  ^~~
pymusic.cpp: At global scope:
pymusic.cpp:25805:15: warning: 'double __Pyx__PyObject_AsDouble(PyObject*)' defined but not used [-Wunused-function]
25805 | static double __Pyx__PyObject_AsDouble(PyObject* obj) {
      |               ^~~~~~~~~~~~~~~~~~~~~~~~
hroncok commented 10 months ago

I belive this might be a regression in Cython, I've opened https://github.com/cython/cython/issues/5818

hroncok commented 10 months ago

With https://github.com/cython/cython/commit/bbbe428ee5c52dd8fad10b0ee3bb2e33b5a8918c Cython 3 works here.

sanjayankur31 commented 9 months ago

I've got a WIP spec here, waiting on Cython 3.0.6:

https://bugzilla.redhat.com/show_bug.cgi?id=2224366