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