Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: a58a626092151ed73e2e0382ff63bd0c7dca4a14 authored by Benjamin Peterson on 07 March 2009, 16:34:40 UTC
bump version to 3.1a1
Tip revision: a58a626
test_init.py
from ctypes import *
import unittest

class X(Structure):
    _fields_ = [("a", c_int),
                ("b", c_int)]
    new_was_called = False

    def __new__(cls):
        result = super().__new__(cls)
        result.new_was_called = True
        return result

    def __init__(self):
        self.a = 9
        self.b = 12

class Y(Structure):
    _fields_ = [("x", X)]


class InitTest(unittest.TestCase):
    def test_get(self):
        # make sure the only accessing a nested structure
        # doesn't call the structure's __new__ and __init__
        y = Y()
        self.failUnlessEqual((y.x.a, y.x.b), (0, 0))
        self.failUnlessEqual(y.x.new_was_called, False)

        # But explicitely creating an X structure calls __new__ and __init__, of course.
        x = X()
        self.failUnlessEqual((x.a, x.b), (9, 12))
        self.failUnlessEqual(x.new_was_called, True)

        y.x = x
        self.failUnlessEqual((y.x.a, y.x.b), (9, 12))
        self.failUnlessEqual(y.x.new_was_called, False)

if __name__ == "__main__":
    unittest.main()
back to top