# -*- coding: iso-8859-1 -*-
import unittest
, test
.test_support
class SysModuleTest(unittest
.TestCase
):
def test_original_displayhook(self
):
out
= cStringIO
.StringIO()
self
.assertRaises(TypeError, dh
)
if hasattr(__builtin__
, "_"):
self
.assertEqual(out
.getvalue(), "")
self
.assert_(not hasattr(__builtin__
, "_"))
self
.assertEqual(out
.getvalue(), "42\n")
self
.assertEqual(__builtin__
._, 42)
self
.assertRaises(RuntimeError, dh
, 42)
def test_lost_displayhook(self
):
olddisplayhook
= sys
.displayhook
code
= compile("42", "<string>", "single")
self
.assertRaises(RuntimeError, eval, code
)
sys
.displayhook
= olddisplayhook
def test_custom_displayhook(self
):
olddisplayhook
= sys
.displayhook
sys
.displayhook
= baddisplayhook
code
= compile("42", "<string>", "single")
self
.assertRaises(ValueError, eval, code
)
sys
.displayhook
= olddisplayhook
def test_original_excepthook(self
):
err
= cStringIO
.StringIO()
self
.assertRaises(TypeError, eh
)
self
.assert_(err
.getvalue().endswith("ValueError: 42\n"))
# FIXME: testing the code for a lost or replaced excepthook in
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
def test_exc_clear(self
):
self
.assertRaises(TypeError, sys
.exc_clear
, 42)
# Verify that exc_info is present and matches exc, then clear it, and
typ
, value
, traceback
= sys
.exc_info()
self
.assert_(typ
is not None)
self
.assert_(value
is exc
)
self
.assert_(traceback
is not None)
typ
, value
, traceback
= sys
.exc_info()
self
.assert_(typ
is None)
self
.assert_(value
is None)
self
.assert_(traceback
is None)
# Raise an exception and check that it can be cleared
# Verify that a frame currently handling an exception is
# unaffected by calling exc_clear in a nested frame.
typ1
, value1
, traceback1
= sys
.exc_info()
typ2
, value2
, traceback2
= sys
.exc_info()
self
.assert_(typ1
is typ2
)
self
.assert_(value1
is exc
)
self
.assert_(value1
is value2
)
self
.assert_(traceback1
is traceback2
)
# Check that an exception can be cleared outside of an except block
self
.assertRaises(TypeError, sys
.exit
, 42, 42)
self
.assertEquals(exc
.code
, 0)
self
.fail("wrong exception")
self
.fail("no exception")
# call with tuple argument with one entry
self
.assertEquals(exc
.code
, 42)
self
.fail("wrong exception")
self
.fail("no exception")
# call with integer argument
self
.assertEquals(exc
.code
, 42)
self
.fail("wrong exception")
self
.fail("no exception")
# call with string argument
self
.assertEquals(exc
.code
, "exit")
self
.fail("wrong exception")
self
.fail("no exception")
# call with tuple argument with two entries
self
.assertEquals(exc
.code
, (17, 23))
self
.fail("wrong exception")
self
.fail("no exception")
def test_getdefaultencoding(self
):
if test
.test_support
.have_unicode
:
self
.assertRaises(TypeError, sys
.getdefaultencoding
, 42)
# can't check more than the type, as the user might have changed it
self
.assert_(isinstance(sys
.getdefaultencoding(), str))
# testing sys.settrace() is done in test_trace.py
# testing sys.setprofile() is done in test_profile.py
def test_setcheckinterval(self
):
self
.assertRaises(TypeError, sys
.setcheckinterval
)
orig
= sys
.getcheckinterval()
for n
in 0, 100, 120, orig
: # orig last to restore starting state
self
.assertEquals(sys
.getcheckinterval(), n
)
def test_recursionlimit(self
):
self
.assertRaises(TypeError, sys
.getrecursionlimit
, 42)
oldlimit
= sys
.getrecursionlimit()
self
.assertRaises(TypeError, sys
.setrecursionlimit
)
self
.assertRaises(ValueError, sys
.setrecursionlimit
, -42)
sys
.setrecursionlimit(10000)
self
.assertEqual(sys
.getrecursionlimit(), 10000)
sys
.setrecursionlimit(oldlimit
)
def test_getwindowsversion(self
):
if hasattr(sys
, "getwindowsversion"):
v
= sys
.getwindowsversion()
self
.assert_(isinstance(v
, tuple))
self
.assertEqual(len(v
), 5)
self
.assert_(isinstance(v
[0], int))
self
.assert_(isinstance(v
[1], int))
self
.assert_(isinstance(v
[2], int))
self
.assert_(isinstance(v
[3], int))
self
.assert_(isinstance(v
[4], str))
def test_dlopenflags(self
):
if hasattr(sys
, "setdlopenflags"):
self
.assert_(hasattr(sys
, "getdlopenflags"))
self
.assertRaises(TypeError, sys
.getdlopenflags
, 42)
oldflags
= sys
.getdlopenflags()
self
.assertRaises(TypeError, sys
.setdlopenflags
)
sys
.setdlopenflags(oldflags
+1)
self
.assertEqual(sys
.getdlopenflags(), oldflags
+1)
sys
.setdlopenflags(oldflags
)
self
.assertRaises(TypeError, sys
.getrefcount
)
c
= sys
.getrefcount(None)
self
.assertEqual(sys
.getrefcount(None), c
+1)
self
.assertEqual(sys
.getrefcount(None), c
)
if hasattr(sys
, "gettotalrefcount"):
self
.assert_(isinstance(sys
.gettotalrefcount(), int))
self
.assertRaises(TypeError, sys
._getframe
, 42, 42)
self
.assertRaises(ValueError, sys
._getframe
, 2000000000)
SysModuleTest
.test_getframe
.im_func
.func_code \
is sys
._getframe
().f_code
def test_attributes(self
):
self
.assert_(isinstance(sys
.api_version
, int))
self
.assert_(isinstance(sys
.argv
, list))
self
.assert_(sys
.byteorder
in ("little", "big"))
self
.assert_(isinstance(sys
.builtin_module_names
, tuple))
self
.assert_(isinstance(sys
.copyright
, basestring
))
self
.assert_(isinstance(sys
.exec_prefix
, basestring
))
self
.assert_(isinstance(sys
.executable
, basestring
))
self
.assert_(isinstance(sys
.hexversion
, int))
self
.assert_(isinstance(sys
.maxint
, int))
self
.assert_(isinstance(sys
.maxunicode
, int))
self
.assert_(isinstance(sys
.platform
, basestring
))
self
.assert_(isinstance(sys
.prefix
, basestring
))
self
.assert_(isinstance(sys
.version
, basestring
))
self
.assert_(isinstance(vi
, tuple))
self
.assertEqual(len(vi
), 5)
self
.assert_(isinstance(vi
[0], int))
self
.assert_(isinstance(vi
[1], int))
self
.assert_(isinstance(vi
[2], int))
self
.assert_(vi
[3] in ("alpha", "beta", "candidate", "final"))
self
.assert_(isinstance(vi
[4], int))
test
.test_support
.run_unittest(SysModuleTest
)
if __name__
== "__main__":