code: def test_Dataset_flyweight(path1=None, path2=None):
      import gc
      import sys
  
      ds1 = Dataset(path1)
      assert_is_instance(ds1, Dataset)
      # Don't create circular references or anything similar
      assert_equal(1, sys.getrefcount(ds1) - 1)
  
      ds1.create()
  
      # 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()
  
      # refcount still fine after repo creation:
      assert_equal(1, sys.getrefcount(ds1) - 1)
  
      # instantiate again:
      ds2 = Dataset(path1)
      assert_is_instance(ds2, Dataset)
      # the very same object:
      ok_(ds1 is ds2)
  
      # reference the same via relative path:
      with chpwd(path1):
          ds3 = Dataset(relpath(path1, start=path2))
          ok_(ds1 == ds3)
          ok_(ds1 is ds3)
  
      # gc knows one such object only:
      eq_(
          1,
          len(
              [
                  o
                  for o in gc.get_objects()
                  if isinstance(o, Dataset) and o.path == path1
              ]
          ),
      )
  
      # on windows a symlink is not what you think it is
      if not on_windows:
          # reference the same via symlink:
          with chpwd(path2):
              os.symlink(path1, "linked")
              ds4 = Dataset("linked")
              ds4_id = id(ds4)
              ok_(ds4 == ds1)
              ok_(ds4 is not ds1)
  
          # underlying repo, however, IS the same:
          ok_(ds4.repo is ds1.repo)
  
      # deleting one reference has no effect on the other:
      del ds1
      gc.collect()  # TODO: see first comment above
      ok_(ds2 is not None)
      ok_(ds2.repo is ds3.repo)
      if not on_windows:
          ok_(ds2.repo is ds4.repo)
  
      # deleting remaining references should lead to garbage collection
      del ds2
  
      with swallow_logs(new_level=1) as cml:
          del ds3
          gc.collect()  # TODO: see first comment above
          # flyweight vanished:
          assert_not_in(path1, Dataset._unique_instances.keys())
          # no such instance known to gc anymore:
          eq_(
              [],
              [
                  o
                  for o in gc.get_objects()
                  if isinstance(o, Dataset) and o.path == path1
              ],
          )
          # underlying repo should only be cleaned up, if ds3 was the last
          # reference to it. Otherwise the repo instance should live on
          # (via symlinked ds4):
          finalizer_log = "Finalizer called on: AnnexRepo(%s)" % path1
          if on_windows:
              cml.assert_logged(
                  msg=finalizer_log, level="Level 1", regex=False
              )
          else:
              assert_not_in(finalizer_log, cml.out)
              # symlinked is still there:
              ok_(ds4 is not None)
              eq_(ds4_id, id(ds4))
  
imports: import os
  import os.path
  from os.path import abspath
  from os.path import join
  from os.path import lexists, relpath
  import pytest
  import datalad.utils
  from datalad import cfg
  from datalad.api import clone, create, get
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.exceptions import NoDatasetFound
  from datalad.support.gitrepo import GitRepo
  from datalad.tests.utils_pytest import OBSCURE_FILENAME, SkipTest, assert_equal, assert_false, assert_is, assert_is_instance, assert_is_none, assert_is_not, assert_is_not_none, assert_not_equal, assert_not_in, assert_raises, assert_repo_status, assert_result_count, assert_true, eq_, known_failure_windows, ok_, swallow_logs, with_tempfile
  from datalad.utils import Path, _path_, chpwd, on_windows, rmtree
  from ..dataset import Dataset, EnsureDataset, require_dataset, resolve_path
label_desc: Use image node_id 9 for calling a global function with example usage: test_Dataset_flyweight(path1, path2) without return types