Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with a Cython ValueError

Tags:

python

cython

I'm fairly new to Cython extension-types and puzzled with the following Cython-related ValueError that is thrown during runtime:

ValueError: vrptwms.node.Node has the wrong size, try recompiling

The Node class is defined in the files node.pxd and node.pyx in the directory vrptwms. The content of the former is

cdef class Node:
    """
    docstring
    """
    cdef public float x, y, demand
    cdef public float earliest_start, latest_start, servicetime
    cdef public int id

and that of the latter is (I temporarily removed all type declarations hoping to track down the issue)

cdef class Node:
    """
    Represents either a customer or the depot.
    """
    # pylint: disable-msg=C0103, R0913

    def __init__(self,
                id_,
                x,
                y,
                demand,
                earliest_start,
                latest_start,
                servicetime):
        """
        docstring
        """
        self.x = float(x)
        self.y = float(y)
        self.demand = float(demand)
        self.earliest_start = float(earliest_start)
        self.latest_start = float(latest_start)
        self.servicetime = float(servicetime)
        self.id = int(id_)

    # some internal functions

The node class is then included by a third file, problemreader.pyx, the following way:

from vrptwms.node cimport Node
from vrptwms.node import Node

The compilation works without any problem. setup.py contains

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("route", ["fastroute.pyx"]),
            Extension("node", ["node.pyx", "node.pxd"]),
            Extension("problemreader", ["problemreader.pyx"]),
            ]

setup(
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
)

I also attempted to add node.pxd to the problemreader extension without success. The issue is thrown at the following generated C code

  __pyx_ptype_7vrptwms_4node_Node = __Pyx_ImportType("vrptwms.node", "Node", sizeof(struct __pyx_obj_7vrptwms_4node_Node), 1); if (unlikely(!__pyx_ptype_7vrptwms_4node_Node)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

and causes

Traceback (most recent call last):
  File "./cli.py", line 16, in <module>
    from vrptwms.problemreader import ProblemReader
  File "node.pxd", line 9, in init problemreader (problemreader.c:4991)
    cdef class Node:
ValueError: vrptwms.node.Node has the wrong size, try recompiling

I removed all generated .c, .so and .o files multiple times, but couldn't get rid of the problem. Any hints (including any links to documentation that I might have missed) are greatly appreciated.

Edit: the problem does not reproduce if I use old style relative imports (eg import node instead of vrptwms.node) and remove the init.py file - so there's nothing wrong with the source itself. I've created a tiny testcase reproducing the problem: c_test.tar.gz (needs to be extracted to a directory on the PYTHONPATH) and an almost identical case without the use of a package that doesn't reproduce it: c_test_w.tar.gz.

like image 458
Gerald Senarclens de Grancy Avatar asked Nov 04 '22 17:11

Gerald Senarclens de Grancy


1 Answers

Robert Bradshaw proposed a few hints on the cython-users mailing-list. The bottom line is that manually (re-)compiling the .pyx files with cython *.pyx and the running the original setup script works. There is also a newer way of writing the setup script described in CEP 201 - Distutils Preprocessing that should help but doesn't work in my current setup with Cython 0.14.1.

like image 146
Gerald Senarclens de Grancy Avatar answered Nov 15 '22 08:11

Gerald Senarclens de Grancy