Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 1 | import functools |
Brett Cannon | 6d2bb7f | 2009-08-27 23:44:18 +0000 | [diff] [blame] | 2 | import importlib |
Brett Cannon | 6d2bb7f | 2009-08-27 23:44:18 +0000 | [diff] [blame] | 3 | import unittest |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 4 | |
| 5 | |
| 6 | using___import__ = False |
| 7 | |
| 8 | |
| 9 | def import_(*args, **kwargs): |
| 10 | """Delegate to allow for injecting different implementations of import.""" |
| 11 | if using___import__: |
| 12 | return __import__(*args, **kwargs) |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 13 | else: |
Brett Cannon | a650360 | 2009-08-30 18:40:23 +0000 | [diff] [blame] | 14 | return importlib.__import__(*args, **kwargs) |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 15 | |
| 16 | |
Brett Cannon | 44b28a9 | 2009-08-30 18:59:21 +0000 | [diff] [blame] | 17 | def importlib_only(fxn): |
| 18 | """Decorator to skip a test if using __builtins__.__import__.""" |
| 19 | return unittest.skipIf(using___import__, "importlib-specific test")(fxn) |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def mock_path_hook(*entries, importer): |
| 23 | """A mock sys.path_hooks entry.""" |
| 24 | def hook(entry): |
| 25 | if entry not in entries: |
| 26 | raise ImportError |
| 27 | return importer |
| 28 | return hook |