mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	bpo-40275: Avoid importing asyncio in test.support (GH-19600)
* Import asyncio lazily in unittest (only when IsolatedAsyncioTestCase is used). * Import asyncio.events lazily in test.support.
This commit is contained in:
		
							parent
							
								
									d4f3923d59
								
							
						
					
					
						commit
						3c8a5b459d
					
				
					 3 changed files with 19 additions and 2 deletions
				
			
		| 
						 | 
					@ -3,7 +3,6 @@
 | 
				
			||||||
if __name__ != 'test.support':
 | 
					if __name__ != 'test.support':
 | 
				
			||||||
    raise ImportError('support must be imported from the test package')
 | 
					    raise ImportError('support must be imported from the test package')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import asyncio.events
 | 
					 | 
				
			||||||
import collections.abc
 | 
					import collections.abc
 | 
				
			||||||
import contextlib
 | 
					import contextlib
 | 
				
			||||||
import errno
 | 
					import errno
 | 
				
			||||||
| 
						 | 
					@ -3260,6 +3259,7 @@ SMALLEST = _SMALLEST()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def maybe_get_event_loop_policy():
 | 
					def maybe_get_event_loop_policy():
 | 
				
			||||||
    """Return the global event loop policy if one is set, else return None."""
 | 
					    """Return the global event loop policy if one is set, else return None."""
 | 
				
			||||||
 | 
					    import asyncio.events
 | 
				
			||||||
    return asyncio.events._event_loop_policy
 | 
					    return asyncio.events._event_loop_policy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Helpers for testing hashing.
 | 
					# Helpers for testing hashing.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -57,7 +57,6 @@ __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
 | 
				
			||||||
__unittest = True
 | 
					__unittest = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .result import TestResult
 | 
					from .result import TestResult
 | 
				
			||||||
from .async_case import IsolatedAsyncioTestCase
 | 
					 | 
				
			||||||
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
 | 
					from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
 | 
				
			||||||
                   skipIf, skipUnless, expectedFailure)
 | 
					                   skipIf, skipUnless, expectedFailure)
 | 
				
			||||||
from .suite import BaseTestSuite, TestSuite
 | 
					from .suite import BaseTestSuite, TestSuite
 | 
				
			||||||
| 
						 | 
					@ -66,6 +65,7 @@ from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
 | 
				
			||||||
from .main import TestProgram, main
 | 
					from .main import TestProgram, main
 | 
				
			||||||
from .runner import TextTestRunner, TextTestResult
 | 
					from .runner import TextTestRunner, TextTestResult
 | 
				
			||||||
from .signals import installHandler, registerResult, removeResult, removeHandler
 | 
					from .signals import installHandler, registerResult, removeResult, removeHandler
 | 
				
			||||||
 | 
					# IsolatedAsyncioTestCase will be imported lazily.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# deprecated
 | 
					# deprecated
 | 
				
			||||||
_TextTestResult = TextTestResult
 | 
					_TextTestResult = TextTestResult
 | 
				
			||||||
| 
						 | 
					@ -78,3 +78,18 @@ def load_tests(loader, tests, pattern):
 | 
				
			||||||
    # top level directory cached on loader instance
 | 
					    # top level directory cached on loader instance
 | 
				
			||||||
    this_dir = os.path.dirname(__file__)
 | 
					    this_dir = os.path.dirname(__file__)
 | 
				
			||||||
    return loader.discover(start_dir=this_dir, pattern=pattern)
 | 
					    return loader.discover(start_dir=this_dir, pattern=pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Lazy import of IsolatedAsyncioTestCase from .async_case
 | 
				
			||||||
 | 
					# It imports asyncio, which is relatively heavy, but most tests
 | 
				
			||||||
 | 
					# do not need it.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def __dir__():
 | 
				
			||||||
 | 
					    return globals().keys() | {'IsolatedAsyncioTestCase'}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def __getattr__(name):
 | 
				
			||||||
 | 
					    if name == 'IsolatedAsyncioTestCase':
 | 
				
			||||||
 | 
					        global IsolatedAsyncioTestCase
 | 
				
			||||||
 | 
					        from .async_case import IsolatedAsyncioTestCase
 | 
				
			||||||
 | 
					        return IsolatedAsyncioTestCase
 | 
				
			||||||
 | 
					    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					The :mod:`asyncio` package is now imported lazily in :mod:`unittest` only
 | 
				
			||||||
 | 
					when the :class:`~unittest.IsolatedAsyncioTestCase` class is used.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue