<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def search_upstream(self, device: devicetools.Device, name: str = 'upstream') -> 'Selection': """Return the network upstream of the given starting point, including the starting point itself. You can pass both |Node| and |Element| objects and, optionally, the name of the newly created |Selection| object: Selection("upstream", nodes=("dill", "lahn_1", "lahn_2"), elements=("land_dill", "land_lahn_1", "land_lahn_2", "stream_dill_lahn_2", "stream_lahn_1_lahn_2")) Selection("UPSTREAM", nodes="lahn_1", elements=("land_lahn_1", "stream_lahn_1_lahn_2")) Wrong device specifications result in errors like the following: Traceback (most recent call last):
 TypeError: While trying to determine the upstream network of \ selection `test`, the following error occurred: Either a `Node` or \ an `Element` object is required as the "outlet device", but the given \ `device` value is of type `int`. Traceback (most recent call last):
 KeyError: "While trying to determine the upstream network of \ selection `headwaters`, the following error occurred: 'No node named \ `lahn_3` available.'" Method |Selection.select_upstream| restricts the current selection to the one determined with the method |Selection.search_upstream|: Selection("test", nodes=("dill", "lahn_1", "lahn_2"), elements=("land_dill", "land_lahn_1", "land_lahn_2", "stream_dill_lahn_2", "stream_lahn_1_lahn_2")) On the contrary, the method |Selection.deselect_upstream| restricts the current selection to all devices not determined by method |Selection.search_upstream|: Selection("complete", nodes="lahn_3", elements=("land_lahn_3", "stream_lahn_2_lahn_3")) If necessary, include the "outlet device" manually afterwards: Selection("complete", nodes=("lahn_2", "lahn_3"), elements=("land_lahn_3", "stream_lahn_2_lahn_3")) """

        try:
            selection = Selection(name)
            if isinstance(device, devicetools.Node):
                node = self.nodes[device.name]
                return self.__get_nextnode(node, selection)
            if isinstance(device, devicetools.Element):
                element = self.elements[device.name]
                return self.__get_nextelement(element, selection)
            raise TypeError(
                f'Either a `Node` or an `Element` object is required '
                f'as the "outlet device", but the given `device` value '
                f'is of type `{objecttools.classname(device)}`.')
        except BaseException:
            objecttools.augment_excmessage(
                f'While trying to determine the upstream network of '
                f'selection `{self.name}`')