code: def test_GitRepo_flyweight(path1=None, path2=None):
      import gc
  
      repo1 = GitRepo(path1, create=True)
      assert_is_instance(repo1, GitRepo)
  
      # Due to issue 4862, we currently still require gc.collect() under unclear
      # circumstances to get rid of an exception traceback when creating in an
      # existing directory. That traceback references the respective function
      # frames which in turn reference the repo instance (they are methods).
      # Doesn't happen on all systems, though. Eventually we need to figure that
      # out.
      # However, still test for the refcount after gc.collect() to ensure we don't
      # introduce new circular references and make the issue worse!
      gc.collect()
  
      # As long as we don't reintroduce any circular references or produce
      # garbage during instantiation that isn't picked up immediately, `repo1`
      # should be the only counted reference to this instance.
      # Note, that sys.getrefcount reports its own argument and therefore one
      # reference too much.
      assert_equal(1, sys.getrefcount(repo1) - 1)
  
      # instantiate again:
      repo2 = GitRepo(path1, create=False)
      assert_is_instance(repo2, GitRepo)
  
      # the very same object:
      ok_(repo1 is repo2)
  
      # reference the same in a different way:
      with chpwd(path1):
          repo3 = GitRepo(op.relpath(path1, start=path2), create=False)
  
      # it's the same object:
      ok_(repo1 is repo3)
  
      # and realpath attribute is the same, so they are still equal:
      ok_(repo1 == repo3)
  
      orig_id = id(repo1)
  
      # Be sure we have exactly one object in memory:
      assert_equal(
          1,
          len(
              [
                  o
                  for o in gc.get_objects()
                  if isinstance(o, GitRepo) and o.path == path1
              ]
          ),
      )
  
      # deleting one reference doesn't change anything - we still get the same
      # thing:
      gc.collect()  #  TODO: see first comment above
      del repo1
      ok_(repo2 is not None)
      ok_(repo2 is repo3)
      ok_(repo2 == repo3)
  
      # re-requesting still delivers the same thing:
      repo1 = GitRepo(path1)
      assert_equal(orig_id, id(repo1))
  
      # killing all references should result in the instance being gc'd and
      # re-request yields a new object:
      del repo1
      del repo2
  
      # Killing last reference will lead to garbage collection which will call
      # GitRepo's finalizer:
      with swallow_logs(new_level=1) as cml:
          del repo3
          gc.collect()  # TODO: see first comment above
          cml.assert_logged(
              msg="Finalizer called on: GitRepo(%s)" % path1,
              level="Level 1",
              regex=False,
          )
  
      # Flyweight is gone:
      assert_not_in(path1, GitRepo._unique_instances.keys())
      # gc doesn't know any instance anymore:
      assert_equal(
          [],
          [
              o
              for o in gc.get_objects()
              if isinstance(o, GitRepo) and o.path == path1
          ],
      )
  
      # new object is created on re-request:
      repo1 = GitRepo(path1)
      assert_equal(
          1,
          len(
              [
                  o
                  for o in gc.get_objects()
                  if isinstance(o, GitRepo) and o.path == path1
              ]
          ),
      )
  
imports: import logging
  import os
  import os.path
  import sys
  import pytest
  from datalad import get_encoding_info
  from datalad.cmd import StdOutCapture, StdOutErrCapture, WitlessRunner
  from datalad.support.exceptions import CommandError, FileNotInRepositoryError, InvalidGitRepositoryError, NoSuchPathError, PathKnownToRepositoryError
  from datalad.support.external_versions import external_versions
  from datalad.support.gitrepo import GitRepo, _normalize_path, normalize_paths, to_options
  from datalad.support.sshconnector import get_connection_hash
  from datalad.tests.utils_pytest import DEFAULT_BRANCH, DEFAULT_REMOTE, SkipTest, assert_cwd_unchanged, assert_equal, assert_false, assert_in, assert_in_results, assert_is_instance, assert_not_equal, assert_not_in, assert_raises, assert_repo_status, assert_true, create_tree, eq_, get_most_obscure_supported_name, integration, neq_, ok_, skip_if_no_network, skip_if_on_windows, skip_nomultiplex_ssh, slow, swallow_logs, with_tempfile, with_tree, xfail_buggy_annex_info
  from datalad.utils import Path, chpwd, getpwd, on_windows, rmtree
label_desc: Use image node_id 36 for calling a global function with example usage: test_GitRepo_flyweight(path1, path2) without return types