home *** CD-ROM | disk | FTP | other *** search
/ Enter 2003: The Beautiful Scenery / enter-parhaat-2003.iso / files / Python-2.2.1.exe / TEST_IMPORT.PY < prev    next >
Encoding:
Python Source  |  2001-10-24  |  2.0 KB  |  72 lines

  1. from test_support import TESTFN, TestFailed
  2.  
  3. import os
  4. import random
  5. import sys
  6.  
  7. # Brief digression to test that import is case-sensitive:  if we got this
  8. # far, we know for sure that "random" exists.
  9. try:
  10.     import RAnDoM
  11. except ImportError:
  12.     pass
  13. else:
  14.     raise TestFailed("import of RAnDoM should have failed (case mismatch)")
  15.  
  16. # Another brief digression to test the accuracy of manifest float constants.
  17. import double_const  # don't blink -- that *was* the test
  18.  
  19. def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
  20.     source = TESTFN + ext
  21.     pyo = TESTFN + os.extsep + "pyo"
  22.     if sys.platform.startswith('java'):
  23.         pyc = TESTFN + "$py.class"
  24.     else:
  25.         pyc = TESTFN + os.extsep + "pyc"
  26.  
  27.     f = open(source, "w")
  28.     print >> f, "# This tests Python's ability to import a", ext, "file."
  29.     a = random.randrange(1000)
  30.     b = random.randrange(1000)
  31.     print >> f, "a =", a
  32.     print >> f, "b =", b
  33.     f.close()
  34.  
  35.     try:
  36.         try:
  37.             mod = __import__(TESTFN)
  38.         except ImportError, err:
  39.             raise ValueError("import from %s failed: %s" % (ext, err))
  40.  
  41.         if mod.a != a or mod.b != b:
  42.             print a, "!=", mod.a
  43.             print b, "!=", mod.b
  44.             raise ValueError("module loaded (%s) but contents invalid" % mod)
  45.     finally:
  46.         os.unlink(source)
  47.  
  48.     try:
  49.         try:
  50.             reload(mod)
  51.         except ImportError, err:
  52.             raise ValueError("import from .pyc/.pyo failed: %s" % err)
  53.     finally:
  54.         try:
  55.             os.unlink(pyc)
  56.         except os.error:
  57.             pass
  58.         try:
  59.             os.unlink(pyo)
  60.         except os.error:
  61.             pass
  62.         del sys.modules[TESTFN]
  63.  
  64. sys.path.insert(0, os.curdir)
  65. try:
  66.     test_with_extension(os.extsep + "py")
  67.     if sys.platform.startswith("win"):
  68.         for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
  69.             test_with_extension(ext)
  70. finally:
  71.     del sys.path[0]
  72.