From 190e78603a4ce58a2f248fdf8a3472fa1fc6c064 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 29 Jan 2015 17:32:39 +0100 Subject: Python issue #23243: On Python 3.4 and newer, emit a ResourceWarning when an event loop or a transport is not explicitly closed --- asyncio/base_events.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'asyncio/base_events.py') diff --git a/asyncio/base_events.py b/asyncio/base_events.py index e40d3ad..7108f25 100644 --- a/asyncio/base_events.py +++ b/asyncio/base_events.py @@ -26,6 +26,7 @@ import threading import time import traceback import sys +import warnings from . import coroutines from . import events @@ -333,6 +334,16 @@ class BaseEventLoop(events.AbstractEventLoop): """Returns True if the event loop was closed.""" return self._closed + # On Python 3.3 and older, objects with a destructor part of a reference + # cycle are never destroyed. It's not more the case on Python 3.4 thanks + # to the PEP 442. + if sys.version_info >= (3, 4): + def __del__(self): + if not self.is_closed(): + warnings.warn("unclosed event loop %r" % self, ResourceWarning) + if not self.is_running(): + self.close() + def is_running(self): """Returns True if the event loop is running.""" return (self._owner is not None) -- cgit v1.2.1