Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_import.ImportTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_case_sensitivity
def test_double_const
def test_import
def test_execute_bit_not_copied
def test_imp_module
def test_module_with_large_stack
def test_failing_import_sticks
def test_import_name_binding
def test_failing_reload
def test_file_to_source
def test_import_name_binding
def test_import_initless_directory_warning
def test_import_by_filename
def test_import_in_del_does_not_crash

Static Public Attributes

 setUp = tearDown

Detailed Description

Definition at line 31 of file test_import.py.


Member Function Documentation

Definition at line 33 of file test_import.py.

00033 
00034     def setUp(self):
00035         remove_files(TESTFN)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 36 of file test_import.py.

00036 
00037     def tearDown(self):
00038         unload(TESTFN)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 41 of file test_import.py.

00041 
00042     def test_case_sensitivity(self):
00043         # Brief digression to test that import is case-sensitive:  if we got
00044         # this far, we know for sure that "random" exists.
00045         with self.assertRaises(ImportError):
00046             import RAnDoM

Here is the call graph for this function:

Definition at line 47 of file test_import.py.

00047 
00048     def test_double_const(self):
00049         # Another brief digression to test the accuracy of manifest float
00050         # constants.
00051         from test import double_const  # don't blink -- that *was* the test

Definition at line 99 of file test_import.py.

00099 
00100     def test_execute_bit_not_copied(self):
00101         # Issue 6070: under posix .pyc files got their execute bit set if
00102         # the .py file had the execute bit set, but they aren't executable.
00103         with temp_umask(0o022):
00104             sys.path.insert(0, os.curdir)
00105             try:
00106                 fname = TESTFN + os.extsep + "py"
00107                 open(fname, 'w').close()
00108                 os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
00109                                  stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
00110                 __import__(TESTFN)
00111                 fn = imp.cache_from_source(fname)
00112                 if not os.path.exists(fn):
00113                     self.fail("__import__ did not result in creation of "
00114                               "either a .pyc or .pyo file")
00115                     s = os.stat(fn)
00116                     self.assertEqual(
00117                         stat.S_IMODE(s.st_mode),
00118                         stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
00119             finally:
00120                 del sys.path[0]
00121                 remove_files(TESTFN)
00122                 unload(TESTFN)

Here is the call graph for this function:

Definition at line 173 of file test_import.py.

00173 
00174     def test_failing_import_sticks(self):
00175         source = TESTFN + ".py"
00176         with open(source, "w") as f:
00177             print("a = 1/0", file=f)
00178 
00179         # New in 2.4, we shouldn't be able to import that no matter how often
00180         # we try.
00181         sys.path.insert(0, os.curdir)
00182         if TESTFN in sys.modules:
00183             del sys.modules[TESTFN]
00184         try:
00185             for i in [1, 2, 3]:
00186                 self.assertRaises(ZeroDivisionError, __import__, TESTFN)
00187                 self.assertNotIn(TESTFN, sys.modules,
00188                                  "damaged module in sys.modules on %i try" % i)
00189         finally:
00190             del sys.path[0]
00191             remove_files(TESTFN)

Here is the call graph for this function:

Definition at line 203 of file test_import.py.

00203 
00204     def test_failing_reload(self):
00205         # A failing reload should leave the module object in sys.modules.
00206         source = TESTFN + os.extsep + "py"
00207         with open(source, "w") as f:
00208             f.write("a = 1\nb=2\n")
00209 
00210         sys.path.insert(0, os.curdir)
00211         try:
00212             mod = __import__(TESTFN)
00213             self.assertIn(TESTFN, sys.modules)
00214             self.assertEqual(mod.a, 1, "module has wrong attribute values")
00215             self.assertEqual(mod.b, 2, "module has wrong attribute values")
00216 
00217             # On WinXP, just replacing the .py file wasn't enough to
00218             # convince reload() to reparse it.  Maybe the timestamp didn't
00219             # move enough.  We force it to get reparsed by removing the
00220             # compiled file too.
00221             remove_files(TESTFN)
00222 
00223             # Now damage the module.
00224             with open(source, "w") as f:
00225                 f.write("a = 10\nb=20//0\n")
00226 
00227             self.assertRaises(ZeroDivisionError, imp.reload, mod)
00228             # But we still expect the module to be in sys.modules.
00229             mod = sys.modules.get(TESTFN)
00230             self.assertIsNot(mod, None, "expected module to be in sys.modules")
00231 
00232             # We should have replaced a w/ 10, but the old b value should
00233             # stick.
00234             self.assertEqual(mod.a, 10, "module has wrong attribute values")
00235             self.assertEqual(mod.b, 2, "module has wrong attribute values")
00236 
00237         finally:
00238             del sys.path[0]
00239             remove_files(TESTFN)
00240             unload(TESTFN)

Here is the call graph for this function:

Definition at line 241 of file test_import.py.

00241 
00242     def test_file_to_source(self):
00243         # check if __file__ points to the source file where available
00244         source = TESTFN + ".py"
00245         with open(source, "w") as f:
00246             f.write("test = None\n")
00247 
00248         sys.path.insert(0, os.curdir)
00249         try:
00250             mod = __import__(TESTFN)
00251             self.assertTrue(mod.__file__.endswith('.py'))
00252             os.remove(source)
00253             del sys.modules[TESTFN]
00254             make_legacy_pyc(source)
00255             mod = __import__(TESTFN)
00256             base, ext = os.path.splitext(mod.__file__)
00257             self.assertIn(ext, ('.pyc', '.pyo'))
00258         finally:
00259             del sys.path[0]
00260             remove_files(TESTFN)
00261             if TESTFN in sys.modules:
00262                 del sys.modules[TESTFN]

Here is the call graph for this function:

Definition at line 123 of file test_import.py.

00123 
00124     def test_imp_module(self):
00125         # Verify that the imp module can correctly load and find .py files
00126         # XXX (ncoghlan): It would be nice to use support.CleanImport
00127         # here, but that breaks because the os module registers some
00128         # handlers in copy_reg on import. Since CleanImport doesn't
00129         # revert that registration, the module is left in a broken
00130         # state after reversion. Reinitialising the module contents
00131         # and just reverting os.environ to its previous state is an OK
00132         # workaround
00133         orig_path = os.path
00134         orig_getenv = os.getenv
00135         with EnvironmentVarGuard():
00136             x = imp.find_module("os")
00137             self.addCleanup(x[0].close)
00138             new_os = imp.load_module("os", *x)
00139             self.assertIs(os, new_os)
00140             self.assertIs(orig_path, new_os.path)
00141             self.assertIsNot(orig_getenv, new_os.getenv)

Here is the call graph for this function:

Definition at line 52 of file test_import.py.

00052 
00053     def test_import(self):
00054         def test_with_extension(ext):
00055             # The extension is normally ".py", perhaps ".pyw".
00056             source = TESTFN + ext
00057             pyo = TESTFN + ".pyo"
00058             if is_jython:
00059                 pyc = TESTFN + "$py.class"
00060             else:
00061                 pyc = TESTFN + ".pyc"
00062 
00063             with open(source, "w") as f:
00064                 print("# This tests Python's ability to import a",
00065                       ext, "file.", file=f)
00066                 a = random.randrange(1000)
00067                 b = random.randrange(1000)
00068                 print("a =", a, file=f)
00069                 print("b =", b, file=f)
00070 
00071             if TESTFN in sys.modules:
00072                 del sys.modules[TESTFN]
00073             try:
00074                 try:
00075                     mod = __import__(TESTFN)
00076                 except ImportError as err:
00077                     self.fail("import from %s failed: %s" % (ext, err))
00078 
00079                 self.assertEqual(mod.a, a,
00080                     "module loaded (%s) but contents invalid" % mod)
00081                 self.assertEqual(mod.b, b,
00082                     "module loaded (%s) but contents invalid" % mod)
00083             finally:
00084                 forget(TESTFN)
00085                 unlink(source)
00086                 unlink(pyc)
00087                 unlink(pyo)
00088 
00089         sys.path.insert(0, os.curdir)
00090         try:
00091             test_with_extension(".py")
00092             if sys.platform.startswith("win"):
00093                 for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
00094                     test_with_extension(ext)
00095         finally:
00096             del sys.path[0]

Here is the call graph for this function:

Definition at line 280 of file test_import.py.

00280 
00281     def test_import_by_filename(self):
00282         path = os.path.abspath(TESTFN)
00283         encoding = sys.getfilesystemencoding()
00284         try:
00285             path.encode(encoding)
00286         except UnicodeEncodeError:
00287             self.skipTest('path is not encodable to {}'.format(encoding))
00288         with self.assertRaises(ImportError) as c:
00289             __import__(path)
00290         self.assertEqual("Import by filename is not supported.",
00291                          c.exception.args[0])

Here is the call graph for this function:

Definition at line 292 of file test_import.py.

00292 
00293     def test_import_in_del_does_not_crash(self):
00294         # Issue 4236
00295         testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
00296             import sys
00297             class C:
00298                def __del__(self):
00299                   import imp
00300             sys.argv.insert(0, C())
00301             """))
00302         script_helper.assert_python_ok(testfn)
00303 

Here is the call graph for this function:

Definition at line 274 of file test_import.py.

00274 
00275     def test_import_initless_directory_warning(self):
00276         with check_warnings(('', ImportWarning)):
00277             # Just a random non-package directory we always expect to be
00278             # somewhere in sys.path...
00279             self.assertRaises(ImportError, __import__, "site-packages")

Here is the call graph for this function:

Definition at line 192 of file test_import.py.

00192 
00193     def test_import_name_binding(self):
00194         # import x.y.z binds x in the current namespace
00195         import test as x
00196         import test.support
00197         self.assertTrue(x is test, x.__name__)
00198         self.assertTrue(hasattr(test.support, "__file__"))
00199 
00200         # import x.y.z as w binds z as w
00201         import test.support as y
00202         self.assertTrue(y is test.support, y.__name__)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 263 of file test_import.py.

00263 
00264     def test_import_name_binding(self):
00265         # import x.y.z binds x in the current namespace.
00266         import test as x
00267         import test.support
00268         self.assertIs(x, test, x.__name__)
00269         self.assertTrue(hasattr(test.support, "__file__"))
00270 
00271         # import x.y.z as w binds z as w.
00272         import test.support as y
00273         self.assertIs(y, test.support, y.__name__)

Here is the call graph for this function:

def test.test_import.ImportTests.test_module_with_large_stack (   self,
  module = 'longlist' 
)

Definition at line 142 of file test_import.py.

00142 
00143     def test_module_with_large_stack(self, module='longlist'):
00144         # Regression test for http://bugs.python.org/issue561858.
00145         filename = module + '.py'
00146 
00147         # Create a file with a list of 65000 elements.
00148         with open(filename, 'w') as f:
00149             f.write('d = [\n')
00150             for i in range(65000):
00151                 f.write('"",\n')
00152             f.write(']')
00153 
00154         try:
00155             # Compile & remove .py file; we only need .pyc (or .pyo).
00156             # Bytecode must be relocated from the PEP 3147 bytecode-only location.
00157             py_compile.compile(filename)
00158         finally:
00159             unlink(filename)
00160 
00161         # Need to be able to load from current dir.
00162         sys.path.append('')
00163 
00164         try:
00165             make_legacy_pyc(filename)
00166             # This used to crash.
00167             exec('import ' + module)
00168         finally:
00169             # Cleanup.
00170             del sys.path[-1]
00171             unlink(filename + 'c')
00172             unlink(filename + 'o')

Here is the call graph for this function:


Member Data Documentation

Definition at line 39 of file test_import.py.


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