def getpwd():
    'Try to return a CWD without dereferencing possible symlinks\n\n    This function will try to use PWD environment variable to provide a current\n    working directory, possibly with some directories along the path being\n    symlinks to other directories.  Unfortunately, PWD is used/set only by the\n    shell and such functions as `os.chdir` and `os.getcwd` nohow use or modify\n    it, thus `os.getcwd()` returns path with links dereferenced.\n\n    While returning current working directory based on PWD env variable we\n    verify that the directory is the same as `os.getcwd()` after resolving all\n    symlinks.  If that verification fails, we fall back to always use\n    `os.getcwd()`.\n\n    Initial decision to either use PWD env variable or os.getcwd() is done upon\n    the first call of this function.\n    '
    global _pwd_mode
    if (_pwd_mode is None):
        try:
            pwd = os.environ['PWD']
            if (on_windows and pwd and pwd.startswith('/')):
                _pwd_mode = 'cwd'
            else:
                _pwd_mode = 'PWD'
        except KeyError:
            _pwd_mode = 'cwd'
    if (_pwd_mode == 'cwd'):
        return os.getcwd()
    elif (_pwd_mode == 'PWD'):
        try:
            cwd = os.getcwd()
        except OSError as exc:
            if ('o such file' in str(exc)):
                cwd = None
            else:
                raise
        try:
            pwd = os.environ['PWD']
            pwd_real = str(Path(pwd).resolve().absolute())
            if ((cwd is not None) and (pwd_real != cwd)):
                _switch_to_getcwd('realpath of PWD=%s is %s whenever os.getcwd()=%s', pwd, pwd_real, cwd)
                return cwd
            return pwd
        except KeyError:
            _switch_to_getcwd('PWD env variable is no longer available')
            return cwd
    else:
        raise RuntimeError(('Must have not got here. pwd_mode must be either cwd or PWD. And it is now %r' % (_pwd_mode,)))
Try to return a CWD without dereferencing possible symlinks

This function will try to use PWD environment variable to provide a current
working directory, possibly with some directories along the path being
symlinks to other directories.  Unfortunately, PWD is used/set only by the
shell and such functions as `os.chdir` and `os.getcwd` nohow use or modify
it, thus `os.getcwd()` returns path with links dereferenced.

While returning current working directory based on PWD env variable we
verify that the directory is the same as `os.getcwd()` after resolving all
symlinks.  If that verification fails, we fall back to always use
`os.getcwd()`.

Initial decision to either use PWD env variable or os.getcwd() is done upon
the first call of this function.
def getpwd():
    'Try to return a CWD without dereferencing possible symlinks\n\n    This function will try to use PWD environment variable to provide a current\n    working directory, possibly with some directories along the path being\n    symlinks to other directories.  Unfortunately, PWD is used/set only by the\n    shell and such functions as `os.chdir` and `os.getcwd` nohow use or modify\n    it, thus `os.getcwd()` returns path with links dereferenced.\n\n    While returning current working directory based on PWD env variable we\n    verify that the directory is the same as `os.getcwd()` after resolving all\n    symlinks.  If that verification fails, we fall back to always use\n    `os.getcwd()`.\n\n    Initial decision to either use PWD env variable or os.getcwd() is done upon\n    the first call of this function.\n    '
    global _pwd_mode
    if (_pwd_mode is None):
        try:
            pwd = os.environ['PWD']
            if (on_windows and pwd and pwd.startswith('/')):
                _pwd_mode = 'cwd'
            else:
                _pwd_mode = 'PWD'
        except KeyError:
            _pwd_mode = 'cwd'
    if (_pwd_mode == 'cwd'):
        return os.getcwd()
    elif (_pwd_mode == 'PWD'):
        try:
            cwd = os.getcwd()
        except OSError as exc:
            if ('o such file' in str(exc)):
                cwd = None
            else:
                raise
        try:
            pwd = os.environ['PWD']
            pwd_real = str(Path(pwd).resolve().absolute())
            if ((cwd is not None) and (pwd_real != cwd)):
                _switch_to_getcwd('realpath of PWD=%s is %s whenever os.getcwd()=%s', pwd, pwd_real, cwd)
                return cwd
            return pwd
        except KeyError:
            _switch_to_getcwd('PWD env variable is no longer available')
            return cwd
    else:
        raise RuntimeError(('Must have not got here. pwd_mode must be either cwd or PWD. And it is now %r' % (_pwd_mode,)))