# Buggy code
def sync_avatar_from_ldap(self, user: UserProfile, ldap_user: _LDAPUser) -> None:
    if "avatar" in settings.AUTH_LDAP_USER_ATTR_MAP:
        # We do local imports here to avoid import loops
        from zerver.lib.upload import upload_avatar_image
        from zerver.lib.actions import do_change_avatar_fields
        from io import BytesIO

        avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP["avatar"]
        if avatar_attr_name not in ldap_user.attrs:  # nocoverage
            # If this specific user doesn't have e.g. a
            # thumbnailPhoto set in LDAP, just skip that user.
            return
        upload_avatar_image(BytesIO(ldap_user.attrs[avatar_attr_name][0]), user, user)
        do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./zerver/views/auth.py", line 647, in login_page
extra_context=extra_context, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/views.py", line 54, in inner
return func(*args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/views.py", line 150, in login
)(request)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/views.py", line 90, in dispatch
return super(LoginView, self).dispatch(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/views/generic/edit.py", line 182, in post
if form.is_valid():
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/forms/forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/forms/forms.py", line 385, in full_clean
self._clean_form()
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/forms/forms.py", line 412, in _clean_form
cleaned_data = self.clean()
File "./zerver/forms.py", line 283, in clean
realm=realm, return_data=return_data)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 70, in authenticate
user = _authenticate_with_backend(backend, backend_path, request, credentials)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 116, in _authenticate_with_backend
return backend.authenticate(*args, **credentials)
File "./zproject/backends.py", line 431, in authenticate
password=password)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django_auth_ldap/backend.py", line 150, in authenticate
user = self.authenticate_ldap_user(ldap_user, password)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django_auth_ldap/backend.py", line 210, in authenticate_ldap_user
return ldap_user.authenticate(password)
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django_auth_ldap/backend.py", line 350, in authenticate
self._get_or_create_user()
File "/home/zulip/deployments/2019-05-20-11-38-33/zulip-py3-venv/lib/python3.6/site-packages/django_auth_ldap/backend.py", line 591, in _get_or_create_user
self._user, built = self.backend.get_or_build_user(username, self)
File "./zproject/backends.py", line 500, in get_or_build_user
self.sync_avatar_from_ldap(user_profile, ldap_user)
File "./zproject/backends.py", line 307, in sync_avatar_from_ldap
upload_avatar_image(BytesIO(ldap_user.attrs[avatar_attr_name][0]), user, user)
File "./zerver/lib/upload.py", line 769, in upload_avatar_image
upload_backend.upload_avatar_image(user_file, acting_user_profile, target_user_profile)
File "./zerver/lib/upload.py", line 402, in upload_avatar_image
content_type = guess_type(user_file.name)[0]
AttributeError: '_io.BytesIO' object has no attribute 'name'

# Fixed code
def sync_avatar_from_ldap(self, user: UserProfile, ldap_user: _LDAPUser) -> None:
    if "avatar" in settings.AUTH_LDAP_USER_ATTR_MAP:
        # We do local imports here to avoid import loops
        from zerver.lib.upload import upload_avatar_image
        from zerver.lib.actions import do_change_avatar_fields
        from io import BytesIO

        avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP["avatar"]
        if avatar_attr_name not in ldap_user.attrs:  # nocoverage
            # If this specific user doesn't have e.g. a
            # thumbnailPhoto set in LDAP, just skip that user.
            return

        io = BytesIO(ldap_user.attrs[avatar_attr_name][0])
        # Structurally, to make the S3 backend happy, we need to
        # provide a Content-Type; since that isn't specified in
        # any metadata, we auto-detect it.
        content_type = magic.from_buffer(copy.deepcopy(io).read()[0:1024], mime=True)
        if content_type.startswith("image/"):
            upload_avatar_image(io, user, user, content_type=content_type)
            do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER)
        else:
            logging.warning(
                "Could not parse %s field for user %s" % (avatar_attr_name, user.id)
            )