code: def test_weakref(create_weakref, create_weakref_with_callback):
      from weakref import getweakrefcount
  
      # Apparently, you cannot weakly reference an object()
      class WeaklyReferenced:
          pass
  
      callback_called = False
  
      def callback(wr):
          nonlocal callback_called
          callback_called = True
  
      obj = WeaklyReferenced()
      assert getweakrefcount(obj) == 0
      wr = create_weakref(obj)
      assert getweakrefcount(obj) == 1
  
      obj = WeaklyReferenced()
      assert getweakrefcount(obj) == 0
      wr = create_weakref_with_callback(obj, callback)  # noqa: F841
      assert getweakrefcount(obj) == 1
      assert not callback_called
      del obj
      pytest.gc_collect()
      assert callback_called
  
imports: import contextlib
  import sys
  import types
  import pytest
  import env
  from pybind11_tests import detailed_error_messages_enabled
  from pybind11_tests import pytypes
label_desc: Use image node_id 42 for calling a global function with example usage: test_weakref(create_weakref, create_weakref_with_callback) without return types