def __init__(self, t):
        assert hasattr(t, '__typed_python_category__')
        super().__init__(t)
        bytecount = _types.bytecount(t)

        self.subTypeWrappers = tuple(typeWrapper(sub_t) for sub_t in t.ElementTypes)
        self.byteOffsets = [0]

        for i in range(len(self.subTypeWrappers)-1):
            self.byteOffsets.append(self.byteOffsets[-1] + _types.bytecount(t.ElementTypes[i]))

        self.layoutType = native_ast.Type.Array(element_type=native_ast.UInt8, count=bytecount)

        self._is_pod = all(typeWrapper(possibility).is_pod for possibility in self.subTypeWrappers)
        self.is_default_constructible = _types.is_default_constructible(t)
    def __init__(self, t):
        super().__init__(t)

        self.nameToIndex = {}
        self.indexToByteOffset = {}
        self.classType = t

        element_types = [('refcount', native_ast.Int64),
                         ('data', native_ast.UInt8)]

        # this follows the general layout of 'held class' which is 1 bit per field for initialization and then
        # each field packed directly according to byte size
        byteOffset = 8 + (len(self.classType.MemberNames) // 8 + 1)

        self.bytesOfInitBits = byteOffset - 8

        for i, name in enumerate(self.classType.MemberNames):
            self.nameToIndex[name] = i
            self.indexToByteOffset[i] = byteOffset

            byteOffset += _types.bytecount(self.classType.MemberTypes[i])

        self.layoutType = native_ast.Type.Struct(element_types=element_types,
                                                 name=t.__qualname__ +
                                                 "Layout").pointer()
    def test_class(self):
        with self.assertRaises(TypeError):
            class A(Class):
                x = Member((1,2,3))

        class A(Class):
            x = Member(int)

            y = int #not a member. Just a value.

            def f(self):
                return 10

        self.assertEqual(_types.bytecount(A), 8)
        self.assertTrue(A.y is int)

        a = A()

        with self.assertRaises(AttributeError):
            a.not_an_attribute

        self.assertEqual(a.x, 0)

        a.x = 10

        self.assertEqual(a.x, 10)
    def convert_call(self, context, expr, args, kwargs):
        if len(args) == 1 and isinstance(
                args[0].expr_type, PythonTypeObjectWrapper) and not kwargs:
            return context.constant(
                bytecount(args[0].expr_type.typeRepresentation))

        return super().convert_call(context, expr, args, kwargs)
    def generateConstructor(self, context, out, *args):
        context.pushEffect(
            out.expr.store(
                runtime_functions.malloc.call(
                    native_ast.const_int_expr(
                        _types.bytecount(self.typeRepresentation.HeldClass) +
                        8)).cast(self.getNativeLayoutType())) >>
            # store a refcount
            out.expr.load().ElementPtrIntegers(0, 0).store(
                native_ast.const_int_expr(1)))

        # clear bits of init flags
        for byteOffset in range(self.bytesOfInitBits):
            context.pushEffect(
                out.nonref_expr.cast(
                    native_ast.UInt8.pointer()).ElementPtrIntegers(
                        8 + byteOffset).store(native_ast.const_uint8_expr(0)))

        for i in range(len(self.classType.MemberTypes)):
            if _types.wantsToDefaultConstruct(self.classType.MemberTypes[i]):
                name = self.classType.MemberNames[i]

                if name in self.classType.MemberDefaultValues:
                    defVal = self.classType.MemberDefaultValues.get(name)
                    context.pushReference(
                        self.classType.MemberTypes[i],
                        self.memberPtr(out, i)).convert_copy_initialize(
                            nativepython.python_object_representation.
                            pythonObjectRepresentation(context, defVal))
                else:
                    context.pushReference(self.classType.MemberTypes[i],
                                          self.memberPtr(
                                              out,
                                              i)).convert_default_initialize()
                context.pushEffect(self.setIsInitializedExpr(out, i))

        if '__init__' in self.typeRepresentation.MemberFunctions:
            initFuncType = typeWrapper(
                self.typeRepresentation.MemberFunctions['__init__'])
            initFuncType.convert_call(context, context.pushVoid(initFuncType),
                                      (out, ) + args, {})
        else:
            if len(args):
                context.pushException(
                    TypeError, "Can't construct a " +
                    self.typeRepresentation.__qualname__ +
                    " with positional arguments because it doesn't have an __init__"
                )
Exemple #6
0
    def __init__(self, t):
        assert hasattr(t, '__typed_python_category__')
        super().__init__(t)

        assert len(t.Types) > 1

        excessBytes = _types.bytecount(t) - 1

        self.layoutType = native_ast.Type.Struct(
            element_types=(('which', native_ast.UInt8),
                           ('data',
                            native_ast.Type.Array(
                                element_type=native_ast.UInt8,
                                count=excessBytes))),
            name='OneOfLayout')

        self._is_pod = all(
            typeWrapper(possibility).is_pod for possibility in t.Types)
 def testfun(x):
     return _types.bytecount(type(x))
    def getBytecount(self):
        if self.is_empty:
            return 0

        return _types.bytecount(self.typeRepresentation)
 def test_object_bytecounts(self):
     self.assertEqual(_types.bytecount(NoneType()), 0)
     self.assertEqual(_types.bytecount(Int8()), 1)
     self.assertEqual(_types.bytecount(Int64()), 8)