import doctest import unittest def loadtestsloader tests pattern tests addtestdoctest doctestsuite return tests if name main unittest main this tests the _objects attribute of ctypes instances _objects holds references to objects that must be kept alive as long as the ctypes instance to make sure that the memory buffer is valid warning the _objects attribute is exposed only for debugging ctypes itself it must never be modified _objects is initialized to a dictionary on first use before that it is none here is an array of string pointers from ctypes import structure c_int c_char_p array c_char_p 5 print array _objects none the memory block stores pointers to strings and the strings itself assigned from python must be kept array 4 b foo bar array _objects 4 b foo bar array 4 b foo bar it gets more complicated when the ctypes instance itself is contained in a base object class x structure _fields_ x c_int y c_int array c_char_p 5 x x print x _objects none the array attribute of the x object shares part of the memory buffer of x _b_base_ is either none or the root object owning the memory block print x array _b_base_ doctest ellipsis test test_ctypes test_objects x object at 0x x array 0 b spam spam spam x _objects 0 2 b spam spam spam x array _b_base_ _objects 0 2 b spam spam spam
r

import doctest
import unittest


def load_tests(loader, tests, pattern):
    tests.addTest(doctest.DocTestSuite())
    return tests


if __name__ == '__main__':
    unittest.main()