Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_pkgimport.TestImport Class Reference

List of all members.

Public Member Functions

def __init__
def remove_modules
def setUp
def tearDown
def rewrite_file
def test_package_import__semantics

Public Attributes

 package_name
 module_name
 test_dir
 package_dir
 module_path

Detailed Description

Definition at line 12 of file test_pkgimport.py.


Constructor & Destructor Documentation

def test.test_pkgimport.TestImport.__init__ (   self,
  args,
  kw 
)

Definition at line 14 of file test_pkgimport.py.

00014 
00015     def __init__(self, *args, **kw):
00016         self.package_name = 'PACKAGE_'
00017         while self.package_name in sys.modules:
00018             self.package_name += random.choose(string.ascii_letters)
00019         self.module_name = self.package_name + '.foo'
00020         unittest.TestCase.__init__(self, *args, **kw)

Here is the caller graph for this function:


Member Function Documentation

Definition at line 21 of file test_pkgimport.py.

00021 
00022     def remove_modules(self):
00023         for module_name in (self.package_name, self.module_name):
00024             if module_name in sys.modules:
00025                 del sys.modules[module_name]

Here is the caller graph for this function:

def test.test_pkgimport.TestImport.rewrite_file (   self,
  contents 
)

Definition at line 41 of file test_pkgimport.py.

00041 
00042     def rewrite_file(self, contents):
00043         compiled_path = cache_from_source(self.module_path)
00044         if os.path.exists(compiled_path):
00045             os.remove(compiled_path)
00046         with open(self.module_path, 'w') as f:
00047             f.write(contents)

Here is the caller graph for this function:

Definition at line 26 of file test_pkgimport.py.

00026 
00027     def setUp(self):
00028         self.test_dir = tempfile.mkdtemp()
00029         sys.path.append(self.test_dir)
00030         self.package_dir = os.path.join(self.test_dir,
00031                                         self.package_name)
00032         os.mkdir(self.package_dir)
00033         open(os.path.join(self.package_dir, '__init__.py'), 'w').close()
00034         self.module_path = os.path.join(self.package_dir, 'foo.py')

Here is the caller graph for this function:

Definition at line 35 of file test_pkgimport.py.

00035 
00036     def tearDown(self):
00037         shutil.rmtree(self.test_dir)
00038         self.assertNotEqual(sys.path.count(self.test_dir), 0)
00039         sys.path.remove(self.test_dir)
00040         self.remove_modules()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 48 of file test_pkgimport.py.

00048 
00049     def test_package_import__semantics(self):
00050 
00051         # Generate a couple of broken modules to try importing.
00052 
00053         # ...try loading the module when there's a SyntaxError
00054         self.rewrite_file('for')
00055         try: __import__(self.module_name)
00056         except SyntaxError: pass
00057         else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
00058         self.assertNotIn(self.module_name, sys.modules)
00059         self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))
00060 
00061         # ...make up a variable name that isn't bound in __builtins__
00062         var = 'a'
00063         while var in dir(__builtins__):
00064             var += random.choose(string.ascii_letters)
00065 
00066         # ...make a module that just contains that
00067         self.rewrite_file(var)
00068 
00069         try: __import__(self.module_name)
00070         except NameError: pass
00071         else: raise RuntimeError('Failed to induce NameError.')
00072 
00073         # ...now  change  the module  so  that  the NameError  doesn't
00074         # happen
00075         self.rewrite_file('%s = 1' % var)
00076         module = __import__(self.module_name).foo
00077         self.assertEqual(getattr(module, var), 1)
00078 

Here is the call graph for this function:


Member Data Documentation

Definition at line 18 of file test_pkgimport.py.

Definition at line 33 of file test_pkgimport.py.

Definition at line 29 of file test_pkgimport.py.

Definition at line 15 of file test_pkgimport.py.

Definition at line 27 of file test_pkgimport.py.


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