wizzwizz4
wizzwizz4

Reputation: 6426

How can I create a circular reference of tuples?

For historical reasons (read: horrible type(t) == tuple checks), I find myself needing to freeze a circular graph as a collection of tuple objects. This, obviously, isn't ideal:

>>> head = ("head", None)
>>> a = ("a", ("b", ("c", head)))
>>> head[1] = a
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    head[1] = a
TypeError: 'tuple' object does not support item assignment

I, however, am not really a great believer in TypeErrors, and suspect that, through implementation-specific hackery, this can be done.

Upvotes: 2

Views: 364

Answers (1)

kmaork
kmaork

Reputation: 6012

I, however, am not really a great believer in TypeErrors, and suspect that, through implementation-specific hackery, this can be done.

Sadly, you are correct:

from ctypes import Structure, c_ssize_t, c_void_p, py_object, pythonapi

pythonapi.Py_DecRef.argtypes = py_object,


def mutable(tup):
    # We are generating this class dynamically because the size of ob_item
    # varies according to the size of the given tuple
    class PyTupleObject(Structure):
        _fields_ = [('ob_refcnt', c_ssize_t),
                    ('ob_type', c_void_p),
                    ('ob_size', c_ssize_t),
                    ('ob_item', py_object * len(tup))]

        @classmethod
        def from_tuple(cls, tup):
            instance = cls.from_address(id(tup))
            # Save a reference to tup on the instance, as we are using it directly from memory
            # and don't want it to be garbage collected
            instance.original = tup
            return instance

        def __setitem__(self, idx, val):
            # Replacing a value in self.ob_item doesn't decref the old value but does indref the new value
            pythonapi.Py_DecRef(self.ob_item[idx])
            self.ob_item[idx] = val

        def __getitem__(self, idx):
            return self.ob_item[idx]

        def __iter__(self):
            return iter(self.ob_item)

        def __len__(self):
            return len(self.ob_item)

        def __contains__(self, val):
            return val in self.ob_item

    return PyTupleObject.from_tuple(tup)


if __name__ == '__main__':
    tup = (None,)
    mut_tup = mutable(tup)
    mut_tup[0] = tup
    print(tup is tup[0]) # Outputs "True"

How can I produce this monstrosity without risking going Out of Bounds or causing other C Undefined Behaviour?

We prevent out of bounds access by defining the member ob_item to be py_object * len(tup).

Can the circular dependency freeing part of the Garbage Collector cope with such a thing?

No! Tuples are supposed to be immutable, and therefore are not expected to have a cyclic reference to themselves. That's why they don't implement the tp_clear method, that is used by the python garbage collector to break reference cycles and collect the involved garbages. More details here

Upvotes: 3

Related Questions