<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ListNames(self):
 """List the names of all keys and values."""


    # TODO: This check is flawed, because the current definition of
    # "IsDirectory" is the negation of "is a file". One registry path can
    # actually refer to a key ("directory"), a value of the same name ("file")
    # and the default value of the key at the same time.
    if not self.IsDirectory():
      return

    # Handle the special case where no hive is specified and just list the hives
    if self.hive is None:
      for name in dir(winreg):
        if name.startswith("HKEY_"):
          yield name

      return

    try:
      with OpenKey(self.hive, self.local_path) as key:
        (self.number_of_keys, self.number_of_values,
         self.last_modified) = QueryInfoKey(key)

        found_keys = set()

        # First keys
        for i in range(self.number_of_keys):
          try:
            key_name = EnumKey(key, i)
            found_keys.add(key_name)
            yield key_name
          except OSError:
            pass

        # Now Values
        for i in range(self.number_of_values):
          try:
            name, unused_value, unused_value_type = EnumValue(key, i)

            # A key might contain a sub-key and value of the same name. Do not
            # yield the same name twice in this case. With only the name,
            # the caller cannot differentiate between a key and a value anyway.
            if name not in found_keys:
              yield name
          except OSError:
            pass

    except OSError as e:
      raise IOError("Unable to list key %s: %s" % (self.key_name, e))