<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def popd(pop_all=False, throw_if_dir_invalid=True):
 """Restore current working directory to previous directory. The previous directory is whatever it was when last :py:meth:`pushd()` was *last* called. :py:meth:`pushd()` creates a stack, so each call to popd() simply sets the CWD back to what it was on the prior pushd() call. Parameters pop_all : bool, optional When `pop_all` is True, sets the CWD to the state when pushd() was first called. Does NOT call os.getcwd() for intervening paths, only the final path. throw_if_dir_invalid : bool, optional Whether or not to pass back up any exception raised by chdir(). Default is True. Returns ------- True : bool Success False : bool Failure Raises ------ OSError If `throw_if_dir_invalid` is True and chdir raises an exception, this function will chain the same exception as chdir, typically OSError ValueError If popd() called on an empty stack; i.e. before :py:meth:`pushd()` has been called. Notes ----- This method and its counterpart :py:meth:`pushd` are **not** thread safe! """

    global _pushdstack
    from os import chdir

    if len(_pushdstack) == 0:
        raise ValueError("popd() called on an empty stack.")

    if pop_all:
        while( len(_pushdstack) > 1):
            _pushdstack.pop()

    try:
        chdir(_pushdstack.pop())
        err = 0
    except OSError:
        if throw_if_dir_invalid:
            raise
        err = 1

    return err == 0