| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | # -*- coding: iso-8859-1 -*- | 
					
						
							|  |  |  | import unittest, test.test_support | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | import sys, cStringIO, os | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  | import struct | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class SysModuleTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_original_displayhook(self): | 
					
						
							|  |  |  |         import __builtin__ | 
					
						
							|  |  |  |         savestdout = sys.stdout | 
					
						
							|  |  |  |         out = cStringIO.StringIO() | 
					
						
							|  |  |  |         sys.stdout = out | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dh = sys.__displayhook__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(TypeError, dh) | 
					
						
							|  |  |  |         if hasattr(__builtin__, "_"): | 
					
						
							|  |  |  |             del __builtin__._ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dh(None) | 
					
						
							|  |  |  |         self.assertEqual(out.getvalue(), "") | 
					
						
							|  |  |  |         self.assert_(not hasattr(__builtin__, "_")) | 
					
						
							|  |  |  |         dh(42) | 
					
						
							|  |  |  |         self.assertEqual(out.getvalue(), "42\n") | 
					
						
							|  |  |  |         self.assertEqual(__builtin__._, 42) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         del sys.stdout | 
					
						
							|  |  |  |         self.assertRaises(RuntimeError, dh, 42) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sys.stdout = savestdout | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_lost_displayhook(self): | 
					
						
							|  |  |  |         olddisplayhook = sys.displayhook | 
					
						
							|  |  |  |         del sys.displayhook | 
					
						
							|  |  |  |         code = compile("42", "<string>", "single") | 
					
						
							|  |  |  |         self.assertRaises(RuntimeError, eval, code) | 
					
						
							|  |  |  |         sys.displayhook = olddisplayhook | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_custom_displayhook(self): | 
					
						
							|  |  |  |         olddisplayhook = sys.displayhook | 
					
						
							|  |  |  |         def baddisplayhook(obj): | 
					
						
							|  |  |  |             raise ValueError | 
					
						
							|  |  |  |         sys.displayhook = baddisplayhook | 
					
						
							|  |  |  |         code = compile("42", "<string>", "single") | 
					
						
							|  |  |  |         self.assertRaises(ValueError, eval, code) | 
					
						
							|  |  |  |         sys.displayhook = olddisplayhook | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_original_excepthook(self): | 
					
						
							|  |  |  |         savestderr = sys.stderr | 
					
						
							|  |  |  |         err = cStringIO.StringIO() | 
					
						
							|  |  |  |         sys.stderr = err | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         eh = sys.__excepthook__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(TypeError, eh) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             raise ValueError(42) | 
					
						
							|  |  |  |         except ValueError, exc: | 
					
						
							|  |  |  |             eh(*sys.exc_info()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sys.stderr = savestderr | 
					
						
							|  |  |  |         self.assert_(err.getvalue().endswith("ValueError: 42\n")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-03 23:05:27 +00:00
										 |  |  |     # FIXME: testing the code for a lost or replaced excepthook in | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |     # Python/pythonrun.c::PyErr_PrintEx() is tricky. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  |     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 | 
					
						
							|  |  |  |         # check that it worked. | 
					
						
							|  |  |  |         def clear_check(exc): | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             typ, value, traceback = sys.exc_info() | 
					
						
							|  |  |  |             self.assert_(typ is not None) | 
					
						
							|  |  |  |             self.assert_(value is exc) | 
					
						
							|  |  |  |             self.assert_(traceback is not None) | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             sys.exc_clear() | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             typ, value, traceback = sys.exc_info() | 
					
						
							|  |  |  |             self.assert_(typ is None) | 
					
						
							|  |  |  |             self.assert_(value is None) | 
					
						
							|  |  |  |             self.assert_(traceback is None) | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def clear(): | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 raise ValueError, 42 | 
					
						
							|  |  |  |             except ValueError, exc: | 
					
						
							|  |  |  |                 clear_check(exc) | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Raise an exception and check that it can be cleared | 
					
						
							|  |  |  |         clear() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Verify that a frame currently handling an exception is | 
					
						
							|  |  |  |         # unaffected by calling exc_clear in a nested frame. | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             raise ValueError, 13 | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  |         except ValueError, exc: | 
					
						
							| 
									
										
										
										
											2003-03-01 03:25:41 +00:00
										 |  |  |             typ1, value1, traceback1 = sys.exc_info() | 
					
						
							|  |  |  |             clear() | 
					
						
							|  |  |  |             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) | 
					
						
							| 
									
										
										
										
											2003-03-01 03:20:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Check that an exception can be cleared outside of an except block | 
					
						
							|  |  |  |         clear_check(exc) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |     def test_exit(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, sys.exit, 42, 42) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # call without argument | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sys.exit(0) | 
					
						
							|  |  |  |         except SystemExit, exc: | 
					
						
							|  |  |  |             self.assertEquals(exc.code, 0) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("wrong exception") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("no exception") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # call with tuple argument with one entry | 
					
						
							|  |  |  |         # entry will be unpacked | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sys.exit(42) | 
					
						
							|  |  |  |         except SystemExit, exc: | 
					
						
							|  |  |  |             self.assertEquals(exc.code, 42) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("wrong exception") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("no exception") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # call with integer argument | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sys.exit((42,)) | 
					
						
							|  |  |  |         except SystemExit, exc: | 
					
						
							|  |  |  |             self.assertEquals(exc.code, 42) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("wrong exception") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("no exception") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # call with string argument | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sys.exit("exit") | 
					
						
							|  |  |  |         except SystemExit, exc: | 
					
						
							|  |  |  |             self.assertEquals(exc.code, "exit") | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("wrong exception") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("no exception") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # call with tuple argument with two entries | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sys.exit((17, 23)) | 
					
						
							|  |  |  |         except SystemExit, exc: | 
					
						
							|  |  |  |             self.assertEquals(exc.code, (17, 23)) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.fail("wrong exception") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("no exception") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-15 15:26:11 +00:00
										 |  |  |         # test that the exit machinery handles SystemExits properly | 
					
						
							|  |  |  |         import subprocess | 
					
						
							|  |  |  |         # both unnormalized... | 
					
						
							|  |  |  |         rc = subprocess.call([sys.executable, "-c", | 
					
						
							|  |  |  |                               "raise SystemExit, 46"]) | 
					
						
							|  |  |  |         self.assertEqual(rc, 46) | 
					
						
							|  |  |  |         # ... and normalized | 
					
						
							|  |  |  |         rc = subprocess.call([sys.executable, "-c", | 
					
						
							|  |  |  |                               "raise SystemExit(47)"]) | 
					
						
							|  |  |  |         self.assertEqual(rc, 47) | 
					
						
							| 
									
										
										
										
											2005-02-15 21:50:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |     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): | 
					
						
							| 
									
										
										
										
											2003-02-19 02:35:07 +00:00
										 |  |  |         self.assertRaises(TypeError, sys.setcheckinterval) | 
					
						
							| 
									
										
										
										
											2003-07-06 18:36:54 +00:00
										 |  |  |         orig = sys.getcheckinterval() | 
					
						
							|  |  |  |         for n in 0, 100, 120, orig: # orig last to restore starting state | 
					
						
							|  |  |  |             sys.setcheckinterval(n) | 
					
						
							|  |  |  |             self.assertEquals(sys.getcheckinterval(), n) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_recursionlimit(self): | 
					
						
							| 
									
										
										
										
											2003-02-19 02:35:07 +00:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_refcount(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, sys.getrefcount) | 
					
						
							|  |  |  |         c = sys.getrefcount(None) | 
					
						
							|  |  |  |         n = None | 
					
						
							|  |  |  |         self.assertEqual(sys.getrefcount(None), c+1) | 
					
						
							|  |  |  |         del n | 
					
						
							|  |  |  |         self.assertEqual(sys.getrefcount(None), c) | 
					
						
							|  |  |  |         if hasattr(sys, "gettotalrefcount"): | 
					
						
							|  |  |  |             self.assert_(isinstance(sys.gettotalrefcount(), int)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_getframe(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, sys._getframe, 42, 42) | 
					
						
							| 
									
										
										
										
											2003-02-18 15:22:10 +00:00
										 |  |  |         self.assertRaises(ValueError, sys._getframe, 2000000000) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |         self.assert_( | 
					
						
							|  |  |  |             SysModuleTest.test_getframe.im_func.func_code \ | 
					
						
							|  |  |  |             is sys._getframe().f_code | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-10 21:08:24 +00:00
										 |  |  |     # sys._current_frames() is a CPython-only gimmick. | 
					
						
							|  |  |  |     def test_current_frames(self): | 
					
						
							| 
									
										
										
										
											2006-07-19 00:03:19 +00:00
										 |  |  |         have_threads = True | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import thread | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             have_threads = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if have_threads: | 
					
						
							|  |  |  |             self.current_frames_with_threads() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.current_frames_without_threads() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Test sys._current_frames() in a WITH_THREADS build. | 
					
						
							|  |  |  |     def current_frames_with_threads(self): | 
					
						
							| 
									
										
										
										
											2006-07-10 21:08:24 +00:00
										 |  |  |         import threading, thread | 
					
						
							|  |  |  |         import traceback | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Spawn a thread that blocks at a known place.  Then the main | 
					
						
							|  |  |  |         # thread does sys._current_frames(), and verifies that the frames | 
					
						
							|  |  |  |         # returned make sense. | 
					
						
							|  |  |  |         entered_g = threading.Event() | 
					
						
							|  |  |  |         leave_g = threading.Event() | 
					
						
							|  |  |  |         thread_info = []  # the thread's id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def f123(): | 
					
						
							|  |  |  |             g456() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def g456(): | 
					
						
							|  |  |  |             thread_info.append(thread.get_ident()) | 
					
						
							|  |  |  |             entered_g.set() | 
					
						
							|  |  |  |             leave_g.wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         t = threading.Thread(target=f123) | 
					
						
							|  |  |  |         t.start() | 
					
						
							|  |  |  |         entered_g.wait() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-25 04:07:22 +00:00
										 |  |  |         # At this point, t has finished its entered_g.set(), although it's | 
					
						
							|  |  |  |         # impossible to guess whether it's still on that line or has moved on | 
					
						
							|  |  |  |         # to its leave_g.wait(). | 
					
						
							| 
									
										
										
										
											2006-07-10 21:08:24 +00:00
										 |  |  |         self.assertEqual(len(thread_info), 1) | 
					
						
							|  |  |  |         thread_id = thread_info[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         d = sys._current_frames() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         main_id = thread.get_ident() | 
					
						
							|  |  |  |         self.assert_(main_id in d) | 
					
						
							|  |  |  |         self.assert_(thread_id in d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Verify that the captured main-thread frame is _this_ frame. | 
					
						
							|  |  |  |         frame = d.pop(main_id) | 
					
						
							|  |  |  |         self.assert_(frame is sys._getframe()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Verify that the captured thread frame is blocked in g456, called | 
					
						
							|  |  |  |         # from f123.  This is a litte tricky, since various bits of | 
					
						
							|  |  |  |         # threading.py are also in the thread's call stack. | 
					
						
							|  |  |  |         frame = d.pop(thread_id) | 
					
						
							|  |  |  |         stack = traceback.extract_stack(frame) | 
					
						
							|  |  |  |         for i, (filename, lineno, funcname, sourceline) in enumerate(stack): | 
					
						
							|  |  |  |             if funcname == "f123": | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail("didn't find f123() on thread's call stack") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(sourceline, "g456()") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # And the next record must be for g456(). | 
					
						
							|  |  |  |         filename, lineno, funcname, sourceline = stack[i+1] | 
					
						
							|  |  |  |         self.assertEqual(funcname, "g456") | 
					
						
							| 
									
										
										
										
											2006-07-25 04:07:22 +00:00
										 |  |  |         self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) | 
					
						
							| 
									
										
										
										
											2006-07-10 21:08:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Reap the spawned thread. | 
					
						
							|  |  |  |         leave_g.set() | 
					
						
							|  |  |  |         t.join() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-19 00:03:19 +00:00
										 |  |  |     # Test sys._current_frames() when thread support doesn't exist. | 
					
						
							|  |  |  |     def current_frames_without_threads(self): | 
					
						
							|  |  |  |         # Not much happens here:  there is only one thread, with artificial | 
					
						
							|  |  |  |         # "thread id" 0. | 
					
						
							|  |  |  |         d = sys._current_frames() | 
					
						
							|  |  |  |         self.assertEqual(len(d), 1) | 
					
						
							|  |  |  |         self.assert_(0 in d) | 
					
						
							|  |  |  |         self.assert_(d[0] is sys._getframe()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |     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)) | 
					
						
							| 
									
										
										
										
											2007-12-01 11:20:10 +00:00
										 |  |  |         self.assertEqual(len(sys.float_info), 11) | 
					
						
							| 
									
										
										
										
											2008-01-14 04:13:37 +00:00
										 |  |  |         self.assertEqual(sys.float_info.radix, 2) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |         self.assert_(isinstance(sys.hexversion, int)) | 
					
						
							|  |  |  |         self.assert_(isinstance(sys.maxint, int)) | 
					
						
							| 
									
										
										
										
											2005-08-03 17:09:04 +00:00
										 |  |  |         if test.test_support.have_unicode: | 
					
						
							|  |  |  |             self.assert_(isinstance(sys.maxunicode, int)) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  |         self.assert_(isinstance(sys.platform, basestring)) | 
					
						
							|  |  |  |         self.assert_(isinstance(sys.prefix, basestring)) | 
					
						
							|  |  |  |         self.assert_(isinstance(sys.version, basestring)) | 
					
						
							|  |  |  |         vi = sys.version_info | 
					
						
							|  |  |  |         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)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-03 11:05:39 +00:00
										 |  |  |     def test_43581(self): | 
					
						
							|  |  |  |         # Can't use sys.stdout, as this is a cStringIO object when | 
					
						
							|  |  |  |         # the test runs under regrtest. | 
					
						
							|  |  |  |         self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-14 03:42:48 +00:00
										 |  |  |     def test_sys_flags(self): | 
					
						
							|  |  |  |         self.failUnless(sys.flags) | 
					
						
							|  |  |  |         attrs = ("debug", "py3k_warning", "division_warning", "division_new", | 
					
						
							|  |  |  |                  "inspect", "interactive", "optimize", "dont_write_bytecode", | 
					
						
							| 
									
										
										
										
											2008-01-15 01:29:16 +00:00
										 |  |  |                  "no_site", "ignore_environment", "tabcheck", "verbose", | 
					
						
							| 
									
										
										
										
											2008-05-08 20:23:06 +00:00
										 |  |  |                  "unicode", "bytes_warning") | 
					
						
							| 
									
										
										
										
											2008-01-14 03:42:48 +00:00
										 |  |  |         for attr in attrs: | 
					
						
							|  |  |  |             self.assert_(hasattr(sys.flags, attr), attr) | 
					
						
							|  |  |  |             self.assertEqual(type(getattr(sys.flags, attr)), int, attr) | 
					
						
							|  |  |  |         self.assert_(repr(sys.flags)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 18:00:12 +00:00
										 |  |  |     def test_clear_type_cache(self): | 
					
						
							|  |  |  |         sys._clear_type_cache() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_compact_freelists(self): | 
					
						
							|  |  |  |         sys._compact_freelists() | 
					
						
							|  |  |  |         r = sys._compact_freelists() | 
					
						
							| 
									
										
										
										
											2008-02-09 10:06:20 +00:00
										 |  |  | ##        # freed blocks shouldn't change | 
					
						
							|  |  |  | ##        self.assertEqual(r[0][2], 0) | 
					
						
							|  |  |  | ##        self.assertEqual(r[1][2], 0) | 
					
						
							|  |  |  | ##        # fill freelists | 
					
						
							|  |  |  | ##        ints = list(range(10000)) | 
					
						
							|  |  |  | ##        floats = [float(i) for i in ints] | 
					
						
							|  |  |  | ##        del ints | 
					
						
							|  |  |  | ##        del floats | 
					
						
							|  |  |  | ##        # should free more than 200 blocks each | 
					
						
							|  |  |  | ##        r = sys._compact_freelists() | 
					
						
							|  |  |  | ##        self.assert_(r[0][1] > 100, r[0][1]) | 
					
						
							|  |  |  | ##        self.assert_(r[1][2] > 100, r[1][1]) | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ##        self.assert_(r[0][2] > 100, r[0][2]) | 
					
						
							|  |  |  | ##        self.assert_(r[1][2] > 100, r[1][2]) | 
					
						
							| 
									
										
										
										
											2008-01-14 03:42:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-01 07:20:46 +00:00
										 |  |  |     def test_ioencoding(self): | 
					
						
							|  |  |  |         import subprocess,os | 
					
						
							|  |  |  |         env = dict(os.environ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, | 
					
						
							|  |  |  |         # not representable in ASCII. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         env["PYTHONIOENCODING"] = "cp424" | 
					
						
							|  |  |  |         p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], | 
					
						
							|  |  |  |                              stdout = subprocess.PIPE, env=env) | 
					
						
							|  |  |  |         out = p.stdout.read().strip() | 
					
						
							|  |  |  |         self.assertEqual(out, unichr(0xa2).encode("cp424")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         env["PYTHONIOENCODING"] = "ascii:replace" | 
					
						
							|  |  |  |         p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], | 
					
						
							|  |  |  |                              stdout = subprocess.PIPE, env=env) | 
					
						
							|  |  |  |         out = p.stdout.read().strip() | 
					
						
							|  |  |  |         self.assertEqual(out, '?') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | class SizeofTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.c = len(struct.pack('c', ' ')) | 
					
						
							|  |  |  |         self.H = len(struct.pack('H', 0)) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         self.i = len(struct.pack('i', 0)) | 
					
						
							|  |  |  |         self.l = len(struct.pack('l', 0)) | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.P = len(struct.pack('P', 0)) | 
					
						
							|  |  |  |         # due to missing size_t information from struct, it is assumed that | 
					
						
							|  |  |  |         # sizeof(Py_ssize_t) = sizeof(void*) | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         self.header = 'PP' | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         if hasattr(sys, "gettotalrefcount"): | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |             self.header += '2P' | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         self.file = open(test.test_support.TESTFN, 'wb') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         self.file.close() | 
					
						
							| 
									
										
										
										
											2008-06-01 16:42:16 +00:00
										 |  |  |         test.test_support.unlink(test.test_support.TESTFN) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-10 10:10:31 +00:00
										 |  |  |     def check_sizeof(self, o, size, size2=None): | 
					
						
							|  |  |  |         """Check size of o. Possible are size and optionally size2).""" | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         result = sys.getsizeof(o) | 
					
						
							| 
									
										
										
										
											2008-06-10 10:10:31 +00:00
										 |  |  |         msg = 'wrong size for %s: got %d, expected ' % (type(o), result) | 
					
						
							|  |  |  |         if (size2 != None) and (result != size): | 
					
						
							|  |  |  |             self.assertEqual(result, size2, msg + str(size2)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.assertEqual(result, size, msg + str(size)) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |     def calcsize(self, fmt): | 
					
						
							|  |  |  |         """Wrapper around struct.calcsize which enforces the alignment of the
 | 
					
						
							|  |  |  |         end of a structure to the alignment requirement of pointer. | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         Note: This wrapper should only be used if a pointer member is included | 
					
						
							|  |  |  |         and no member with a size larger than a pointer exists. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return struct.calcsize(fmt + '0P') | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_standardtypes(self): | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         h = self.header | 
					
						
							|  |  |  |         size = self.calcsize | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # bool | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(True, size(h + 'l')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # buffer | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(buffer(''), size(h + '2P2Pil')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # cell | 
					
						
							|  |  |  |         def get_cell(): | 
					
						
							|  |  |  |             x = 42 | 
					
						
							|  |  |  |             def inner(): | 
					
						
							|  |  |  |                 return x | 
					
						
							|  |  |  |             return inner | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(get_cell().func_closure[0], size(h + 'P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # old-style class | 
					
						
							|  |  |  |         class class_oldstyle(): | 
					
						
							|  |  |  |             def method(): | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(class_oldstyle, size(h + '6P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # instance | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(class_oldstyle(), size(h + '3P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # method | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(class_oldstyle().method, size(h + '4P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # code | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(get_cell().func_code, size(h + '4i8Pi2P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # complex | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(complex(0,1), size(h + '2d')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # enumerate | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(enumerate([]), size(h + 'l3P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # reverse | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(reversed(''), size(h + 'PP')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # file | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(self.file, size(h + '4P2i4P3i3Pi')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # float | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(float(0), size(h + 'd')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # function | 
					
						
							|  |  |  |         def func(): pass | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         self.check_sizeof(func, size(h + '9P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         class c(): | 
					
						
							|  |  |  |             @staticmethod | 
					
						
							|  |  |  |             def foo(): | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             @classmethod | 
					
						
							|  |  |  |             def bar(cls): | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             # staticmethod | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |             self.check_sizeof(foo, size(h + 'P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |             # classmethod | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |             self.check_sizeof(bar, size(h + 'P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # generator | 
					
						
							|  |  |  |         def get_gen(): yield 1 | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(get_gen(), size(h + 'Pi2P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # integer | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(1, size(h + 'l')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # builtin_function_or_method | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(abs, size(h + '3P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # module | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(unittest, size(h + 'P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:42:16 +00:00
										 |  |  |         # xrange | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         self.check_sizeof(xrange(1), size(h + '3l')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # slice | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(slice(0), size(h + '3P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         h += 'P' | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # new-style class | 
					
						
							|  |  |  |         class class_newstyle(object): | 
					
						
							|  |  |  |             def method(): | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |         # type (PyTypeObject + PyNumberMethods +  PyMappingMethods + | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         #       PySequenceMethods + PyBufferProcs) | 
					
						
							|  |  |  |         self.check_sizeof(class_newstyle, size('P2P15Pl4PP9PP11PI') +\ | 
					
						
							|  |  |  |                                           size(h + '41P 10P 3P 6P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_specialtypes(self): | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         h = self.header | 
					
						
							|  |  |  |         size = self.calcsize | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # dict | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         self.check_sizeof({}, size(h + '3P2P') + 8*size('P2P')) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         self.check_sizeof(longdict, size(h + '3P2P') + (8+16)*size('P2P')) | 
					
						
							| 
									
										
										
										
											2008-06-10 10:10:31 +00:00
										 |  |  |         # unicode | 
					
						
							| 
									
										
										
										
											2008-06-17 08:42:15 +00:00
										 |  |  |         usize = len(u'\0'.encode('unicode-internal')) | 
					
						
							| 
									
										
										
										
											2008-06-10 10:10:31 +00:00
										 |  |  |         samples = [u'', u'1'*100] | 
					
						
							|  |  |  |         # we need to test for both sizes, because we don't know if the string | 
					
						
							|  |  |  |         # has been cached | 
					
						
							|  |  |  |         for s in samples: | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |             basicsize =  size(h + 'PPlP') + usize * (len(s) + 1) | 
					
						
							| 
									
										
										
										
											2008-06-10 10:10:31 +00:00
										 |  |  |             self.check_sizeof(s, basicsize,\ | 
					
						
							|  |  |  |                                   size2=basicsize + sys.getsizeof(str(s))) | 
					
						
							|  |  |  |         # XXX trigger caching encoded version as Python string | 
					
						
							|  |  |  |         s = samples[1] | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             getattr(sys, s) | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             self.check_sizeof(s, basicsize + sys.getsizeof(str(s))) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-26 15:20:35 +00:00
										 |  |  |         h += 'P' | 
					
						
							|  |  |  |         # list | 
					
						
							|  |  |  |         self.check_sizeof([], size(h + 'PP')) | 
					
						
							|  |  |  |         self.check_sizeof([1, 2, 3], size(h + 'PP') + 3*self.P) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # long | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof(0L, size(h + 'H')) | 
					
						
							|  |  |  |         self.check_sizeof(1L, size(h + 'H')) | 
					
						
							|  |  |  |         self.check_sizeof(-1L, size(h + 'H')) | 
					
						
							|  |  |  |         self.check_sizeof(32768L, size(h + 'H') + self.H) | 
					
						
							|  |  |  |         self.check_sizeof(32768L*32768L-1, size(h + 'H') + self.H) | 
					
						
							|  |  |  |         self.check_sizeof(32768L*32768L, size(h + 'H') + 2*self.H) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |         # string | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof('', size(h + 'lic')) | 
					
						
							|  |  |  |         self.check_sizeof('abc', size(h + 'lic') + 3*self.c) | 
					
						
							| 
									
										
										
										
											2008-06-13 13:29:37 +00:00
										 |  |  |         # tuple | 
					
						
							| 
									
										
										
										
											2008-06-25 09:20:03 +00:00
										 |  |  |         self.check_sizeof((), size(h)) | 
					
						
							|  |  |  |         self.check_sizeof((1,2,3), size(h) + 3*self.P) | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2008-06-01 16:16:17 +00:00
										 |  |  |     test_classes = (SysModuleTest, SizeofTest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test.test_support.run_unittest(*test_classes) | 
					
						
							| 
									
										
										
										
											2003-02-03 23:03:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |