# Buggy code
def _add_name_vhost_if_necessary(self, vhost):
    """Add NameVirtualHost Directives if necessary for new vhost.

    NameVirtualHosts was a directive in Apache < 2.4
    https://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost

    :param vhost: New virtual host that was recently created.
    :type vhost: :class:`~letsencrypt_apache.obj.VirtualHost`

    """
    need_to_save = False

    # See if the exact address appears in any other vhost
    # Remember 1.1.1.1:* == 1.1.1.1 -> hence any()
    for addr in vhost.addrs:
        # In Apache 2.2, when a NameVirtualHost directive is not
        # set, "*" and "_default_" will conflict when sharing a port
        if addr.get_addr() in ("*", "_default_"):
            addrs = [
                obj.Addr(
                    (
                        a,
                        addr.get_port(),
                    )
                )
                for a in ("*", "_default_")
            ]

        for test_vh in self.vhosts:
            if (
                vhost.filep != test_vh.filep
                and any(test_addr in addrs for test_addr in test_vh.addrs)
                and not self.is_name_vhost(addr)
            ):
                self.add_name_vhost(addr)
                logger.info("Enabling NameVirtualHosts on %s", addr)
                need_to_save = True

    if need_to_save:
        self.save()


# Traceback (NameError)
2016-02-13 10:13:29,799:DEBUG:letsencrypt.error_handler:Calling registered functions
2016-02-13 10:13:29,802:DEBUG:letsencrypt.cli:Exiting abnormally:
Traceback (most recent call last):
File "/home/aaa/.local/share/letsencrypt/bin/letsencrypt", line 11, in <module>
sys.exit(main())
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt/cli.py", line 1987, in main
return config.func(config, plugins)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt/cli.py", line 667, in run
lineage.chain, lineage.fullchain)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt/client.py", line 355, in deploy_certificate
fullchain_path=fullchain_path)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt_apache/configurator.py", line 214, in deploy_cert
vhost = self.choose_vhost(domain)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt_apache/configurator.py", line 308, in choose_vhost
vhost = self.make_vhost_ssl(vhost)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt_apache/configurator.py", line 733, in make_vhost_ssl
self._add_name_vhost_if_necessary(ssl_vhost)
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt_apache/configurator.py", line 905, in _add_name_vhost_if_necessary
test_addr in test_vh.addrs) and
File "/home/aaa/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt_apache/configurator.py", line 905, in <genexpr>
test_addr in test_vh.addrs) and
NameError: free variable 'addrs' referenced before assignment in enclosing scope

# Fixed code
def _add_name_vhost_if_necessary(self, vhost):
    """Add NameVirtualHost Directives if necessary for new vhost.

    NameVirtualHosts was a directive in Apache < 2.4
    https://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost

    :param vhost: New virtual host that was recently created.
    :type vhost: :class:`~letsencrypt_apache.obj.VirtualHost`

    """
    need_to_save = False

    # See if the exact address appears in any other vhost
    # Remember 1.1.1.1:* == 1.1.1.1 -> hence any()
    for addr in vhost.addrs:
        # In Apache 2.2, when a NameVirtualHost directive is not
        # set, "*" and "_default_" will conflict when sharing a port
        addrs = set((addr,))
        if addr.get_addr() in ("*", "_default_"):
            addrs.update(
                obj.Addr(
                    (
                        a,
                        addr.get_port(),
                    )
                )
                for a in ("*", "_default_")
            )

        for test_vh in self.vhosts:
            if (
                vhost.filep != test_vh.filep
                and any(test_addr in addrs for test_addr in test_vh.addrs)
                and not self.is_name_vhost(addr)
            ):
                self.add_name_vhost(addr)
                logger.info("Enabling NameVirtualHosts on %s", addr)
                need_to_save = True

    if need_to_save:
        self.save()