| 1 |
from test.support import is_apple_mobile |
| 2 |
from test.test_importlib import abc, util |
| 3 |
|
| 4 |
machinery = util.import_importlib('importlib.machinery') |
| 5 |
|
| 6 |
import unittest |
| 7 |
import sys |
| 8 |
|
| 9 |
|
| 10 |
class FinderTests(abc.FinderTests): |
| 11 |
|
| 12 |
"""Test the finder for extension modules.""" |
| 13 |
|
| 14 |
def setUp(self): |
| 15 |
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS: |
| 16 |
raise unittest.SkipTest("Requires dynamic loading support.") |
| 17 |
if util.EXTENSIONS.name in sys.builtin_module_names: |
| 18 |
raise unittest.SkipTest( |
| 19 |
f"{util.EXTENSIONS.name} is a builtin module" |
| 20 |
) |
| 21 |
|
| 22 |
def find_spec(self, fullname): |
| 23 |
if is_apple_mobile: |
| 24 |
# Apple mobile platforms require a specialist loader that uses |
| 25 |
# .fwork files as placeholders for the true `.so` files. |
| 26 |
loaders = [ |
| 27 |
( |
| 28 |
self.machinery.AppleFrameworkLoader, |
| 29 |
[ |
| 30 |
ext.replace(".so", ".fwork") |
| 31 |
for ext in self.machinery.EXTENSION_SUFFIXES |
| 32 |
] |
| 33 |
) |
| 34 |
] |
| 35 |
else: |
| 36 |
loaders = [ |
| 37 |
( |
| 38 |
self.machinery.ExtensionFileLoader, |
| 39 |
self.machinery.EXTENSION_SUFFIXES |
| 40 |
) |
| 41 |
] |
| 42 |
|
| 43 |
importer = self.machinery.FileFinder(util.EXTENSIONS.path, *loaders) |
| 44 |
|
| 45 |
return importer.find_spec(fullname) |
| 46 |
|
| 47 |
def test_module(self): |
| 48 |
self.assertTrue(self.find_spec(util.EXTENSIONS.name)) |
| 49 |
|
| 50 |
# No extension module as an __init__ available for testing. |
| 51 |
test_package = test_package_in_package = None |
| 52 |
|
| 53 |
# No extension module in a package available for testing. |
| 54 |
test_module_in_package = None |
| 55 |
|
| 56 |
# Extension modules cannot be an __init__ for a package. |
| 57 |
test_package_over_module = None |
| 58 |
|
| 59 |
def test_failure(self): |
| 60 |
self.assertIsNone(self.find_spec('asdfjkl;')) |
| 61 |
|
| 62 |
|
| 63 |
(Frozen_FinderTests, |
| 64 |
Source_FinderTests |
| 65 |
) = util.test_both(FinderTests, machinery=machinery) |
| 66 |
|
| 67 |
|
| 68 |
if __name__ == '__main__': |
| 69 |
unittest.main() |