diff --git a/tests/test_base.py b/tests/test_base.py index 64738f6..1bb7dfe 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -4,6 +4,7 @@ import threading import time import uvloop +import unittest from unittest import mock from uvloop._testbase import UVTestCase, AIOTestCase @@ -398,3 +399,17 @@ class TestBaseUV(_TestBase, UVTestCase): class TestBaseAIO(_TestBase, AIOTestCase): pass + + +class TestPolicy(unittest.TestCase): + + def test_uvloop_policy(self): + try: + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + loop = asyncio.new_event_loop() + try: + self.assertIsInstance(loop, uvloop._Loop) + finally: + loop.close() + finally: + asyncio.set_event_loop_policy(None) diff --git a/uvloop/__init__.py b/uvloop/__init__.py index 7d5a224..14d3888 100644 --- a/uvloop/__init__.py +++ b/uvloop/__init__.py @@ -1,12 +1,34 @@ import asyncio +from asyncio.events import BaseDefaultEventLoopPolicy as __BasePolicy + from . import includes as __includes from .loop import Loop as __BaseLoop +__all__ = ('new_event_loop', 'EventLoopPolicy') + + class _Loop(__BaseLoop, asyncio.AbstractEventLoop): pass def new_event_loop(): + """Return a new event loop.""" return _Loop() + + +class EventLoopPolicy(__BasePolicy): + """Event loop policy. + + The preferred way to make your application use uvloop: + + >>> import asyncio + >>> import uvloop + >>> asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + >>> asyncio.get_event_loop() + + """ + + def _loop_factory(self): + return new_event_loop() diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index 5d2c4b4..45a8ee9 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -764,9 +764,12 @@ cdef class Loop: # Public API def __repr__(self): - return ('<%s running=%s closed=%s debug=%s>' - % (self.__class__.__name__, self.is_running(), - self.is_closed(), self.get_debug())) + return '<{}.{} running={} closed={} debug={}>'.format( + self.__class__.__module__, + self.__class__.__name__, + self.is_running(), + self.is_closed(), + self.get_debug()) def call_soon(self, callback, *args): if self._debug == 1: