Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.import_.test___package__.Using__package__ Class Reference

List of all members.

Public Member Functions

def test_using___package__
def test_using___name__
def test_None_as___package__
def test_bad__package__
def test_bunk__package__

Detailed Description

Use of __package__ supercedes the use of __name__/__path__ to calculate
what package a module belongs to. The basic algorithm is [__package__]::

  def resolve_name(name, package, level):
      level -= 1
      base = package.rsplit('.', level)[0]
      return '{0}.{1}'.format(base, name)

But since there is no guarantee that __package__ has been set (or not been
set to None [None]), there has to be a way to calculate the attribute's value
[__name__]::

  def calc_package(caller_name, has___path__):
      if has__path__:
          return caller_name
      else:
          return caller_name.rsplit('.', 1)[0]

Then the normal algorithm for relative name imports can proceed as if
__package__ had been set.

Definition at line 12 of file test___package__.py.


Member Function Documentation

Definition at line 63 of file test___package__.py.

00063 
00064     def test_bad__package__(self):
00065         globals = {'__package__': '<not real>'}
00066         with self.assertRaises(SystemError):
00067             import_util.import_('', globals, {}, ['relimport'], 1)

Here is the call graph for this function:

Definition at line 68 of file test___package__.py.

00068 
00069     def test_bunk__package__(self):
00070         globals = {'__package__': 42}
00071         with self.assertRaises(ValueError):
00072             import_util.import_('', globals, {}, ['relimport'], 1)
00073 
00074 
@import_util.importlib_only

Here is the call graph for this function:

Definition at line 59 of file test___package__.py.

00059 
00060     def test_None_as___package__(self):
00061         # [None]
00062         self.test_using___name__(package_as_None=True)

Here is the call graph for this function:

Definition at line 47 of file test___package__.py.

00047 
00048     def test_using___name__(self, package_as_None=False):
00049         # [__name__]
00050         globals_ = {'__name__': 'pkg.fake', '__path__': []}
00051         if package_as_None:
00052             globals_['__package__'] = None
00053         with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
00054             with util.import_state(meta_path=[importer]):
00055                 import_util.import_('pkg.fake')
00056                 module = import_util.import_('', globals= globals_,
00057                                                 fromlist=['attr'], level=2)
00058             self.assertEqual(module.__name__, 'pkg')

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 37 of file test___package__.py.

00037 
00038     def test_using___package__(self):
00039         # [__package__]
00040         with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
00041             with util.import_state(meta_path=[importer]):
00042                 import_util.import_('pkg.fake')
00043                 module = import_util.import_('',
00044                                             globals={'__package__': 'pkg.fake'},
00045                                             fromlist=['attr'], level=2)
00046         self.assertEqual(module.__name__, 'pkg')

Here is the call graph for this function:


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