# Buggy code
def upload_avatar_image(
    self,
    user_file: File,
    acting_user_profile: UserProfile,
    target_user_profile: UserProfile,
) -> None:
    file_path = user_avatar_path(target_user_profile)

    image_data = user_file.read()
    self.write_avatar_images(file_path, image_data)


# 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 upload_avatar_image(
    self,
    user_file: File,
    acting_user_profile: UserProfile,
    target_user_profile: UserProfile,
    content_type: Optional[str] = None,
) -> None:
    file_path = user_avatar_path(target_user_profile)

    image_data = user_file.read()
    self.write_avatar_images(file_path, image_data)