| 1 | n/a | """PEP 366 ("Main module explicit relative imports") specifies the |
|---|
| 2 | n/a | semantics for the __package__ attribute on modules. This attribute is |
|---|
| 3 | n/a | used, when available, to detect which package a module belongs to (instead |
|---|
| 4 | n/a | of using the typical __path__/__name__ test). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | """ |
|---|
| 7 | n/a | import unittest |
|---|
| 8 | n/a | from .. import util |
|---|
| 9 | n/a | from . import util as import_util |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | class Using__package__(unittest.TestCase): |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | """Use of __package__ supercedes the use of __name__/__path__ to calculate |
|---|
| 15 | n/a | what package a module belongs to. The basic algorithm is [__package__]:: |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | def resolve_name(name, package, level): |
|---|
| 18 | n/a | level -= 1 |
|---|
| 19 | n/a | base = package.rsplit('.', level)[0] |
|---|
| 20 | n/a | return '{0}.{1}'.format(base, name) |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | But since there is no guarantee that __package__ has been set (or not been |
|---|
| 23 | n/a | set to None [None]), there has to be a way to calculate the attribute's value |
|---|
| 24 | n/a | [__name__]:: |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def calc_package(caller_name, has___path__): |
|---|
| 27 | n/a | if has__path__: |
|---|
| 28 | n/a | return caller_name |
|---|
| 29 | n/a | else: |
|---|
| 30 | n/a | return caller_name.rsplit('.', 1)[0] |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | Then the normal algorithm for relative name imports can proceed as if |
|---|
| 33 | n/a | __package__ had been set. |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | """ |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def test_using___package__(self): |
|---|
| 38 | n/a | # [__package__] |
|---|
| 39 | n/a | with util.mock_modules('pkg.__init__', 'pkg.fake') as importer: |
|---|
| 40 | n/a | with util.import_state(meta_path=[importer]): |
|---|
| 41 | n/a | import_util.import_('pkg.fake') |
|---|
| 42 | n/a | module = import_util.import_('', |
|---|
| 43 | n/a | globals={'__package__': 'pkg.fake'}, |
|---|
| 44 | n/a | fromlist=['attr'], level=2) |
|---|
| 45 | n/a | self.assertEqual(module.__name__, 'pkg') |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def test_using___name__(self, package_as_None=False): |
|---|
| 48 | n/a | # [__name__] |
|---|
| 49 | n/a | globals_ = {'__name__': 'pkg.fake', '__path__': []} |
|---|
| 50 | n/a | if package_as_None: |
|---|
| 51 | n/a | globals_['__package__'] = None |
|---|
| 52 | n/a | with util.mock_modules('pkg.__init__', 'pkg.fake') as importer: |
|---|
| 53 | n/a | with util.import_state(meta_path=[importer]): |
|---|
| 54 | n/a | import_util.import_('pkg.fake') |
|---|
| 55 | n/a | module = import_util.import_('', globals= globals_, |
|---|
| 56 | n/a | fromlist=['attr'], level=2) |
|---|
| 57 | n/a | self.assertEqual(module.__name__, 'pkg') |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def test_None_as___package__(self): |
|---|
| 60 | n/a | # [None] |
|---|
| 61 | n/a | self.test_using___name__(package_as_None=True) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | def test_bad__package__(self): |
|---|
| 64 | n/a | globals = {'__package__': '<not real>'} |
|---|
| 65 | n/a | with self.assertRaises(SystemError): |
|---|
| 66 | n/a | import_util.import_('', globals, {}, ['relimport'], 1) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def test_bunk__package__(self): |
|---|
| 69 | n/a | globals = {'__package__': 42} |
|---|
| 70 | n/a | with self.assertRaises(TypeError): |
|---|
| 71 | n/a | import_util.import_('', globals, {}, ['relimport'], 1) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | @import_util.importlib_only |
|---|
| 75 | n/a | class Setting__package__(unittest.TestCase): |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | """Because __package__ is a new feature, it is not always set by a loader. |
|---|
| 78 | n/a | Import will set it as needed to help with the transition to relying on |
|---|
| 79 | n/a | __package__. |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | For a top-level module, __package__ is set to None [top-level]. For a |
|---|
| 82 | n/a | package __name__ is used for __package__ [package]. For submodules the |
|---|
| 83 | n/a | value is __name__.rsplit('.', 1)[0] [submodule]. |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | """ |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # [top-level] |
|---|
| 88 | n/a | def test_top_level(self): |
|---|
| 89 | n/a | with util.mock_modules('top_level') as mock: |
|---|
| 90 | n/a | with util.import_state(meta_path=[mock]): |
|---|
| 91 | n/a | del mock['top_level'].__package__ |
|---|
| 92 | n/a | module = import_util.import_('top_level') |
|---|
| 93 | n/a | self.assertEqual(module.__package__, '') |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | # [package] |
|---|
| 96 | n/a | def test_package(self): |
|---|
| 97 | n/a | with util.mock_modules('pkg.__init__') as mock: |
|---|
| 98 | n/a | with util.import_state(meta_path=[mock]): |
|---|
| 99 | n/a | del mock['pkg'].__package__ |
|---|
| 100 | n/a | module = import_util.import_('pkg') |
|---|
| 101 | n/a | self.assertEqual(module.__package__, 'pkg') |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | # [submodule] |
|---|
| 104 | n/a | def test_submodule(self): |
|---|
| 105 | n/a | with util.mock_modules('pkg.__init__', 'pkg.mod') as mock: |
|---|
| 106 | n/a | with util.import_state(meta_path=[mock]): |
|---|
| 107 | n/a | del mock['pkg.mod'].__package__ |
|---|
| 108 | n/a | pkg = import_util.import_('pkg.mod') |
|---|
| 109 | n/a | module = getattr(pkg, 'mod') |
|---|
| 110 | n/a | self.assertEqual(module.__package__, 'pkg') |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | def test_main(): |
|---|
| 114 | n/a | from test.support import run_unittest |
|---|
| 115 | n/a | run_unittest(Using__package__, Setting__package__) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if __name__ == '__main__': |
|---|
| 119 | n/a | test_main() |
|---|