Skip to content

Commit

Permalink
Privatize nurseries
Browse files Browse the repository at this point in the history
  • Loading branch information
bengartner committed Jun 14, 2019
1 parent 330b53b commit c149ea4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)
from .. import _core
from .._deprecate import deprecated
from .._util import Final
from .._util import Final, NoPublicConstructor

# At the bottom of this file there's also some "clever" code that generates
# wrapper functions for runner and io manager methods, and adds them to
Expand Down Expand Up @@ -717,7 +717,7 @@ class NurseryManager:
async def __aenter__(self):
self._scope = CancelScope()
self._scope.__enter__()
self._nursery = Nursery(current_task(), self._scope)
self._nursery = Nursery._create(current_task(), self._scope)
return self._nursery

@enable_ki_protection
Expand Down Expand Up @@ -761,7 +761,7 @@ def open_nursery():
return NurseryManager()


class Nursery:
class Nursery(metaclass=NoPublicConstructor):
def __init__(self, parent_task, cancel_scope):
self._parent_task = parent_task
parent_task._child_nurseries.append(self)
Expand Down
24 changes: 24 additions & 0 deletions trio/_core/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,30 @@ async def inner():
_core.run(inner)


def test_Nursery_init():
check_Nursery_error = pytest.raises(
TypeError, match='no public constructor available'
)

with check_Nursery_error:
_core._run.Nursery(None, None)


async def test_Nursery_private_init():
# context manager creation should not raise
async with _core.open_nursery() as nursery:
assert False == nursery._closed


def test_Nursery_subclass():
with pytest.raises(
TypeError, match='`Nursery` does not support subclassing'
):

class Subclass(_core._run.Nursery):
pass


def test_Cancelled_init():
check_Cancelled_error = pytest.raises(
TypeError, match='no public constructor available'
Expand Down

0 comments on commit c149ea4

Please sign in to comment.