| 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 |
| 8 |
import warnings |
| 9 |
from test.test_importlib import util |
| 10 |
|
| 11 |
|
| 12 |
class Using__package__: |
| 13 |
|
| 14 |
"""Use of __package__ supersedes 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 |
|
| 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__]:: |
| 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 import_module(self, globals_): |
| 38 |
with self.mock_modules('pkg.__init__', 'pkg.fake') as importer: |
| 39 |
with util.import_state(meta_path=[importer]): |
| 40 |
self.__import__('pkg.fake') |
| 41 |
module = self.__import__('', |
| 42 |
globals=globals_, |
| 43 |
fromlist=['attr'], level=2) |
| 44 |
return module |
| 45 |
|
| 46 |
def test_using___package__(self): |
| 47 |
# [__package__] |
| 48 |
module = self.import_module({'__package__': 'pkg.fake'}) |
| 49 |
self.assertEqual(module.__name__, 'pkg') |
| 50 |
|
| 51 |
def test_using___name__(self): |
| 52 |
# [__name__] |
| 53 |
with warnings.catch_warnings(): |
| 54 |
warnings.simplefilter("ignore") |
| 55 |
module = self.import_module({'__name__': 'pkg.fake', |
| 56 |
'__path__': []}) |
| 57 |
self.assertEqual(module.__name__, 'pkg') |
| 58 |
|
| 59 |
def test_warn_when_using___name__(self): |
| 60 |
with self.assertWarns(ImportWarning): |
| 61 |
self.import_module({'__name__': 'pkg.fake', '__path__': []}) |
| 62 |
|
| 63 |
def test_None_as___package__(self): |
| 64 |
# [None] |
| 65 |
with warnings.catch_warnings(): |
| 66 |
warnings.simplefilter("ignore") |
| 67 |
module = self.import_module({ |
| 68 |
'__name__': 'pkg.fake', '__path__': [], '__package__': None }) |
| 69 |
self.assertEqual(module.__name__, 'pkg') |
| 70 |
|
| 71 |
def test_spec_fallback(self): |
| 72 |
# If __package__ isn't defined, fall back on __spec__.parent. |
| 73 |
module = self.import_module({'__spec__': FakeSpec('pkg.fake')}) |
| 74 |
self.assertEqual(module.__name__, 'pkg') |
| 75 |
|
| 76 |
def test_warn_when_package_and_spec_disagree(self): |
| 77 |
# Raise a DeprecationWarning if __package__ != __spec__.parent. |
| 78 |
with self.assertWarns(DeprecationWarning): |
| 79 |
self.import_module({'__package__': 'pkg.fake', |
| 80 |
'__spec__': FakeSpec('pkg.fakefake')}) |
| 81 |
|
| 82 |
def test_bad__package__(self): |
| 83 |
globals = {'__package__': '<not real>'} |
| 84 |
with self.assertRaises(ModuleNotFoundError): |
| 85 |
self.__import__('', globals, {}, ['relimport'], 1) |
| 86 |
|
| 87 |
def test_bunk__package__(self): |
| 88 |
globals = {'__package__': 42} |
| 89 |
with self.assertRaises(TypeError): |
| 90 |
self.__import__('', globals, {}, ['relimport'], 1) |
| 91 |
|
| 92 |
|
| 93 |
class FakeSpec: |
| 94 |
def __init__(self, parent): |
| 95 |
self.parent = parent |
| 96 |
|
| 97 |
|
| 98 |
class Using__package__PEP451(Using__package__): |
| 99 |
mock_modules = util.mock_spec |
| 100 |
|
| 101 |
|
| 102 |
(Frozen_UsingPackagePEP451, |
| 103 |
Source_UsingPackagePEP451 |
| 104 |
) = util.test_both(Using__package__PEP451, __import__=util.__import__) |
| 105 |
|
| 106 |
|
| 107 |
class Setting__package__: |
| 108 |
|
| 109 |
"""Because __package__ is a new feature, it is not always set by a loader. |
| 110 |
Import will set it as needed to help with the transition to relying on |
| 111 |
__package__. |
| 112 |
|
| 113 |
For a top-level module, __package__ is set to None [top-level]. For a |
| 114 |
package __name__ is used for __package__ [package]. For submodules the |
| 115 |
value is __name__.rsplit('.', 1)[0] [submodule]. |
| 116 |
|
| 117 |
""" |
| 118 |
|
| 119 |
__import__ = util.__import__['Source'] |
| 120 |
|
| 121 |
# [top-level] |
| 122 |
def test_top_level(self): |
| 123 |
with self.mock_modules('top_level') as mock: |
| 124 |
with util.import_state(meta_path=[mock]): |
| 125 |
del mock['top_level'].__package__ |
| 126 |
module = self.__import__('top_level') |
| 127 |
self.assertEqual(module.__package__, '') |
| 128 |
|
| 129 |
# [package] |
| 130 |
def test_package(self): |
| 131 |
with self.mock_modules('pkg.__init__') as mock: |
| 132 |
with util.import_state(meta_path=[mock]): |
| 133 |
del mock['pkg'].__package__ |
| 134 |
module = self.__import__('pkg') |
| 135 |
self.assertEqual(module.__package__, 'pkg') |
| 136 |
|
| 137 |
# [submodule] |
| 138 |
def test_submodule(self): |
| 139 |
with self.mock_modules('pkg.__init__', 'pkg.mod') as mock: |
| 140 |
with util.import_state(meta_path=[mock]): |
| 141 |
del mock['pkg.mod'].__package__ |
| 142 |
pkg = self.__import__('pkg.mod') |
| 143 |
module = getattr(pkg, 'mod') |
| 144 |
self.assertEqual(module.__package__, 'pkg') |
| 145 |
|
| 146 |
|
| 147 |
class Setting__package__PEP451(Setting__package__, unittest.TestCase): |
| 148 |
mock_modules = util.mock_spec |
| 149 |
|
| 150 |
|
| 151 |
if __name__ == '__main__': |
| 152 |
unittest.main() |