예제 #1
0
    # hashing and comparability. But in PYX mode, we override _hash and
    # _equals as well.
    def _key(self):
        raise TypeError("Cannot hash or compare capnpy structs. "
                        "Use the $Py.key annotation to enable it")

    def __hash__(self):
        return hash(self._key())

    def _equals(self, other):
        if isinstance(other, Struct) or isinstance(other, tuple):
            # by doing this, we ensure that we compare equals to tuples
            return other == self._key()
        return False

    # this is already defined in blob.py: however, it seems if we do not
    # redeclare it here, Cython won't use it
    def __richcmp__(self, other, op):
        return self._richcmp(other, op)


# Attach the dump[s] methods to Struct. This is the only way I found to make
# sure that Struct.dumps is implemented in C (on CPython). The obvious
# alternative is to implement dumps directly in the class body and to declare
# it as cpdef in struct_.pxd: however, this cannot work because there are
# circular dependencies between struct_.pxd and message.pxd. This gives a ~20%
# improvement.
import capnpy.message
magic_setattr(Struct, 'dump', capnpy.message.dump)
magic_setattr(Struct, 'dumps', capnpy.message.dumps)
예제 #2
0
def fill_enum(cls):
    for i, member in enumerate(cls.__members__):
        value = cls(i)
        magic_setattr(cls, member, value)