def setdefault(doc, key, default):
    """Return the value in 'doc' with key 'key' if present and not None.

    Otherwise set the value to default and return it.
    """
    value = doc.setdefault(key, default)
    if value is not None:
        return value
    doc[key] = default
    return default