SiteSupport/gevent — pull request #198 by jgehrcke (2012-11-12 19:05:03+00:00)
Title: Add support for creation of a new default loop after destroying it.

`hub.destroy(destroy_loop=True)` destroys the default loop. I expect a subsequent call to `gevent.get_hub(default=True)` to create a new hub including a new default event loop. However, the explicit `default=True` becomes overridden within the `core.loop` constructor. We end up without default event loop.

Currently, the logic of the `default` argument treatment in the `hub.Hub` and `core.loop` constructors is not straight forward. With the changes I have made,
- the logic is clearer (the `default` argument to `get_hub` has highest priority and is not overridden anymore)
- gevent's internal behavior is not changed (`get_hub()` is never called with argument; see below for test results)
- the following test runs fine (commented version of `greentests/test__destroy.py`, now also covering the new scenario):

```python
import gevent
# Loop of initial Hub is default loop.
hub = gevent.get_hub()
assert hub.loop.default, hub

# Destroy hub. Does not destroy default loop if not explicitly told to.
hub.destroy()
hub = gevent.get_hub()
assert hub.loop.default, hub

# Destroy hub including default loop.
hub.destroy(destroy_loop=True)
# Create new hub and explicitly request creation of a new default loop.
hub = gevent.get_hub(default=True)
assert hub.loop.default, hub

# Destroy hub including default loop.
hub.destroy(destroy_loop=True)
# Create new non-default loop in new hub.
hub = gevent.get_hub()
assert not hub.loop.default, hub
hub.destroy()
```

I ran the unit tests via
```
python testrunner.py --full --expected ../known_failures.txt
```
with Python 2.7.3 on Ubuntu 10.04 before and after my changes. The changes do not affect the test results negatively.

Pros:
- I don't see why it should not be allowed to create a new default loop after having it destroyed ;)
- destroying and creating a new default event loop is a clean way of surviving a fork on Unix
- other scenarios might come up in the future where re-creation of the default event loop is required.

Cons: I'm not sure if there are any.