blob: c7ab75377a82eee99dabb10583a3785021ae2afb [file] [log] [blame]
Guido van Rossume8d2f552000-10-09 22:14:43 +00001from test_support import TESTFN
2
3import os
4import random
5
6source = TESTFN + ".py"
7pyc = TESTFN + ".pyc"
8pyo = TESTFN + ".pyo"
9
10f = open(source, "w")
11print >> f, "# This will test Python's ability to import a .py file"
12a = random.randrange(1000)
13b = random.randrange(1000)
14print >> f, "a =", a
15print >> f, "b =", b
16f.close()
17
18try:
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
28finally:
29 os.unlink(source)
30
31try:
32 try:
33 reload(mod)
34 except ImportError, err:
35 raise ValueError, "import from .pyc/.pyo failed: %s" % err
36finally:
37 try:
38 os.unlink(pyc)
39 except os.error:
40 pass
41 try:
42 os.unlink(pyo)
43 except os.error:
44 pass