a testcase which accesses values in a dll this test checks and changes an integer stored inside the ctypestest dllshared lib to avoid test failures when this test is repeated several times the original value must be restored this test only works when python itself is a dllshared library def testoptimizeflagself this test accesses the pyoptimizeflag integer which is exported by the python dll and should match the sys flags value opt cint indllpythonapi pyoptimizeflag value self assertequalopt sys flags optimize def testfrozentableself python exports a pyimportfrozenmodules symbol this is a pointer to an array of struct frozen entries the end of the array is marked by an entry containing a null name and zero size in standard python this table contains a hello module and a phello package containing a spam module class structfrozenstructure fields name ccharp code pointercubyte size cint ispackage cint frozentable pointerstructfrozen modules for group in bootstrap stdlib test ft frozentable indllpythonapi fpyimportfrozengroup ft is a pointer to the structfrozen entries for entry in ft this is dangerous we can iterate over a pointer but the loop will not terminate maybe with an access violation because the pointer instance has no size if entry name is none break modname entry name decodeascii modules appendmodname with self subtestmodname if entry size 0 do a sanity check on entry size and entry code self assertgreaterabsentry size 10 self asserttrueentry codei for i in rangeabsentry size check the module s packageness with importhelper frozenmodules spec importlib util findspecmodname if entry ispackage it s a package self assertisnotnonespec submodulesearchlocations else self assertisnonespec submodulesearchlocations with importhelper frozenmodules expected imp frozenmodulenames self maxdiff none self assertequalmodules expected pyimportfrozenbootstrap example in doclibraryctypes rst may be out of date del pointertypecachestructfrozen def testundefinedself self assertraisesvalueerror cint indll pythonapi undefinedsymbol if name main unittest main this test checks and changes an integer stored inside the _ctypes_test dll shared lib to avoid test failures when this test is repeated several times the original value must be restored this test only works when python itself is a dll shared library this test accesses the py_optimizeflag integer which is exported by the python dll and should match the sys flags value python exports a pyimport_frozenmodules symbol this is a pointer to an array of struct _frozen entries the end of the array is marked by an entry containing a null name and zero size in standard python this table contains a __hello__ module and a __phello__ package containing a spam module ft is a pointer to the struct_frozen entries this is dangerous we can iterate over a pointer but the loop will not terminate maybe with an access violation because the pointer instance has no size do a sanity check on entry size and entry code check the module s package ness it s a package
import _ctypes_test
import _imp
import importlib.util
import sys
import unittest
from ctypes import (Structure, CDLL, POINTER, pythonapi,
                    _pointer_type_cache,
                    c_ubyte, c_char_p, c_int)
from test.support import import_helper


class ValuesTestCase(unittest.TestCase):

    def test_an_integer(self):
        
        
        ctdll = CDLL(_ctypes_test.__file__)
        an_integer = c_int.in_dll(ctdll, "an_integer")
        x = an_integer.value
        self.assertEqual(x, ctdll.get_an_integer())
        an_integer.value *= 2
        self.assertEqual(x*2, ctdll.get_an_integer())
        
        
        an_integer.value = x
        self.assertEqual(x, ctdll.get_an_integer())

    def test_undefined(self):
        ctdll = CDLL(_ctypes_test.__file__)
        self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol")


class PythonValuesTestCase(unittest.TestCase):
    

    def test_optimizeflag(self):
        
        

        opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value
        self.assertEqual(opt, sys.flags.optimize)

    def test_frozentable(self):
        
        
        
        

        
        
        
        class struct_frozen(Structure):
            _fields_ = [("name", c_char_p),
                        ("code", POINTER(c_ubyte)),
                        ("size", c_int),
                        ("is_package", c_int),
                        ]
        FrozenTable = POINTER(struct_frozen)

        modules = []
        for group in ["Bootstrap", "Stdlib", "Test"]:
            ft = FrozenTable.in_dll(pythonapi, f"_PyImport_Frozen{group}")
            
            for entry in ft:
                
                
                
                if entry.name is None:
                    break
                modname = entry.name.decode("ascii")
                modules.append(modname)
                with self.subTest(modname):
                    if entry.size != 0:
                        
                        self.assertGreater(abs(entry.size), 10)
                        self.assertTrue([entry.code[i] for i in range(abs(entry.size))])
                    
                    with import_helper.frozen_modules():
                        spec = importlib.util.find_spec(modname)
                    if entry.is_package:
                        
                        self.assertIsNotNone(spec.submodule_search_locations)
                    else:
                        self.assertIsNone(spec.submodule_search_locations)

        with import_helper.frozen_modules():
            expected = _imp._frozen_module_names()
        self.maxDiff = None
        self.assertEqual(modules, expected,
                         "_PyImport_FrozenBootstrap example "
                         "in Doc/library/ctypes.rst may be out of date")

        del _pointer_type_cache[struct_frozen]

    def test_undefined(self):
        self.assertRaises(ValueError, c_int.in_dll, pythonapi,
                          "Undefined_Symbol")


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