from test
.test_support
import TESTFN
, TestFailed
# Brief digression to test that import is case-sensitive: if we got this
# far, we know for sure that "random" exists.
raise TestFailed("import of RAnDoM should have failed (case mismatch)")
# Another brief digression to test the accuracy of manifest float constants.
import double_const
# don't blink -- that *was* the test
for f
in (name
+ os
.extsep
+ "py",
name
+ os
.extsep
+ "pyc",
name
+ os
.extsep
+ "pyo",
name
+ os
.extsep
+ "pyw",
def test_with_extension(ext
): # ext normally ".py"; perhaps ".pyw"
pyo
= TESTFN
+ os
.extsep
+ "pyo"
if sys
.platform
.startswith('java'):
pyc
= TESTFN
+ "$py.class"
pyc
= TESTFN
+ os
.extsep
+ "pyc"
print >> f
, "# This tests Python's ability to import a", ext
, "file."
a
= random
.randrange(1000)
b
= random
.randrange(1000)
raise ValueError("import from %s failed: %s" % (ext
, err
))
if mod
.a
!= a
or mod
.b
!= b
:
raise ValueError("module loaded (%s) but contents invalid" % mod
)
raise ValueError("import from .pyc/.pyo failed: %s" % err
)
sys
.path
.insert(0, os
.curdir
)
test_with_extension(os
.extsep
+ "py")
if sys
.platform
.startswith("win"):
for ext
in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
# Verify that the imp module can correctly load and find .py files
x
= imp
.find_module("os")
os
= imp
.load_module("os", *x
)
def test_module_with_large_stack(module
):
# create module w/list of 65000 elements to test bug #561858
filename
= module
+ os
.extsep
+ 'py'
# create a file with a list of 65000 elements
# compile & remove .py file, we only need .pyc (or .pyo)
py_compile
.compile(filename
)
# need to be able to load from current dir
fname
= module
+ os
.extsep
+ ext
if os
.path
.exists(fname
):
test_module_with_large_stack('longlist')
def test_failing_import_sticks():
source
= TESTFN
+ os
.extsep
+ "py"
# New in 2.4, we shouldn't be able to import that no matter how often
sys
.path
.insert(0, os
.curdir
)
except ZeroDivisionError:
if TESTFN
in sys
.modules
:
raise TestFailed("damaged module in sys.modules", i
)
raise TestFailed("was able to import a damaged module", i
)
test_failing_import_sticks()
def test_failing_reload():
# A failing reload should leave the module object in sys.modules.
source
= TESTFN
+ os
.extsep
+ "py"
sys
.path
.insert(0, os
.curdir
)
if TESTFN
not in sys
.modules
:
raise TestFailed("expected module in sys.modules")
if mod
.a
!= 1 or mod
.b
!= 2:
raise TestFailed("module has wrong attribute values")
# On WinXP, just replacing the .py file wasn't enough to
# convince reload() to reparse it. Maybe the timestamp didn't
# move enough. We force it to get reparsed by removing the
except ZeroDivisionError:
raise TestFailed("was able to reload a damaged module")
# But we still expect the module to be in sys.modules.
mod
= sys
.modules
.get(TESTFN
)
raise TestFailed("expected module to still be in sys.modules")
# We should have replaced a w/ 10, but the old b value should
if mod
.a
!= 10 or mod
.b
!= 2:
raise TestFailed("module has wrong attribute values")
if TESTFN
in sys
.modules
: