Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame] | 1 | from test_support import TESTFN |
| 2 | |
| 3 | import os |
| 4 | import random |
| 5 | |
| 6 | source = TESTFN + ".py" |
| 7 | pyc = TESTFN + ".pyc" |
| 8 | pyo = TESTFN + ".pyo" |
| 9 | |
| 10 | f = open(source, "w") |
| 11 | print >> f, "# This will test Python's ability to import a .py file" |
| 12 | a = random.randrange(1000) |
| 13 | b = random.randrange(1000) |
| 14 | print >> f, "a =", a |
| 15 | print >> f, "b =", b |
| 16 | f.close() |
| 17 | |
| 18 | try: |
| 19 | try: |
| 20 | mod = __import__(TESTFN) |
| 21 | except ImportError, err: |
| 22 | raise ValueError, "import from .py failed: %s" % err |
| 23 | |
| 24 | if mod.a != a or mod.b != b: |
| 25 | print a, "!=", mod.a |
| 26 | print b, "!=", mod.b |
| 27 | raise ValueError, "module loaded (%s) but contents invalid" % mod |
| 28 | finally: |
| 29 | os.unlink(source) |
| 30 | |
| 31 | try: |
| 32 | try: |
| 33 | reload(mod) |
| 34 | except ImportError, err: |
| 35 | raise ValueError, "import from .pyc/.pyo failed: %s" % err |
| 36 | finally: |
| 37 | try: |
| 38 | os.unlink(pyc) |
| 39 | except os.error: |
| 40 | pass |
| 41 | try: |
| 42 | os.unlink(pyo) |
| 43 | except os.error: |
| 44 | pass |