[ctypes-commit] ctypes/unittests test_struct_fields.py,NONE,1.1
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2005-02-10 09:12:45
|
Update of /cvsroot/ctypes/ctypes/unittests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15074 Added Files: test_struct_fields.py Log Message: New flag value DICTFLAG_FINAL. The flag is set when one of the following occurrs: - The _fields_ attribute is set. - An instance of the type is created. - The type is used as field in another Structure/Union. - The type is subclassed. When this flag is set, assigning the _fields_ attribute is no longer allowed. --- NEW FILE: test_struct_fields.py --- import unittest from ctypes import * class StructFieldsTestCase(unittest.TestCase): # Structure/Union classes must get 'finalized' sooner or # later, when one of these things happen: # # 1. _fields_ is set. # 2. An instance is created. # 3. The type is used as field of another Structure/Union. # 4. The type is subclassed # # When they are finalized, assigning _fields_ is no longer allowed. def test_1_A(self): class X(Structure): pass self.failUnlessEqual(sizeof(X), 0) # not finalized X._fields_ = [] # finalized self.assertRaises(AttributeError, setattr, X, "_fields_", []) def test_1_B(self): class X(Structure): _fields_ = [] # finalized self.assertRaises(AttributeError, setattr, X, "_fields_", []) def test_2(self): class X(Structure): pass X() self.assertRaises(AttributeError, setattr, X, "_fields_", []) def test_3(self): class X(Structure): pass class Y(Structure): _fields_ = [("x", X)] # finalizes X self.assertRaises(AttributeError, setattr, X, "_fields_", []) def test_4(self): class X(Structure): pass class Y(X): pass self.assertRaises(AttributeError, setattr, X, "_fields_", []) Y._fields_ = [] self.assertRaises(AttributeError, setattr, X, "_fields_", []) if __name__ == "__main__": unittest.main() |