Segmentation fault when creating IntVector of set

Issue #407 resolved
Chandler May
created an issue

I know creating an IntVector from a set is not an immensely useful feature. I had in fact done so on accident. But the segfault made the bug harder to track down than I would have wished...

I'm using Python 2.7.12, rpy2 2.8.5, and R 3.2.3, on Ubuntu 16.04.2 LTS.

To reproduce the segmentation fault:

from rpy2.robjects import IntVector
IntVector(set((0, 1)))

Comments (5)

  1. Laurent Gautier

    The issue is at the rinterface level:

    from rpy2.rinterface import IntSexpVector, initr
    initr()
    
    IntSexpVextor(set(0, 1))
    

    What is happening at the C level is that the Python object passed (here a set) is first checked to have a length (__len__, and set objects happen to have one) before trying to access the elements by positional index (which cannot be done with sets) using a C macro that does not check much (so it crashes).

    The constructor for rpy2 vectors would be better if iterating through Python objects with a length rather than trying to access elements by positions. There might be a small performance penalty (I am not sure it would matter in most situation) but the trade-off would be convenience.

  2. Laurent Gautier

    It turned out that the case of a Python objects that has a length and is an iterator was covered, but set objects (or dict objects) are iterable but not iterators.

    The fix in 23c457171504 is triggering a ValueError exception (arguably better than a segfault), and this will be backported to the branch version_2.8.x soon.

  3. Log in to comment