PyXR

c:\python24\lib \ test \ test_import.py



0001 from test.test_support import TESTFN, TestFailed
0002 
0003 import os
0004 import random
0005 import sys
0006 import py_compile
0007 
0008 # Brief digression to test that import is case-sensitive:  if we got this
0009 # far, we know for sure that "random" exists.
0010 try:
0011     import RAnDoM
0012 except ImportError:
0013     pass
0014 else:
0015     raise TestFailed("import of RAnDoM should have failed (case mismatch)")
0016 
0017 # Another brief digression to test the accuracy of manifest float constants.
0018 import double_const  # don't blink -- that *was* the test
0019 
0020 def remove_files(name):
0021     for f in (name + os.extsep + "py",
0022               name + os.extsep + "pyc",
0023               name + os.extsep + "pyo",
0024               name + os.extsep + "pyw",
0025               name + "$py.class"):
0026         if os.path.exists(f):
0027             os.remove(f)
0028 
0029 def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
0030     source = TESTFN + ext
0031     pyo = TESTFN + os.extsep + "pyo"
0032     if sys.platform.startswith('java'):
0033         pyc = TESTFN + "$py.class"
0034     else:
0035         pyc = TESTFN + os.extsep + "pyc"
0036 
0037     f = open(source, "w")
0038     print >> f, "# This tests Python's ability to import a", ext, "file."
0039     a = random.randrange(1000)
0040     b = random.randrange(1000)
0041     print >> f, "a =", a
0042     print >> f, "b =", b
0043     f.close()
0044 
0045     try:
0046         try:
0047             mod = __import__(TESTFN)
0048         except ImportError, err:
0049             raise ValueError("import from %s failed: %s" % (ext, err))
0050 
0051         if mod.a != a or mod.b != b:
0052             print a, "!=", mod.a
0053             print b, "!=", mod.b
0054             raise ValueError("module loaded (%s) but contents invalid" % mod)
0055     finally:
0056         os.unlink(source)
0057 
0058     try:
0059         try:
0060             reload(mod)
0061         except ImportError, err:
0062             raise ValueError("import from .pyc/.pyo failed: %s" % err)
0063     finally:
0064         try:
0065             os.unlink(pyc)
0066         except os.error:
0067             pass
0068         try:
0069             os.unlink(pyo)
0070         except os.error:
0071             pass
0072         del sys.modules[TESTFN]
0073 
0074 sys.path.insert(0, os.curdir)
0075 try:
0076     test_with_extension(os.extsep + "py")
0077     if sys.platform.startswith("win"):
0078         for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
0079             test_with_extension(ext)
0080 finally:
0081     del sys.path[0]
0082 
0083 # Verify that the imp module can correctly load and find .py files
0084 import imp
0085 x = imp.find_module("os")
0086 os = imp.load_module("os", *x)
0087 
0088 def test_module_with_large_stack(module):
0089     # create module w/list of 65000 elements to test bug #561858
0090     filename = module + os.extsep + 'py'
0091 
0092     # create a file with a list of 65000 elements
0093     f = open(filename, 'w+')
0094     f.write('d = [\n')
0095     for i in range(65000):
0096         f.write('"",\n')
0097     f.write(']')
0098     f.close()
0099 
0100     # compile & remove .py file, we only need .pyc (or .pyo)
0101     f = open(filename, 'r')
0102     py_compile.compile(filename)
0103     f.close()
0104     os.unlink(filename)
0105 
0106     # need to be able to load from current dir
0107     sys.path.append('')
0108 
0109     # this used to crash
0110     exec 'import ' + module
0111 
0112     # cleanup
0113     del sys.path[-1]
0114     for ext in 'pyc', 'pyo':
0115         fname = module + os.extsep + ext
0116         if os.path.exists(fname):
0117             os.unlink(fname)
0118 
0119 test_module_with_large_stack('longlist')
0120 
0121 def test_failing_import_sticks():
0122     source = TESTFN + os.extsep + "py"
0123     f = open(source, "w")
0124     print >> f, "a = 1/0"
0125     f.close()
0126 
0127     # New in 2.4, we shouldn't be able to import that no matter how often
0128     # we try.
0129     sys.path.insert(0, os.curdir)
0130     try:
0131         for i in 1, 2, 3:
0132             try:
0133                 mod = __import__(TESTFN)
0134             except ZeroDivisionError:
0135                 if TESTFN in sys.modules:
0136                     raise TestFailed("damaged module in sys.modules", i)
0137             else:
0138                 raise TestFailed("was able to import a damaged module", i)
0139     finally:
0140         sys.path.pop(0)
0141         remove_files(TESTFN)
0142 
0143 test_failing_import_sticks()
0144 
0145 def test_failing_reload():
0146     # A failing reload should leave the module object in sys.modules.
0147     source = TESTFN + os.extsep + "py"
0148     f = open(source, "w")
0149     print >> f, "a = 1"
0150     print >> f, "b = 2"
0151     f.close()
0152 
0153     sys.path.insert(0, os.curdir)
0154     try:
0155         mod = __import__(TESTFN)
0156         if TESTFN not in sys.modules:
0157             raise TestFailed("expected module in sys.modules")
0158         if mod.a != 1 or mod.b != 2:
0159             raise TestFailed("module has wrong attribute values")
0160 
0161         # On WinXP, just replacing the .py file wasn't enough to
0162         # convince reload() to reparse it.  Maybe the timestamp didn't
0163         # move enough.  We force it to get reparsed by removing the
0164         # compiled file too.
0165         remove_files(TESTFN)
0166 
0167         # Now damage the module.
0168         f = open(source, "w")
0169         print >> f, "a = 10"
0170         print >> f, "b = 20//0"
0171         f.close()
0172         try:
0173             reload(mod)
0174         except ZeroDivisionError:
0175             pass
0176         else:
0177             raise TestFailed("was able to reload a damaged module")
0178 
0179         # But we still expect the module to be in sys.modules.
0180         mod = sys.modules.get(TESTFN)
0181         if mod is None:
0182             raise TestFailed("expected module to still be in sys.modules")
0183         # We should have replaced a w/ 10, but the old b value should
0184         # stick.
0185         if mod.a != 10 or mod.b != 2:
0186             raise TestFailed("module has wrong attribute values")
0187 
0188     finally:
0189         sys.path.pop(0)
0190         remove_files(TESTFN)
0191         if TESTFN in sys.modules:
0192             del sys.modules[TESTFN]
0193 
0194 test_failing_reload()
0195 

Generated by PyXR 0.9.4
SourceForge.net Logo