antocuni / capnpy

Other
45 stars 26 forks source link

Hack for import enum #28

Closed colinfang closed 6 years ago

colinfang commented 6 years ago

Fix #13

Basically in pyx

A enum has

    @staticmethod
    cdef _new(long x, __prebuilt=(TestEnum(0), TestEnum(1),)):
        try:
            return __prebuilt[x]
        except IndexError:
            return TestEnum(x)
    @staticmethod
    def _new_hack(long x, __prebuilt=(TestEnum(0), TestEnum(1),)):
        try:
            return __prebuilt[x]
        except IndexError:
            return TestEnum(x)

And a caller would do

    property test:
        def __get__(self):
            # no union check
            value = self._read_data_int16(0)
            if 0 != 0:
                value = (value ^ 0)
            return _schema_base_capnp.TestEnum._new_hack(value)

based on if the enum is imported or not