code: def test_refcount(self):
      def func(*args):
          pass
  
      # this is the standard refcount for func
      self.assertEqual(sys.getrefcount(func), 2)
  
      # the CFuncPtr instance holds at least one refcount on func:
      f = OtherCallback(func)
      self.assertGreater(sys.getrefcount(func), 2)
  
      # and may release it again
      del f
      self.assertGreaterEqual(sys.getrefcount(func), 2)
  
      # but now it must be gone
      gc.collect()
      self.assertEqual(sys.getrefcount(func), 2)
  
      class X(ctypes.Structure):
          _fields_ = [("a", OtherCallback)]
  
      x = X()
      x.a = OtherCallback(func)
  
      # the CFuncPtr instance holds at least one refcount on func:
      self.assertGreater(sys.getrefcount(func), 2)
  
      # and may release it again
      del x
      self.assertGreaterEqual(sys.getrefcount(func), 2)
  
      # and now it must be gone again
      gc.collect()
      self.assertEqual(sys.getrefcount(func), 2)
  
      f = OtherCallback(func)
  
      # the CFuncPtr instance holds at least one refcount on func:
      self.assertGreater(sys.getrefcount(func), 2)
  
      # create a cycle
      f.cycle = f
  
      del f
      gc.collect()
      self.assertEqual(sys.getrefcount(func), 2)
  
file_path: cpython/Lib/test/test_ctypes/test_refcounts.py
imports: import _ctypes_test
  import ctypes
  import gc
  import sys
  import unittest
  from test import support
label_desc: Use image node_id 2 for calling the RefcountTestCase obj's underlying member method code with example usage: obj.test_refcount() without return types