def _safe_get(obj, key, default=None):
    """This acts like obj.get(key, default), except that if obj[key] exists but
    is None, we still return default rather than the accessed result. Also, if
    obj happens to be None, we return default rather than raising an exception.

    To see the difference, suppose obj = {"a": None}. Then obj.get("a", 1) is
    None but _safe_get(obj, "a", 1) is 1.

    Since obj can be None, _safe_get can also be nested without checking for
    None each time: _safe_get(_safe_get({}, "a"), "b", 1) is 1. Thus in some
    cases a default need only be specified at the end."""
    if obj is None:
        return default
    result = obj.get(key)
    if result is None:
        result = default
    return result