Repository: pypa/setuptools

Issue:
pkg_resources: UnboundLocalError: local variable 'dist' referenced before assignment
When trying to run my package's tests frozen via cx_Freeze on Windows, I just got this:

```
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\pkg_resources\__init__.py", line 2986, in _initialize_master_working_set
    del dist
UnboundLocalError: local variable 'dist' referenced before assignment
```

full traceback:

```
unittests-frozen runtests: commands[2] | C:\projects\qutebrowser\.tox\unittests-frozen/build/run-frozen-tests
Traceback (most recent call last):
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "scripts/dev/run_frozen_tests.py", line 38, in <module>
    pytest_rerunfailures, pytest_warnings]))
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\config.py", line 39, in main
    config = _prepareconfig(args, plugins)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\config.py", line 118, in _prepareconfig
    pluginmanager=pluginmanager, args=args)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 724, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 595, in execute
    return _wrapped_call(hook_impl.function(*args), self.execute)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 249, in _wrapped_call
    wrap_controller.send(call_outcome)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\helpconfig.py", line 28, in pytest_cmdline_parse
    config = outcome.get_result()
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 278, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 264, in __init__
    self.result = func()
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\config.py", line 861, in pytest_cmdline_parse
    self.parse(args)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\config.py", line 966, in parse
    self._preparse(args, addopts=addopts)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\config.py", line 927, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 495, in load_setuptools_entrypoints
    from pkg_resources import iter_entry_points, DistributionNotFound
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\pkg_resources\__init__.py", line 2958, in <module>
    @_call_aside
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\pkg_resources\__init__.py", line 2944, in _call_aside
    f(*args, **kwargs)
  File "C:\projects\qutebrowser\.tox\unittests-frozen\lib\site-packages\pkg_resources\__init__.py", line 2986, in _initialize_master_working_set
    del dist
UnboundLocalError: local variable 'dist' referenced before assignment
```

This seems to be introduced in #698 (cc @minrk) and happens when the `for dist in working_set:` above doesn't assign to `dist` because `working_set` is empty.



Discussion:

Resolving patch:
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -2981,6 +2981,7 @@ def _initialize_master_working_set():
     # ensure that all distributions added to the working set in the future
     # (e.g. by calling ``require()``) will get activated as well,
     # with higher priority (replace=True).
+    dist = None # ensure dist is defined for del dist below
     for dist in working_set:
         dist.activate(replace=False)
     del dist


Test patch: