1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# -*- coding: iso-8859-1 -*-
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest, test.test_support
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys, os, cStringIO
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport struct
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport operator
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SysModuleTest(unittest.TestCase):
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test.test_support.reap_children()
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_original_displayhook(self):
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import __builtin__
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        savestdout = sys.stdout
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        out = cStringIO.StringIO()
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stdout = out
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dh = sys.__displayhook__
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, dh)
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(__builtin__, "_"):
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del __builtin__._
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dh(None)
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(out.getvalue(), "")
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not hasattr(__builtin__, "_"))
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dh(42)
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(out.getvalue(), "42\n")
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(__builtin__._, 42)
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del sys.stdout
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(RuntimeError, dh, 42)
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stdout = savestdout
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_lost_displayhook(self):
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        olddisplayhook = sys.displayhook
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del sys.displayhook
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        code = compile("42", "<string>", "single")
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(RuntimeError, eval, code)
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.displayhook = olddisplayhook
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_custom_displayhook(self):
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        olddisplayhook = sys.displayhook
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def baddisplayhook(obj):
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise ValueError
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.displayhook = baddisplayhook
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        code = compile("42", "<string>", "single")
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, eval, code)
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.displayhook = olddisplayhook
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_original_excepthook(self):
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        savestderr = sys.stderr
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        err = cStringIO.StringIO()
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stderr = err
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eh = sys.__excepthook__
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, eh)
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise ValueError(42)
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError, exc:
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eh(*sys.exc_info())
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stderr = savestderr
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # FIXME: testing the code for a lost or replaced excepthook in
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Python/pythonrun.c::PyErr_PrintEx() is tricky.
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_exc_clear(self):
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.exc_clear, 42)
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that exc_info is present and matches exc, then clear it, and
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check that it worked.
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def clear_check(exc):
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            typ, value, traceback = sys.exc_info()
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(typ is not None)
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(value is exc)
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(traceback is not None)
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            with test.test_support.check_py3k_warnings():
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sys.exc_clear()
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            typ, value, traceback = sys.exc_info()
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(typ is None)
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(value is None)
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(traceback is None)
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def clear():
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise ValueError, 42
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except ValueError, exc:
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                clear_check(exc)
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Raise an exception and check that it can be cleared
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        clear()
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that a frame currently handling an exception is
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # unaffected by calling exc_clear in a nested frame.
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise ValueError, 13
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError, exc:
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            typ1, value1, traceback1 = sys.exc_info()
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            clear()
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            typ2, value2, traceback2 = sys.exc_info()
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(typ1 is typ2)
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(value1 is exc)
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(value1 is value2)
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(traceback1 is traceback2)
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check that an exception can be cleared outside of an except block
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        clear_check(exc)
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_exit(self):
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.exit, 42, 42)
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call without argument
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.exit(0)
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except SystemExit, exc:
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(exc.code, 0)
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("wrong exception")
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no exception")
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call with tuple argument with one entry
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # entry will be unpacked
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.exit(42)
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except SystemExit, exc:
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(exc.code, 42)
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("wrong exception")
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no exception")
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call with integer argument
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.exit((42,))
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except SystemExit, exc:
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(exc.code, 42)
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("wrong exception")
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no exception")
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call with string argument
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.exit("exit")
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except SystemExit, exc:
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(exc.code, "exit")
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("wrong exception")
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no exception")
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call with tuple argument with two entries
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.exit((17, 23))
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except SystemExit, exc:
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(exc.code, (17, 23))
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("wrong exception")
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no exception")
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # test that the exit machinery handles SystemExits properly
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import subprocess
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # both unnormalized...
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        rc = subprocess.call([sys.executable, "-c",
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                              "raise SystemExit, 46"])
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(rc, 46)
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # ... and normalized
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        rc = subprocess.call([sys.executable, "-c",
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                              "raise SystemExit(47)"])
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(rc, 47)
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def check_exit_message(code, expected, env=None):
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            process = subprocess.Popen([sys.executable, "-c", code],
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                       stderr=subprocess.PIPE, env=env)
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            stdout, stderr = process.communicate()
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(process.returncode, 1)
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(stderr.startswith(expected),
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "%s doesn't start with %s" % (repr(stderr), repr(expected)))
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # test that stderr buffer if flushed before the exit message is written
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # into stderr
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_exit_message(
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            b"unflushed,message")
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # test that the unicode message is encoded to the stderr encoding
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env = os.environ.copy()
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env['PYTHONIOENCODING'] = 'latin-1'
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_exit_message(
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            r'import sys; sys.exit(u"h\xe9")',
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            b"h\xe9", env=env)
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getdefaultencoding(self):
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test.test_support.have_unicode:
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(TypeError, sys.getdefaultencoding, 42)
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # can't check more than the type, as the user might have changed it
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIsInstance(sys.getdefaultencoding(), str)
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # testing sys.settrace() is done in test_sys_settrace.py
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # testing sys.setprofile() is done in test_sys_setprofile.py
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_setcheckinterval(self):
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.setcheckinterval)
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        orig = sys.getcheckinterval()
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for n in 0, 100, 120, orig: # orig last to restore starting state
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.setcheckinterval(n)
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(sys.getcheckinterval(), n)
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursionlimit(self):
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.getrecursionlimit, 42)
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        oldlimit = sys.getrecursionlimit()
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.setrecursionlimit)
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, sys.setrecursionlimit, -42)
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.setrecursionlimit(10000)
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getrecursionlimit(), 10000)
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.setrecursionlimit(oldlimit)
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, sys.setrecursionlimit, 1 << 31)
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.setrecursionlimit((1 << 31) - 5)
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # issue13546: isinstance(e, ValueError) used to fail
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # when the recursion limit is close to 1<<31
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise ValueError()
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except ValueError, e:
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.setrecursionlimit(oldlimit)
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getwindowsversion(self):
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Raise SkipTest if sys doesn't have getwindowsversion attribute
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test.test_support.get_attribute(sys, "getwindowsversion")
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        v = sys.getwindowsversion()
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(v), 5)
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v[0], int)
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v[1], int)
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v[2], int)
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v[3], int)
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v[4], str)
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(IndexError, operator.getitem, v, 5)
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.major, int)
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.minor, int)
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.build, int)
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.platform, int)
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.service_pack, str)
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.service_pack_minor, int)
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.service_pack_major, int)
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.suite_mask, int)
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(v.product_type, int)
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(v[0], v.major)
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(v[1], v.minor)
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(v[2], v.build)
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(v[3], v.platform)
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(v[4], v.service_pack)
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This is how platform.py calls it. Make sure tuple
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        #  still has 5 elements
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        maj, min, buildno, plat, csd = sys.getwindowsversion()
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dlopenflags(self):
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(sys, "setdlopenflags"):
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(hasattr(sys, "getdlopenflags"))
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(TypeError, sys.getdlopenflags, 42)
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            oldflags = sys.getdlopenflags()
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(TypeError, sys.setdlopenflags)
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.setdlopenflags(oldflags+1)
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(sys.getdlopenflags(), oldflags+1)
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.setdlopenflags(oldflags)
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_refcount(self):
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # n here must be a global in order for this test to pass while
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tracing with a python function.  Tracing calls PyFrame_FastToLocals
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # which will add a copy of any locals to the frame object, causing
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the reference count to increase by 2 instead of 1.
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global n
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.getrefcount)
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = sys.getrefcount(None)
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = None
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getrefcount(None), c+1)
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del n
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getrefcount(None), c)
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(sys, "gettotalrefcount"):
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIsInstance(sys.gettotalrefcount(), int)
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getframe(self):
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys._getframe, 42, 42)
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, sys._getframe, 2000000000)
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            SysModuleTest.test_getframe.im_func.func_code \
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            is sys._getframe().f_code
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        )
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # sys._current_frames() is a CPython-only gimmick.
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_current_frames(self):
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        have_threads = True
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import thread
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ImportError:
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            have_threads = False
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if have_threads:
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.current_frames_with_threads()
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.current_frames_without_threads()
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Test sys._current_frames() in a WITH_THREADS build.
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test.test_support.reap_threads
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def current_frames_with_threads(self):
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import threading, thread
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import traceback
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Spawn a thread that blocks at a known place.  Then the main
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # thread does sys._current_frames(), and verifies that the frames
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # returned make sense.
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        entered_g = threading.Event()
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        leave_g = threading.Event()
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        thread_info = []  # the thread's id
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def f123():
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            g456()
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def g456():
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            thread_info.append(thread.get_ident())
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            entered_g.set()
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            leave_g.wait()
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = threading.Thread(target=f123)
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.start()
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        entered_g.wait()
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # At this point, t has finished its entered_g.set(), although it's
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # impossible to guess whether it's still on that line or has moved on
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # to its leave_g.wait().
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(thread_info), 1)
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        thread_id = thread_info[0]
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = sys._current_frames()
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        main_id = thread.get_ident()
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(main_id, d)
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(thread_id, d)
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that the captured main-thread frame is _this_ frame.
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        frame = d.pop(main_id)
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(frame is sys._getframe())
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that the captured thread frame is blocked in g456, called
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # from f123.  This is a litte tricky, since various bits of
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # threading.py are also in the thread's call stack.
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        frame = d.pop(thread_id)
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        stack = traceback.extract_stack(frame)
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if funcname == "f123":
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                break
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("didn't find f123() on thread's call stack")
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sourceline, "g456()")
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # And the next record must be for g456().
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        filename, lineno, funcname, sourceline = stack[i+1]
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(funcname, "g456")
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"])
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Reap the spawned thread.
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        leave_g.set()
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t.join()
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Test sys._current_frames() when thread support doesn't exist.
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def current_frames_without_threads(self):
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Not much happens here:  there is only one thread, with artificial
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # "thread id" 0.
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = sys._current_frames()
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(d), 1)
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(0, d)
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(d[0] is sys._getframe())
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_attributes(self):
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.api_version, int)
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.argv, list)
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(sys.byteorder, ("little", "big"))
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.builtin_module_names, tuple)
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.copyright, basestring)
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.exec_prefix, basestring)
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.executable, basestring)
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(sys.float_info), 11)
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.float_info.radix, 2)
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(sys.long_info), 2)
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(sys.long_info.sizeof_digit >= 1)
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(sys.long_info.bits_per_digit), int)
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(sys.long_info.sizeof_digit), int)
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.hexversion, int)
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.maxint, int)
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test.test_support.have_unicode:
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIsInstance(sys.maxunicode, int)
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.platform, basestring)
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.prefix, basestring)
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.version, basestring)
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        vi = sys.version_info
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi[:], tuple)
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(vi), 5)
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi[0], int)
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi[1], int)
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi[2], int)
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(vi[3], ("alpha", "beta", "candidate", "final"))
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi[4], int)
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi.major, int)
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi.minor, int)
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi.micro, int)
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final"))
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(vi.serial, int)
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(vi[0], vi.major)
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(vi[1], vi.minor)
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(vi[2], vi.micro)
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(vi[3], vi.releaselevel)
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(vi[4], vi.serial)
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(vi > (1,0,0))
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(sys.float_repr_style, str)
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(sys.float_repr_style, ('short', 'legacy'))
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_43581(self):
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Can't use sys.stdout, as this is a cStringIO object when
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the test runs under regrtest.
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(sys.__stdout__.encoding == sys.__stderr__.encoding)
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_sys_flags(self):
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(sys.flags)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        attrs = ("debug", "py3k_warning", "division_warning", "division_new",
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "inspect", "interactive", "optimize", "dont_write_bytecode",
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "no_site", "ignore_environment", "tabcheck", "verbose",
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "unicode", "bytes_warning", "hash_randomization")
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for attr in attrs:
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(hasattr(sys.flags, attr), attr)
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(repr(sys.flags))
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_clear_type_cache(self):
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys._clear_type_cache()
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_ioencoding(self):
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import subprocess
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env = dict(os.environ)
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # not representable in ASCII.
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env["PYTHONIOENCODING"] = "cp424"
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                             stdout = subprocess.PIPE, env=env)
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        out = p.communicate()[0].strip()
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(out, unichr(0xa2).encode("cp424"))
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env["PYTHONIOENCODING"] = "ascii:replace"
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                             stdout = subprocess.PIPE, env=env)
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        out = p.communicate()[0].strip()
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(out, '?')
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_call_tracing(self):
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.call_tracing(str, (2,)), "2")
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sys.call_tracing, str, 2)
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_executable(self):
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # sys.executable should be absolute
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(os.path.abspath(sys.executable), sys.executable)
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue #7774: Ensure that sys.executable is an empty string if argv[0]
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # has been set to an non existent program name and Python is unable to
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # retrieve the real program name
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import subprocess
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # For a normal installation, it should work without 'cwd'
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # argument. For test runs in the build directory, see #7774.
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        python_dir = os.path.dirname(os.path.realpath(sys.executable))
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p = subprocess.Popen(
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ["nonexistent", "-c", 'import sys; print repr(sys.executable)'],
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        executable = p.communicate()[0].strip()
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p.wait()
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(executable, ["''", repr(sys.executable)])
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SizeofTest(unittest.TestCase):
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.P = struct.calcsize('P')
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.longdigit = sys.long_info.sizeof_digit
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import _testcapi
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.file = open(test.test_support.TESTFN, 'wb')
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.file.close()
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test.test_support.unlink(test.test_support.TESTFN)
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    check_sizeof = test.test_support.check_sizeof
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_gc_head_size(self):
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check that the gc header size is added to objects tracked by the gc.
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        size = test.test_support.calcobjsize
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        gc_header_size = self.gc_headsize
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bool objects are not gc tracked
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getsizeof(True), size('l'))
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # but lists are
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_default(self):
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        size = test.test_support.calcobjsize
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sys.getsizeof(True, -1), size('l'))
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_objecttypes(self):
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check all types defined in Objects/
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        size = test.test_support.calcobjsize
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        vsize = test.test_support.calcvobjsize
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check = self.check_sizeof
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bool
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(True, size('l'))
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # buffer
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with test.test_support.check_py3k_warnings():
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(buffer(''), size('2P2Pil'))
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # builtin_function_or_method
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(len, size('3P'))
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bytearray
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        samples = ['', 'u'*100000]
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for sample in samples:
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = bytearray(sample)
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(x, vsize('iPP') + x.__alloc__())
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bytearray_iterator
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter(bytearray()), size('PP'))
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # cell
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def get_cell():
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = 42
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def inner():
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return x
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return inner
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(get_cell().func_closure[0], size('P'))
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # classobj (old-style class)
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class class_oldstyle():
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def method():
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(class_oldstyle, size('7P'))
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # instance (old-style class)
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(class_oldstyle(), size('3P'))
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # instancemethod (old-style class)
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(class_oldstyle().method, size('4P'))
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # complex
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(complex(0,1), size('2d'))
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # code
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(get_cell().func_code, size('4i8Pi3P'))
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # BaseException
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(BaseException(), size('3P'))
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # UnicodeEncodeError
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(UnicodeEncodeError("", u"", 0, 0, ""), size('5P2PP'))
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # UnicodeDecodeError
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(UnicodeDecodeError("", "", 0, 0, ""), size('5P2PP'))
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # UnicodeTranslateError
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(UnicodeTranslateError(u"", 0, 1, ""), size('5P2PP'))
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method_descriptor (descriptor object)
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(str.lower, size('2PP'))
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # classmethod_descriptor (descriptor object)
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # member_descriptor (descriptor object)
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import datetime
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(datetime.timedelta.days, size('2PP'))
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # getset_descriptor (descriptor object)
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import __builtin__
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(__builtin__.file.closed, size('2PP'))
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # wrapper_descriptor (descriptor object)
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(int.__add__, size('2P2P'))
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # dictproxy
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(C.__dict__, size('P'))
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method-wrapper (descriptor object)
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check({}.__iter__, size('2P'))
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # dict
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check({}, size('3P2P' + 8*'P2P'))
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(x, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # dictionary-keyiterator
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check({}.iterkeys(), size('P2PPP'))
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # dictionary-valueiterator
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check({}.itervalues(), size('P2PPP'))
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # dictionary-itemiterator
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check({}.iteritems(), size('P2PPP'))
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # ellipses
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(Ellipsis, size(''))
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # EncodingMap
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import codecs, encodings.iso8859_3
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(x, size('32B2iB'))
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # enumerate
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(enumerate([]), size('l3P'))
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # file
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(self.file, size('4P2i4P3i3P3i'))
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # float
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(float(0), size('d'))
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # sys.floatinfo
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(sys.float_info, vsize('') + self.P * len(sys.float_info))
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # frame
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import inspect
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        CO_MAXBLOCKS = 20
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = inspect.currentframe()
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ncells = len(x.f_code.co_cellvars)
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nfrees = len(x.f_code.co_freevars)
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 ncells + nfrees - 1
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # function
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def func(): pass
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(func, size('9P'))
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class c():
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @staticmethod
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo():
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @classmethod
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def bar(cls):
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # staticmethod
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(foo, size('P'))
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # classmethod
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(bar, size('P'))
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # generator
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def get_gen(): yield 1
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(get_gen(), size('Pi2P'))
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # integer
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(1, size('l'))
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(100, size('l'))
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # iterator
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter('abc'), size('lP'))
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # callable-iterator
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import re
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(re.finditer('',''), size('2P'))
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # list
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        samples = [[], [1,2,3], ['1', '2', '3']]
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for sample in samples:
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(sample, vsize('PP') + len(sample)*self.P)
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # sortwrapper (list)
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # cmpwrapper (list)
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # listiterator (list)
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter([]), size('lP'))
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # listreverseiterator (list)
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(reversed([]), size('lP'))
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # long
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(0L, vsize(''))
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(1L, vsize('') + self.longdigit)
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(-1L, vsize('') + self.longdigit)
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        PyLong_BASE = 2**sys.long_info.bits_per_digit
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(long(PyLong_BASE), vsize('') + 2*self.longdigit)
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(long(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(long(PyLong_BASE**2), vsize('') + 3*self.longdigit)
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # module
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(unittest, size('P'))
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # None
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(None, size(''))
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # object
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(object(), size(''))
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # property (descriptor object)
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getx(self): return self.__x
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setx(self, value): self.__x = value
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def delx(self): del self.__x
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = property(getx, setx, delx, "")
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(x, size('4Pi'))
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # PyCObject
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # PyCapsule
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # rangeiterator
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter(xrange(1)), size('4l'))
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # reverse
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(reversed(''), size('PP'))
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # set
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # frozenset
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        PySet_MINSIZE = 8
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        samples = [[], range(10), range(50)]
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = size('3P2P' + PySet_MINSIZE*'lP' + 'lP')
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for sample in samples:
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            minused = len(sample)
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if minused == 0: tmp = 1
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # the computation of minused is actually a bit more complicated
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # but this suffices for the sizeof test
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            minused = minused*2
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            newsize = PySet_MINSIZE
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while newsize <= minused:
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                newsize = newsize << 1
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if newsize <= 8:
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(set(sample), s)
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(frozenset(sample), s)
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(set(sample), s + newsize*struct.calcsize('lP'))
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(frozenset(sample), s + newsize*struct.calcsize('lP'))
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # setiterator
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter(set()), size('P3P'))
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # slice
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(slice(1), size('3P'))
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # str
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        vh = test.test_support._vheader
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check('', struct.calcsize(vh + 'lic'))
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check('abc', struct.calcsize(vh + 'lic') + 3)
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # super
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(super(int), size('3P'))
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tuple
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check((), vsize(''))
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check((1,2,3), vsize('') + 3*self.P)
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tupleiterator
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(iter(()), size('lP'))
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # type
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (PyTypeObject + PyNumberMethods +  PyMappingMethods +
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        #  PySequenceMethods + PyBufferProcs)
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('41P 10P 3P 6P')
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class newstyleclass(object):
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(newstyleclass, s)
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # builtin type
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(int, s)
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # NotImplementedType
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import types
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(types.NotImplementedType, s)
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # unicode
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        usize = len(u'\0'.encode('unicode-internal'))
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        samples = [u'', u'1'*100]
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # we need to test for both sizes, because we don't know if the string
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # has been cached
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for s in samples:
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            check(s, size('PPlP') + usize * (len(s) + 1))
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # weakref
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import weakref
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(weakref.ref(int), size('2Pl2P'))
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # weakproxy
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # weakcallableproxy
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(weakref.proxy(int), size('2Pl2P'))
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # xrange
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(xrange(1), size('3l'))
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(xrange(66000), size('3l'))
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_pythontypes(self):
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check all types defined in Python/
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        size = test.test_support.calcobjsize
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        vsize = test.test_support.calcvobjsize
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check = self.check_sizeof
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # _ast.AST
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import _ast
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(_ast.AST(), size(''))
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # imp.NullImporter
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import imp
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(imp.NullImporter(self.file.name), size(''))
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise TypeError
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            tb = sys.exc_info()[2]
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # traceback
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if tb != None:
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(tb, size('2P2i'))
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # symtable entry
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # sys.flags
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(sys.flags, vsize('') + self.P * len(sys.flags))
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_classes = (SysModuleTest, SizeofTest)
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test.test_support.run_unittest(*test_classes)
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
777