# Buggy code
def list_vm_images(
    image_location=None, publisher_name=None, offer=None, sku=None, all=False
):  # pylint: disable=redefined-builtin
    """vm image list
    :param str image_location:Image location
    :param str publisher_name:Image publisher name
    :param str offer:Image offer name
    :param str sku:Image sku name
    :param bool all:Retrieve image list from live Azure service rather using an offline image list
    """
    load_thru_services = all

    if load_thru_services:
        all_images = load_images_thru_services(
            publisher_name, offer, sku, image_location
        )
    else:
        logger.warning(
            "You are viewing an offline list of images, use --all to retrieve an up-to-date list"
        )
        all_images = load_images_from_aliases_doc(publisher_name, offer, sku)

    for i in all_images:
        i["urn"] = ":".join([i["publisher"], i["offer"], i["sku"], i["version"]])
    return all_images


# Traceback (IOError)
[Errno 24] Too many open files
Traceback (most recent call last):
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/main.py", line 36, in main
cmd_result = APPLICATION.execute(args)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/application.py", line 210, in execute
result = expanded_arg.func(params)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 289, in __call__
return self.handler(*args, **kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 488, in _execute_command
reraise(*sys.exc_info())
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 465, in _execute_command
result = op(client, **kwargs) if client else op(**kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/command_modules/vm/custom.py", line 211, in list_vm_images
all_images = load_images_thru_services(publisher_name, offer, sku, image_location)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/command_modules/vm/_actions.py", line 67, in load_images_thru_services
t.result()  # don't use the result but expose exceptions from the threads
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/concurrent/futures/_base.py", line 422, in result
return self.__get_result()
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/concurrent/futures/thread.py", line 62, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/cli/command_modules/vm/_actions.py", line 50, in _load_images_from_publisher
images = client.virtual_machine_images.list(location, publisher, o.name, s.name)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/azure/mgmt/compute/compute/v2016_04_30_preview/operations/virtual_machine_images_operations.py", line 172, in list
response = self._client.send(request, header_parameters, **operation_config)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/msrest/service_client.py", line 180, in send
**kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/requests/sessions.py", line 513, in request
resp = self.send(prep, **send_kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/requests/sessions.py", line 623, in send
r = adapter.send(request, **kwargs)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/connectionpool.py", line 345, in _make_request
self._validate_conn(conn)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/connectionpool.py", line 844, in _validate_conn
conn.connect()
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/connection.py", line 326, in connect
ssl_context=context)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/util/ssl_.py", line 325, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py", line 440, in wrap_socket
rd = util.wait_for_read(sock, sock.gettimeout())
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/util/wait.py", line 33, in wait_for_read
return _wait_for_io_events(socks, EVENT_READ, timeout)
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/util/wait.py", line 22, in _wait_for_io_events
with DefaultSelector() as selector:
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/util/selectors.py", line 581, in DefaultSelector
return _DEFAULT_SELECTOR()
File "/Users/debekoe/lib/azure-cli/lib/python2.7/site-packages/urllib3/util/selectors.py", line 461, in __init__
self._kqueue = select.kqueue()
IOError: [Errno 24] Too many open files

# Fixed code
def list_vm_images(
    image_location=None, publisher_name=None, offer=None, sku=None, all=False
):  # pylint: disable=redefined-builtin
    """vm image list
    :param str image_location:Image location
    :param str publisher_name:Image publisher name, partial name is accepted
    :param str offer:Image offer name, partial name is accepted
    :param str sku:Image sku name, partial name is accepted
    :param bool all:Retrieve image list from live Azure service rather using an offline image list
    """
    load_thru_services = all

    if load_thru_services:
        if not publisher_name and not offer and not sku:
            logger.warning(
                "You are retrieving all the images from server which could take more than a minute. "
                "To shorten the wait, provide '--publisher', '--offer' or '--sku'. Partial name search "
                "is supported."
            )
        all_images = load_images_thru_services(
            publisher_name, offer, sku, image_location
        )
    else:
        logger.warning(
            "You are viewing an offline list of images, use --all to retrieve an up-to-date list"
        )
        all_images = load_images_from_aliases_doc(publisher_name, offer, sku)

    for i in all_images:
        i["urn"] = ":".join([i["publisher"], i["offer"], i["sku"], i["version"]])
    return all_images