home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_pkg.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  5.7 KB  |  153 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import sys
  5. import os
  6. import string
  7. import tempfile
  8. import traceback
  9. rmdir
  10. del mkdir
  11. del rmdir
  12. TestFailed
  13.  
  14. def mkhier(root, descr):
  15.     mkdir(root)
  16.     for name, contents in descr:
  17.         comps = string.split(name)
  18.         fullname = root
  19.         for c in comps:
  20.             fullname = os.path.join(fullname, c)
  21.         
  22.         if contents is None:
  23.             mkdir(fullname)
  24.         elif verbose:
  25.             print 'write', fullname
  26.         
  27.         f = open(fullname, 'w')
  28.         f.write(contents)
  29.         if contents and contents[-1] != '\n':
  30.             f.write('\n')
  31.         
  32.         f.close()
  33.     
  34.  
  35.  
  36. def mkdir(x):
  37.     if verbose:
  38.         print 'mkdir', x
  39.     
  40.     os.mkdir(x)
  41.  
  42.  
  43. def cleanout(root):
  44.     names = os.listdir(root)
  45.     for name in names:
  46.         fullname = os.path.join(root, name)
  47.     
  48.     rmdir(root)
  49.  
  50.  
  51. def rmdir(x):
  52.     if verbose:
  53.         print 'rmdir', x
  54.     
  55.     os.rmdir(x)
  56.  
  57.  
  58. def runtest(hier, code):
  59.     root = tempfile.mktemp()
  60.     mkhier(root, hier)
  61.     savepath = sys.path[:]
  62.     codefile = tempfile.mktemp()
  63.     f = open(codefile, 'w')
  64.     f.write(code)
  65.     f.close()
  66.     
  67.     try:
  68.         sys.path.insert(0, root)
  69.         if verbose:
  70.             print 'sys.path =', sys.path
  71.         
  72.         
  73.         try:
  74.             execfile(codefile, globals(), { })
  75.         except:
  76.             traceback.print_exc(file = sys.stdout)
  77.  
  78.     finally:
  79.         sys.path[:] = savepath
  80.         
  81.         try:
  82.             cleanout(root)
  83.         except (os.error, IOError):
  84.             pass
  85.  
  86.         os.remove(codefile)
  87.  
  88.  
  89. tests = [
  90.     ('t1', [
  91.         ('t1', None),
  92.         ('t1 __init__.py', '')], 'import t1'),
  93.     ('t2', [
  94.         ('t2', None),
  95.         ('t2 __init__.py', "'doc for t2'; print __name__, 'loading'"),
  96.         ('t2 sub', None),
  97.         ('t2 sub __init__.py', ''),
  98.         ('t2 sub subsub', None),
  99.         ('t2 sub subsub __init__.py', "print __name__, 'loading'; spam = 1")], '\nimport t2\nprint t2.__doc__\nimport t2.sub\nimport t2.sub.subsub\nprint t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__\nimport t2\nfrom t2 import *\nprint dir()\nfrom t2 import sub\nfrom t2.sub import subsub\nfrom t2.sub.subsub import spam\nprint sub.__name__, subsub.__name__\nprint sub.subsub.__name__\nprint dir()\nimport t2.sub\nimport t2.sub.subsub\nprint t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__\nfrom t2 import *\nprint dir()\n'),
  100.     ('t3', [
  101.         ('t3', None),
  102.         ('t3 __init__.py', "print __name__, 'loading'"),
  103.         ('t3 sub', None),
  104.         ('t3 sub __init__.py', ''),
  105.         ('t3 sub subsub', None),
  106.         ('t3 sub subsub __init__.py', "print __name__, 'loading'; spam = 1")], '\nimport t3.sub.subsub\nprint t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__\nreload(t3)\nreload(t3.sub)\nreload(t3.sub.subsub)\n'),
  107.     ('t4', [
  108.         ('t4.py', "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
  109.         ('t4', None),
  110.         ('t4 __init__.py', "print __name__, 'loading'"),
  111.         ('t4 sub.py', "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
  112.         ('t4 sub', None),
  113.         ('t4 sub __init__.py', ''),
  114.         ('t4 sub subsub.py', "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
  115.         ('t4 sub subsub', None),
  116.         ('t4 sub subsub __init__.py', "print __name__, 'loading'; spam = 1")], '\nfrom t4.sub.subsub import *\nprint "t4.sub.subsub.spam =", spam\n'),
  117.     ('t5', [
  118.         ('t5', None),
  119.         ('t5 __init__.py', 'import t5.foo'),
  120.         ('t5 string.py', "print __name__, 'loading'; spam = 1"),
  121.         ('t5 foo.py', "print __name__, 'loading'; import string; print string.spam")], '\nimport t5\nfrom t5 import *\nprint dir()\nimport t5\nprint dir(t5)\nprint dir(t5.foo)\nprint dir(t5.string)\n'),
  122.     ('t6', [
  123.         ('t6', None),
  124.         ('t6 __init__.py', "__all__ = ['spam', 'ham', 'eggs']"),
  125.         ('t6 spam.py', "print __name__, 'loading'"),
  126.         ('t6 ham.py', "print __name__, 'loading'"),
  127.         ('t6 eggs.py', "print __name__, 'loading'")], '\nimport t6\nprint dir(t6)\nfrom t6 import *\nprint dir(t6)\nprint dir()\n')]
  128. nontests = [
  129.     ('x5', [], 'import a' + '.a' * 400),
  130.     ('x6', [], 'import a' + '.a' * 499),
  131.     ('x7', [], 'import a' + '.a' * 500),
  132.     ('x8', [], 'import a' + '.a' * 1100),
  133.     ('x9', [], 'import ' + 'a' * 400),
  134.     ('x10', [], 'import ' + 'a' * 500),
  135.     ('x11', [], 'import ' + 'a' * 998),
  136.     ('x12', [], 'import ' + 'a' * 999),
  137.     ('x13', [], 'import ' + 'a' * 999),
  138.     ('x14', [], 'import ' + 'a' * 2000)]
  139. args = []
  140. for name, hier, code in tests:
  141.     print 'running test', name
  142.     runtest(hier, code)
  143.  
  144. import sys
  145. import imp
  146.  
  147. try:
  148.     import sys.imp as sys
  149. except ImportError:
  150.     pass
  151.  
  152. raise TestFailed, "No ImportError exception on 'import sys.imp'"
  153.