Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.import_.test_relative_imports.RelativeImports Class Reference

List of all members.

Public Member Functions

def relative_import_test
def test_module_from_module
def test_attr_from_module
def test_package_to_module
def test_module_to_package
def test_package_to_package
def test_deep_import
def test_too_high_from_package
def test_too_high_from_module
def test_empty_name_w_level_0
def test_import_from_different_package

Detailed Description

PEP 328 introduced relative imports. This allows for imports to occur
from within a package without having to specify the actual package name.

A simple example is to import another module within the same package
[module from module]::

  # From pkg.mod1 with pkg.mod2 being a module.
  from . import mod2

This also works for getting an attribute from a module that is specified
in a relative fashion [attr from module]::

  # From pkg.mod1.
  from .mod2 import attr

But this is in no way restricted to working between modules; it works
from [package to module],::

  # From pkg, importing pkg.module which is a module.
  from . import module

[module to package],::

  # Pull attr from pkg, called from pkg.module which is a module.
  from . import attr

and [package to package]::

  # From pkg.subpkg1 (both pkg.subpkg[1,2] are packages).
  from .. import subpkg2

The number of dots used is in no way restricted [deep import]::

  # Import pkg.attr from pkg.pkg1.pkg2.pkg3.pkg4.pkg5.
  from ...... import attr

To prevent someone from accessing code that is outside of a package, one
cannot reach the location containing the root package itself::

  # From pkg.__init__ [too high from package]
  from .. import top_level

  # From pkg.module [too high from module]
  from .. import top_level

 Relative imports are the only type of import that allow for an empty
 module name for an import [empty name].

Definition at line 7 of file test_relative_imports.py.


Member Function Documentation

def importlib.test.import_.test_relative_imports.RelativeImports.relative_import_test (   self,
  create,
  globals_,
  callback 
)
Abstract out boilerplace for setting up for an import test.

Definition at line 59 of file test_relative_imports.py.

00059 
00060     def relative_import_test(self, create, globals_, callback):
00061         """Abstract out boilerplace for setting up for an import test."""
00062         uncache_names = []
00063         for name in create:
00064             if not name.endswith('.__init__'):
00065                 uncache_names.append(name)
00066             else:
00067                 uncache_names.append(name[:-len('.__init__')])
00068         with util.mock_modules(*create) as importer:
00069             with util.import_state(meta_path=[importer]):
00070                 for global_ in globals_:
00071                     with util.uncache(*uncache_names):
00072                         callback(global_)
00073 

Here is the caller graph for this function:

Definition at line 86 of file test_relative_imports.py.

00086 
00087     def test_attr_from_module(self):
00088         # [attr from module]
00089         create = 'pkg.__init__', 'pkg.mod2'
00090         globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
00091         def callback(global_):
00092             import_util.import_('pkg')  # For __import__().
00093             module = import_util.import_('mod2', global_, fromlist=['attr'],
00094                                             level=1)
00095             self.assertEqual(module.__name__, 'pkg.mod2')
00096             self.assertEqual(module.attr, 'pkg.mod2')
00097         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 135 of file test_relative_imports.py.

00135 
00136     def test_deep_import(self):
00137         # [deep import]
00138         create = ['pkg.__init__']
00139         for count in range(1,6):
00140             create.append('{0}.pkg{1}.__init__'.format(
00141                             create[-1][:-len('.__init__')], count))
00142         globals_ = ({'__package__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5'},
00143                     {'__name__': 'pkg.pkg1.pkg2.pkg3.pkg4.pkg5',
00144                         '__path__': ['blah']})
00145         def callback(global_):
00146             import_util.import_(globals_[0]['__package__'])
00147             module = import_util.import_('', global_, fromlist=['attr'], level=6)
00148             self.assertEqual(module.__name__, 'pkg')
00149         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 173 of file test_relative_imports.py.

00173 
00174     def test_empty_name_w_level_0(self):
00175         # [empty name]
00176         with self.assertRaises(ValueError):
00177             import_util.import_('')

Here is the call graph for this function:

Definition at line 178 of file test_relative_imports.py.

00178 
00179     def test_import_from_different_package(self):
00180         # Test importing from a different package than the caller.
00181         # in pkg.subpkg1.mod
00182         # from ..subpkg2 import mod
00183         create = ['__runpy_pkg__.__init__',
00184                     '__runpy_pkg__.__runpy_pkg__.__init__',
00185                     '__runpy_pkg__.uncle.__init__',
00186                     '__runpy_pkg__.uncle.cousin.__init__',
00187                     '__runpy_pkg__.uncle.cousin.nephew']
00188         globals_ = {'__package__': '__runpy_pkg__.__runpy_pkg__'}
00189         def callback(global_):
00190             import_util.import_('__runpy_pkg__.__runpy_pkg__')
00191             module = import_util.import_('uncle.cousin', globals_, {},
00192                                     fromlist=['nephew'],
00193                                 level=2)
00194             self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin')
00195         self.relative_import_test(create, globals_, callback)
00196 
00197 

Here is the call graph for this function:

Definition at line 74 of file test_relative_imports.py.

00074 
00075     def test_module_from_module(self):
00076         # [module from module]
00077         create = 'pkg.__init__', 'pkg.mod2'
00078         globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.mod1'}
00079         def callback(global_):
00080             import_util.import_('pkg')  # For __import__().
00081             module = import_util.import_('', global_, fromlist=['mod2'], level=1)
00082             self.assertEqual(module.__name__, 'pkg')
00083             self.assertTrue(hasattr(module, 'mod2'))
00084             self.assertEqual(module.mod2.attr, 'pkg.mod2')
00085         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 112 of file test_relative_imports.py.

00112 
00113     def test_module_to_package(self):
00114         # [module to package]
00115         create = 'pkg.__init__', 'pkg.module'
00116         globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
00117         def callback(global_):
00118             import_util.import_('pkg')  # For __import__().
00119             module = import_util.import_('', global_, fromlist=['attr'], level=1)
00120             self.assertEqual(module.__name__, 'pkg')
00121         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 98 of file test_relative_imports.py.

00098 
00099     def test_package_to_module(self):
00100         # [package to module]
00101         create = 'pkg.__init__', 'pkg.module'
00102         globals_ = ({'__package__': 'pkg'},
00103                     {'__name__': 'pkg', '__path__': ['blah']})
00104         def callback(global_):
00105             import_util.import_('pkg')  # For __import__().
00106             module = import_util.import_('', global_, fromlist=['module'],
00107                              level=1)
00108             self.assertEqual(module.__name__, 'pkg')
00109             self.assertTrue(hasattr(module, 'module'))
00110             self.assertEqual(module.module.attr, 'pkg.module')
00111         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 122 of file test_relative_imports.py.

00122 
00123     def test_package_to_package(self):
00124         # [package to package]
00125         create = ('pkg.__init__', 'pkg.subpkg1.__init__',
00126                     'pkg.subpkg2.__init__')
00127         globals_ =  ({'__package__': 'pkg.subpkg1'},
00128                      {'__name__': 'pkg.subpkg1', '__path__': ['blah']})
00129         def callback(global_):
00130             module = import_util.import_('', global_, fromlist=['subpkg2'],
00131                                             level=2)
00132             self.assertEqual(module.__name__, 'pkg')
00133             self.assertTrue(hasattr(module, 'subpkg2'))
00134             self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')

Here is the call graph for this function:

Definition at line 162 of file test_relative_imports.py.

00162 
00163     def test_too_high_from_module(self):
00164         # [too high from module]
00165         create = ['top_level', 'pkg.__init__', 'pkg.module']
00166         globals_ = {'__package__': 'pkg'}, {'__name__': 'pkg.module'}
00167         def callback(global_):
00168             import_util.import_('pkg')
00169             with self.assertRaises(ValueError):
00170                 import_util.import_('', global_, fromlist=['top_level'],
00171                                     level=2)
00172         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:

Definition at line 150 of file test_relative_imports.py.

00150 
00151     def test_too_high_from_package(self):
00152         # [too high from package]
00153         create = ['top_level', 'pkg.__init__']
00154         globals_ = ({'__package__': 'pkg'},
00155                     {'__name__': 'pkg', '__path__': ['blah']})
00156         def callback(global_):
00157             import_util.import_('pkg')
00158             with self.assertRaises(ValueError):
00159                 import_util.import_('', global_, fromlist=['top_level'],
00160                                     level=2)
00161         self.relative_import_test(create, globals_, callback)

Here is the call graph for this function:


The documentation for this class was generated from the following file: