code: def expand_path(pathname):
      
      # The following logic previously used regular expressions but that approach
      # turned out to be very error prone, hence the current contraption based on
      # direct string manipulation :-).
      home_directory = find_home_directory()
      separators = set([os.sep])
      if os.altsep is not None:
          separators.add(os.altsep)
      if len(pathname) >= 2 and pathname[0] == '~' and pathname[1] in separators:
          pathname = os.path.join(home_directory, pathname[2:])
      # Also expand environment variables.
      return parse_path(pathname)
docs: Expand the home directory in a pathname based on the effective user id.
  
      :param pathname: A pathname that may start with ``~/``, indicating the path
                       should be interpreted as being relative to the home
                       directory of the current (effective) user.
      :returns: The (modified) pathname.
  
      This function is a variant of :func:`os.path.expanduser()` that doesn't use
      ``$HOME`` but instead uses the home directory of the effective user id.
      This is basically a workaround for ``sudo -s`` not resetting ``$HOME``.