Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
test.test_runpy.RunModuleTest Class Reference

List of all members.

Public Member Functions

def expect_import_error
def test_invalid_names
def test_library_module
def test_run_module
def test_run_package
def test_explicit_relative_import
def test_main_relative_import

Private Member Functions

def _add_pkg_dir
def _make_pkg
def _del_pkg
def _check_module
def _check_package
def _add_relative_modules
def _check_relative_imports

Detailed Description

Unit tests for runpy.run_module

Definition at line 86 of file test_runpy.py.


Member Function Documentation

def test.test_runpy.RunModuleTest._add_pkg_dir (   self,
  pkg_dir 
) [private]

Definition at line 112 of file test_runpy.py.

00112 
00113     def _add_pkg_dir(self, pkg_dir):
00114         os.mkdir(pkg_dir)
00115         pkg_fname = os.path.join(pkg_dir, "__init__.py")
00116         pkg_file = open(pkg_fname, "w")
00117         pkg_file.close()
00118         return pkg_fname

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._add_relative_modules (   self,
  base_dir,
  source,
  depth 
) [private]

Definition at line 211 of file test_runpy.py.

00211 
00212     def _add_relative_modules(self, base_dir, source, depth):
00213         if depth <= 1:
00214             raise ValueError("Relative module test needs depth > 1")
00215         pkg_name = "__runpy_pkg__"
00216         module_dir = base_dir
00217         for i in range(depth):
00218             parent_dir = module_dir
00219             module_dir = os.path.join(module_dir, pkg_name)
00220         # Add sibling module
00221         sibling_fname = os.path.join(module_dir, "sibling.py")
00222         sibling_file = open(sibling_fname, "w")
00223         sibling_file.close()
00224         if verbose: print("  Added sibling module:", sibling_fname)
00225         # Add nephew module
00226         uncle_dir = os.path.join(parent_dir, "uncle")
00227         self._add_pkg_dir(uncle_dir)
00228         if verbose: print("  Added uncle package:", uncle_dir)
00229         cousin_dir = os.path.join(uncle_dir, "cousin")
00230         self._add_pkg_dir(cousin_dir)
00231         if verbose: print("  Added cousin package:", cousin_dir)
00232         nephew_fname = os.path.join(cousin_dir, "nephew.py")
00233         nephew_file = open(nephew_fname, "w")
00234         nephew_file.close()
00235         if verbose: print("  Added nephew module:", nephew_fname)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._check_module (   self,
  depth 
) [private]

Definition at line 164 of file test_runpy.py.

00164 
00165     def _check_module(self, depth):
00166         pkg_dir, mod_fname, mod_name = (
00167                self._make_pkg("x=1\n", depth))
00168         forget(mod_name)
00169         try:
00170             if verbose: print("Running from source:", mod_name)
00171             d1 = run_module(mod_name) # Read from source
00172             self.assertIn("x", d1)
00173             self.assertEqual(d1["x"], 1)
00174             del d1 # Ensure __loader__ entry doesn't keep file open
00175             __import__(mod_name)
00176             os.remove(mod_fname)
00177             make_legacy_pyc(mod_fname)
00178             unload(mod_name)  # In case loader caches paths
00179             if verbose: print("Running from compiled:", mod_name)
00180             d2 = run_module(mod_name) # Read from bytecode
00181             self.assertIn("x", d2)
00182             self.assertEqual(d2["x"], 1)
00183             del d2 # Ensure __loader__ entry doesn't keep file open
00184         finally:
00185             self._del_pkg(pkg_dir, depth, mod_name)
00186         if verbose: print("Module executed successfully")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._check_package (   self,
  depth 
) [private]

Definition at line 187 of file test_runpy.py.

00187 
00188     def _check_package(self, depth):
00189         pkg_dir, mod_fname, mod_name = (
00190                self._make_pkg("x=1\n", depth, "__main__"))
00191         pkg_name, _, _ = mod_name.rpartition(".")
00192         forget(mod_name)
00193         try:
00194             if verbose: print("Running from source:", pkg_name)
00195             d1 = run_module(pkg_name) # Read from source
00196             self.assertIn("x", d1)
00197             self.assertTrue(d1["x"] == 1)
00198             del d1 # Ensure __loader__ entry doesn't keep file open
00199             __import__(mod_name)
00200             os.remove(mod_fname)
00201             make_legacy_pyc(mod_fname)
00202             unload(mod_name)  # In case loader caches paths
00203             if verbose: print("Running from compiled:", pkg_name)
00204             d2 = run_module(pkg_name) # Read from bytecode
00205             self.assertIn("x", d2)
00206             self.assertTrue(d2["x"] == 1)
00207             del d2 # Ensure __loader__ entry doesn't keep file open
00208         finally:
00209             self._del_pkg(pkg_dir, depth, pkg_name)
00210         if verbose: print("Package executed successfully")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._check_relative_imports (   self,
  depth,
  run_name = None 
) [private]

Definition at line 236 of file test_runpy.py.

00236 
00237     def _check_relative_imports(self, depth, run_name=None):
00238         contents = r"""\
00239 from __future__ import absolute_import
00240 from . import sibling
00241 from ..uncle.cousin import nephew
00242 """
00243         pkg_dir, mod_fname, mod_name = (
00244                self._make_pkg(contents, depth))
00245         try:
00246             self._add_relative_modules(pkg_dir, contents, depth)
00247             pkg_name = mod_name.rpartition('.')[0]
00248             if verbose: print("Running from source:", mod_name)
00249             d1 = run_module(mod_name, run_name=run_name) # Read from source
00250             self.assertIn("__package__", d1)
00251             self.assertTrue(d1["__package__"] == pkg_name)
00252             self.assertIn("sibling", d1)
00253             self.assertIn("nephew", d1)
00254             del d1 # Ensure __loader__ entry doesn't keep file open
00255             __import__(mod_name)
00256             os.remove(mod_fname)
00257             make_legacy_pyc(mod_fname)
00258             unload(mod_name)  # In case the loader caches paths
00259             if verbose: print("Running from compiled:", mod_name)
00260             d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
00261             self.assertIn("__package__", d2)
00262             self.assertTrue(d2["__package__"] == pkg_name)
00263             self.assertIn("sibling", d2)
00264             self.assertIn("nephew", d2)
00265             del d2 # Ensure __loader__ entry doesn't keep file open
00266         finally:
00267             self._del_pkg(pkg_dir, depth, mod_name)
00268         if verbose: print("Module executed successfully")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._del_pkg (   self,
  top,
  depth,
  mod_name 
) [private]

Definition at line 139 of file test_runpy.py.

00139 
00140     def _del_pkg(self, top, depth, mod_name):
00141         for entry in list(sys.modules):
00142             if entry.startswith("__runpy_pkg__"):
00143                 del sys.modules[entry]
00144         if verbose: print("  Removed sys.modules entries")
00145         del sys.path[0]
00146         if verbose: print("  Removed sys.path entry")
00147         for root, dirs, files in os.walk(top, topdown=False):
00148             for name in files:
00149                 try:
00150                     os.remove(os.path.join(root, name))
00151                 except OSError as ex:
00152                     if verbose: print(ex) # Persist with cleaning up
00153             for name in dirs:
00154                 fullname = os.path.join(root, name)
00155                 try:
00156                     os.rmdir(fullname)
00157                 except OSError as ex:
00158                     if verbose: print(ex) # Persist with cleaning up
00159         try:
00160             os.rmdir(top)
00161             if verbose: print("  Removed package tree")
00162         except OSError as ex:
00163             if verbose: print(ex) # Persist with cleaning up

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest._make_pkg (   self,
  source,
  depth,
  mod_base = "runpy_test" 
) [private]

Definition at line 119 of file test_runpy.py.

00119 
00120     def _make_pkg(self, source, depth, mod_base="runpy_test"):
00121         pkg_name = "__runpy_pkg__"
00122         test_fname = mod_base+os.extsep+"py"
00123         pkg_dir = sub_dir = tempfile.mkdtemp()
00124         if verbose: print("  Package tree in:", sub_dir)
00125         sys.path.insert(0, pkg_dir)
00126         if verbose: print("  Updated sys.path:", sys.path[0])
00127         for i in range(depth):
00128             sub_dir = os.path.join(sub_dir, pkg_name)
00129             pkg_fname = self._add_pkg_dir(sub_dir)
00130             if verbose: print("  Next level in:", sub_dir)
00131             if verbose: print("  Created:", pkg_fname)
00132         mod_fname = os.path.join(sub_dir, test_fname)
00133         mod_file = open(mod_fname, "w")
00134         mod_file.write(source)
00135         mod_file.close()
00136         if verbose: print("  Created:", mod_fname)
00137         mod_name = (pkg_name+".")*depth + mod_base
00138         return pkg_dir, mod_fname, mod_name

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_runpy.RunModuleTest.expect_import_error (   self,
  mod_name 
)

Definition at line 89 of file test_runpy.py.

00089 
00090     def expect_import_error(self, mod_name):
00091         try:
00092             run_module(mod_name)
00093         except ImportError:
00094             pass
00095         else:
00096             self.fail("Expected import error for " + mod_name)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 279 of file test_runpy.py.

00279 
00280     def test_explicit_relative_import(self):
00281         for depth in range(2, 5):
00282             if verbose: print("Testing relative imports at depth:", depth)
00283             self._check_relative_imports(depth)

Here is the call graph for this function:

Definition at line 97 of file test_runpy.py.

00097 
00098     def test_invalid_names(self):
00099         # Builtin module
00100         self.expect_import_error("sys")
00101         # Non-existent modules
00102         self.expect_import_error("sys.imp.eric")
00103         self.expect_import_error("os.path.half")
00104         self.expect_import_error("a.bee")
00105         self.expect_import_error(".howard")
00106         self.expect_import_error("..eaten")
00107         # Package without __main__.py
00108         self.expect_import_error("multiprocessing")

Here is the call graph for this function:

Definition at line 109 of file test_runpy.py.

00109 
00110     def test_library_module(self):
00111         run_module("runpy")

Here is the call graph for this function:

Definition at line 284 of file test_runpy.py.

00284 
00285     def test_main_relative_import(self):
00286         for depth in range(2, 5):
00287             if verbose: print("Testing main relative imports at depth:", depth)
00288             self._check_relative_imports(depth, "__main__")
00289 

Here is the call graph for this function:

Definition at line 269 of file test_runpy.py.

00269 
00270     def test_run_module(self):
00271         for depth in range(4):
00272             if verbose: print("Testing package depth:", depth)
00273             self._check_module(depth)

Here is the call graph for this function:

Definition at line 274 of file test_runpy.py.

00274 
00275     def test_run_package(self):
00276         for depth in range(1, 4):
00277             if verbose: print("Testing package depth:", depth)
00278             self._check_package(depth)

Here is the call graph for this function:


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