def _m_mangled_attr_name(name, cls_name):
    """Returns `name` as mangled by Python for occurences of `__%s` % name
       inside the definition of a class with name `cls_name`.

       >>> print (_m_mangled_attr_name ("foo", "Bar"))
       _Bar__foo
       >>> print (_m_mangled_attr_name ("foo", "_Bar"))
       _Bar__foo
       >>> print (_m_mangled_attr_name ("foo", "_Bar_"))
       _Bar___foo
    """
    if cls_name.startswith('_'):
        format = '%s__%s'
    else:
        format = '_%s__%s'
    return format % (cls_name, name)
