0001 # -*- coding: iso-8859-1 -*- 0002 import unittest, test.test_support 0003 import sys, cStringIO 0004 0005 class SysModuleTest(unittest.TestCase): 0006 0007 def test_original_displayhook(self): 0008 import __builtin__ 0009 savestdout = sys.stdout 0010 out = cStringIO.StringIO() 0011 sys.stdout = out 0012 0013 dh = sys.__displayhook__ 0014 0015 self.assertRaises(TypeError, dh) 0016 if hasattr(__builtin__, "_"): 0017 del __builtin__._ 0018 0019 dh(None) 0020 self.assertEqual(out.getvalue(), "") 0021 self.assert_(not hasattr(__builtin__, "_")) 0022 dh(42) 0023 self.assertEqual(out.getvalue(), "42\n") 0024 self.assertEqual(__builtin__._, 42) 0025 0026 del sys.stdout 0027 self.assertRaises(RuntimeError, dh, 42) 0028 0029 sys.stdout = savestdout 0030 0031 def test_lost_displayhook(self): 0032 olddisplayhook = sys.displayhook 0033 del sys.displayhook 0034 code = compile("42", "<string>", "single") 0035 self.assertRaises(RuntimeError, eval, code) 0036 sys.displayhook = olddisplayhook 0037 0038 def test_custom_displayhook(self): 0039 olddisplayhook = sys.displayhook 0040 def baddisplayhook(obj): 0041 raise ValueError 0042 sys.displayhook = baddisplayhook 0043 code = compile("42", "<string>", "single") 0044 self.assertRaises(ValueError, eval, code) 0045 sys.displayhook = olddisplayhook 0046 0047 def test_original_excepthook(self): 0048 savestderr = sys.stderr 0049 err = cStringIO.StringIO() 0050 sys.stderr = err 0051 0052 eh = sys.__excepthook__ 0053 0054 self.assertRaises(TypeError, eh) 0055 try: 0056 raise ValueError(42) 0057 except ValueError, exc: 0058 eh(*sys.exc_info()) 0059 0060 sys.stderr = savestderr 0061 self.assert_(err.getvalue().endswith("ValueError: 42\n")) 0062 0063 # FIXME: testing the code for a lost or replaced excepthook in 0064 # Python/pythonrun.c::PyErr_PrintEx() is tricky. 0065 0066 def test_exc_clear(self): 0067 self.assertRaises(TypeError, sys.exc_clear, 42) 0068 0069 # Verify that exc_info is present and matches exc, then clear it, and 0070 # check that it worked. 0071 def clear_check(exc): 0072 typ, value, traceback = sys.exc_info() 0073 self.assert_(typ is not None) 0074 self.assert_(value is exc) 0075 self.assert_(traceback is not None) 0076 0077 sys.exc_clear() 0078 0079 typ, value, traceback = sys.exc_info() 0080 self.assert_(typ is None) 0081 self.assert_(value is None) 0082 self.assert_(traceback is None) 0083 0084 def clear(): 0085 try: 0086 raise ValueError, 42 0087 except ValueError, exc: 0088 clear_check(exc) 0089 0090 # Raise an exception and check that it can be cleared 0091 clear() 0092 0093 # Verify that a frame currently handling an exception is 0094 # unaffected by calling exc_clear in a nested frame. 0095 try: 0096 raise ValueError, 13 0097 except ValueError, exc: 0098 typ1, value1, traceback1 = sys.exc_info() 0099 clear() 0100 typ2, value2, traceback2 = sys.exc_info() 0101 0102 self.assert_(typ1 is typ2) 0103 self.assert_(value1 is exc) 0104 self.assert_(value1 is value2) 0105 self.assert_(traceback1 is traceback2) 0106 0107 # Check that an exception can be cleared outside of an except block 0108 clear_check(exc) 0109 0110 def test_exit(self): 0111 self.assertRaises(TypeError, sys.exit, 42, 42) 0112 0113 # call without argument 0114 try: 0115 sys.exit(0) 0116 except SystemExit, exc: 0117 self.assertEquals(exc.code, 0) 0118 except: 0119 self.fail("wrong exception") 0120 else: 0121 self.fail("no exception") 0122 0123 # call with tuple argument with one entry 0124 # entry will be unpacked 0125 try: 0126 sys.exit(42) 0127 except SystemExit, exc: 0128 self.assertEquals(exc.code, 42) 0129 except: 0130 self.fail("wrong exception") 0131 else: 0132 self.fail("no exception") 0133 0134 # call with integer argument 0135 try: 0136 sys.exit((42,)) 0137 except SystemExit, exc: 0138 self.assertEquals(exc.code, 42) 0139 except: 0140 self.fail("wrong exception") 0141 else: 0142 self.fail("no exception") 0143 0144 # call with string argument 0145 try: 0146 sys.exit("exit") 0147 except SystemExit, exc: 0148 self.assertEquals(exc.code, "exit") 0149 except: 0150 self.fail("wrong exception") 0151 else: 0152 self.fail("no exception") 0153 0154 # call with tuple argument with two entries 0155 try: 0156 sys.exit((17, 23)) 0157 except SystemExit, exc: 0158 self.assertEquals(exc.code, (17, 23)) 0159 except: 0160 self.fail("wrong exception") 0161 else: 0162 self.fail("no exception") 0163 0164 def test_getdefaultencoding(self): 0165 if test.test_support.have_unicode: 0166 self.assertRaises(TypeError, sys.getdefaultencoding, 42) 0167 # can't check more than the type, as the user might have changed it 0168 self.assert_(isinstance(sys.getdefaultencoding(), str)) 0169 0170 # testing sys.settrace() is done in test_trace.py 0171 # testing sys.setprofile() is done in test_profile.py 0172 0173 def test_setcheckinterval(self): 0174 self.assertRaises(TypeError, sys.setcheckinterval) 0175 orig = sys.getcheckinterval() 0176 for n in 0, 100, 120, orig: # orig last to restore starting state 0177 sys.setcheckinterval(n) 0178 self.assertEquals(sys.getcheckinterval(), n) 0179 0180 def test_recursionlimit(self): 0181 self.assertRaises(TypeError, sys.getrecursionlimit, 42) 0182 oldlimit = sys.getrecursionlimit() 0183 self.assertRaises(TypeError, sys.setrecursionlimit) 0184 self.assertRaises(ValueError, sys.setrecursionlimit, -42) 0185 sys.setrecursionlimit(10000) 0186 self.assertEqual(sys.getrecursionlimit(), 10000) 0187 sys.setrecursionlimit(oldlimit) 0188 0189 def test_getwindowsversion(self): 0190 if hasattr(sys, "getwindowsversion"): 0191 v = sys.getwindowsversion() 0192 self.assert_(isinstance(v, tuple)) 0193 self.assertEqual(len(v), 5) 0194 self.assert_(isinstance(v[0], int)) 0195 self.assert_(isinstance(v[1], int)) 0196 self.assert_(isinstance(v[2], int)) 0197 self.assert_(isinstance(v[3], int)) 0198 self.assert_(isinstance(v[4], str)) 0199 0200 def test_dlopenflags(self): 0201 if hasattr(sys, "setdlopenflags"): 0202 self.assert_(hasattr(sys, "getdlopenflags")) 0203 self.assertRaises(TypeError, sys.getdlopenflags, 42) 0204 oldflags = sys.getdlopenflags() 0205 self.assertRaises(TypeError, sys.setdlopenflags) 0206 sys.setdlopenflags(oldflags+1) 0207 self.assertEqual(sys.getdlopenflags(), oldflags+1) 0208 sys.setdlopenflags(oldflags) 0209 0210 def test_refcount(self): 0211 self.assertRaises(TypeError, sys.getrefcount) 0212 c = sys.getrefcount(None) 0213 n = None 0214 self.assertEqual(sys.getrefcount(None), c+1) 0215 del n 0216 self.assertEqual(sys.getrefcount(None), c) 0217 if hasattr(sys, "gettotalrefcount"): 0218 self.assert_(isinstance(sys.gettotalrefcount(), int)) 0219 0220 def test_getframe(self): 0221 self.assertRaises(TypeError, sys._getframe, 42, 42) 0222 self.assertRaises(ValueError, sys._getframe, 2000000000) 0223 self.assert_( 0224 SysModuleTest.test_getframe.im_func.func_code \ 0225 is sys._getframe().f_code 0226 ) 0227 0228 def test_attributes(self): 0229 self.assert_(isinstance(sys.api_version, int)) 0230 self.assert_(isinstance(sys.argv, list)) 0231 self.assert_(sys.byteorder in ("little", "big")) 0232 self.assert_(isinstance(sys.builtin_module_names, tuple)) 0233 self.assert_(isinstance(sys.copyright, basestring)) 0234 self.assert_(isinstance(sys.exec_prefix, basestring)) 0235 self.assert_(isinstance(sys.executable, basestring)) 0236 self.assert_(isinstance(sys.hexversion, int)) 0237 self.assert_(isinstance(sys.maxint, int)) 0238 self.assert_(isinstance(sys.maxunicode, int)) 0239 self.assert_(isinstance(sys.platform, basestring)) 0240 self.assert_(isinstance(sys.prefix, basestring)) 0241 self.assert_(isinstance(sys.version, basestring)) 0242 vi = sys.version_info 0243 self.assert_(isinstance(vi, tuple)) 0244 self.assertEqual(len(vi), 5) 0245 self.assert_(isinstance(vi[0], int)) 0246 self.assert_(isinstance(vi[1], int)) 0247 self.assert_(isinstance(vi[2], int)) 0248 self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) 0249 self.assert_(isinstance(vi[4], int)) 0250 0251 def test_main(): 0252 test.test_support.run_unittest(SysModuleTest) 0253 0254 if __name__ == "__main__": 0255 test_main() 0256
Generated by PyXR 0.9.4