def METHOD_NAME(env, fail_on_override=False, return_old=False):
    old_values = {} if return_old else None
    for key, value in env.items():
        if fail_on_override or return_old:
            # old_value = GetConfigOption(str(key))
            old_value = os.environ.get(key)
            if fail_on_override and old_value != value:
                raise Exception(
                    'Would override previous value of %s: %s with %s'
                    % (key, old_value, value)
                )
            elif old_value != value:
                old_values[key] = old_value
        # SetConfigOption(str(key), str(value))
        if value is not None:
            os.environ[key] = value
    return old_values