annotate Lib/test/test_sys.py @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents b31fcbab0520
children 7caf54ce529e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
2 import unittest, test.test_support
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
3 import sys, cStringIO, os
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
4 import struct
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
5
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
6 class SysModuleTest(unittest.TestCase):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
7
56873
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
8 def tearDown(self):
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
9 test.test_support.reap_children()
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
10
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
11 def test_original_displayhook(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
12 import __builtin__
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
13 savestdout = sys.stdout
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
14 out = cStringIO.StringIO()
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
15 sys.stdout = out
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
16
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
17 dh = sys.__displayhook__
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
18
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
19 self.assertRaises(TypeError, dh)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
20 if hasattr(__builtin__, "_"):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
21 del __builtin__._
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
22
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
23 dh(None)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
24 self.assertEqual(out.getvalue(), "")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
25 self.assertTrue(not hasattr(__builtin__, "_"))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
26 dh(42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
27 self.assertEqual(out.getvalue(), "42\n")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
28 self.assertEqual(__builtin__._, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
29
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
30 del sys.stdout
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
31 self.assertRaises(RuntimeError, dh, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
32
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
33 sys.stdout = savestdout
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
34
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
35 def test_lost_displayhook(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
36 olddisplayhook = sys.displayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
37 del sys.displayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
38 code = compile("42", "<string>", "single")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
39 self.assertRaises(RuntimeError, eval, code)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
40 sys.displayhook = olddisplayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
41
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
42 def test_custom_displayhook(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
43 olddisplayhook = sys.displayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
44 def baddisplayhook(obj):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
45 raise ValueError
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
46 sys.displayhook = baddisplayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
47 code = compile("42", "<string>", "single")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
48 self.assertRaises(ValueError, eval, code)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
49 sys.displayhook = olddisplayhook
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
50
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
51 def test_original_excepthook(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
52 savestderr = sys.stderr
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
53 err = cStringIO.StringIO()
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
54 sys.stderr = err
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
55
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
56 eh = sys.__excepthook__
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
57
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
58 self.assertRaises(TypeError, eh)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
59 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
60 raise ValueError(42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
61 except ValueError, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
62 eh(*sys.exc_info())
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
63
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
64 sys.stderr = savestderr
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
65 self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
66
27661
35278aaae775 Fix typo.
Walter Dörwald <walter@livinglogic.de>
parents: 27660
diff changeset
67 # FIXME: testing the code for a lost or replaced excepthook in
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
68 # Python/pythonrun.c::PyErr_PrintEx() is tricky.
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
69
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
70 def test_exc_clear(self):
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
71 self.assertRaises(TypeError, sys.exc_clear, 42)
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
72
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
73 # Verify that exc_info is present and matches exc, then clear it, and
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
74 # check that it worked.
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
75 def clear_check(exc):
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
76 typ, value, traceback = sys.exc_info()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
77 self.assertTrue(typ is not None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
78 self.assertTrue(value is exc)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
79 self.assertTrue(traceback is not None)
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
80
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
81 sys.exc_clear()
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
82
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
83 typ, value, traceback = sys.exc_info()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
84 self.assertTrue(typ is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
85 self.assertTrue(value is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
86 self.assertTrue(traceback is None)
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
87
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
88 def clear():
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
89 try:
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
90 raise ValueError, 42
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
91 except ValueError, exc:
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
92 clear_check(exc)
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
93
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
94 # Raise an exception and check that it can be cleared
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
95 clear()
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
96
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
97 # Verify that a frame currently handling an exception is
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
98 # unaffected by calling exc_clear in a nested frame.
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
99 try:
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
100 raise ValueError, 13
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
101 except ValueError, exc:
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
102 typ1, value1, traceback1 = sys.exc_info()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
103 clear()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
104 typ2, value2, traceback2 = sys.exc_info()
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
105
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
106 self.assertTrue(typ1 is typ2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
107 self.assertTrue(value1 is exc)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
108 self.assertTrue(value1 is value2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
109 self.assertTrue(traceback1 is traceback2)
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
110
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
111 # Check that an exception can be cleared outside of an except block
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
112 clear_check(exc)
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
113
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 def test_exit(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
115 self.assertRaises(TypeError, sys.exit, 42, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
117 # call without argument
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
118 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
119 sys.exit(0)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
120 except SystemExit, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
121 self.assertEquals(exc.code, 0)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
122 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
123 self.fail("wrong exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
124 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
125 self.fail("no exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
126
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
127 # call with tuple argument with one entry
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
128 # entry will be unpacked
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
129 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
130 sys.exit(42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
131 except SystemExit, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
132 self.assertEquals(exc.code, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
133 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
134 self.fail("wrong exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
135 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
136 self.fail("no exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
137
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
138 # call with integer argument
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
139 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
140 sys.exit((42,))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
141 except SystemExit, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
142 self.assertEquals(exc.code, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
143 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
144 self.fail("wrong exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
146 self.fail("no exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
147
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
148 # call with string argument
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
149 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
150 sys.exit("exit")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
151 except SystemExit, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
152 self.assertEquals(exc.code, "exit")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
153 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
154 self.fail("wrong exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
155 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
156 self.fail("no exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
157
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
158 # call with tuple argument with two entries
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
159 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
160 sys.exit((17, 23))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
161 except SystemExit, exc:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
162 self.assertEquals(exc.code, (17, 23))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
163 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
164 self.fail("wrong exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
165 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
166 self.fail("no exception")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
167
34589
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
168 # test that the exit machinery handles SystemExits properly
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
169 import subprocess
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
170 # both unnormalized...
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
171 rc = subprocess.call([sys.executable, "-c",
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
172 "raise SystemExit, 46"])
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
173 self.assertEqual(rc, 46)
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
174 # ... and normalized
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
175 rc = subprocess.call([sys.executable, "-c",
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
176 "raise SystemExit(47)"])
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
177 self.assertEqual(rc, 47)
34591
1ae2c3b66e57 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34589
diff changeset
178
1ae2c3b66e57 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34589
diff changeset
179
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180 def test_getdefaultencoding(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 if test.test_support.have_unicode:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
182 self.assertRaises(TypeError, sys.getdefaultencoding, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
183 # can't check more than the type, as the user might have changed it
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
184 self.assertTrue(isinstance(sys.getdefaultencoding(), str))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
185
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
186 # testing sys.settrace() is done in test_trace.py
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
187 # testing sys.setprofile() is done in test_profile.py
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
188
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
189 def test_setcheckinterval(self):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
190 self.assertRaises(TypeError, sys.setcheckinterval)
29765
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
191 orig = sys.getcheckinterval()
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
192 for n in 0, 100, 120, orig: # orig last to restore starting state
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
193 sys.setcheckinterval(n)
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
194 self.assertEquals(sys.getcheckinterval(), n)
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
195
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
196 def test_recursionlimit(self):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
197 self.assertRaises(TypeError, sys.getrecursionlimit, 42)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
198 oldlimit = sys.getrecursionlimit()
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
199 self.assertRaises(TypeError, sys.setrecursionlimit)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
200 self.assertRaises(ValueError, sys.setrecursionlimit, -42)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
201 sys.setrecursionlimit(10000)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
202 self.assertEqual(sys.getrecursionlimit(), 10000)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
203 sys.setrecursionlimit(oldlimit)
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
204
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
205 def test_getwindowsversion(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
206 if hasattr(sys, "getwindowsversion"):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
207 v = sys.getwindowsversion()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
208 self.assertTrue(isinstance(v, tuple))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
209 self.assertEqual(len(v), 5)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
210 self.assertTrue(isinstance(v[0], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
211 self.assertTrue(isinstance(v[1], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
212 self.assertTrue(isinstance(v[2], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
213 self.assertTrue(isinstance(v[3], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
214 self.assertTrue(isinstance(v[4], str))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
215
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
216 def test_dlopenflags(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
217 if hasattr(sys, "setdlopenflags"):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
218 self.assertTrue(hasattr(sys, "getdlopenflags"))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
219 self.assertRaises(TypeError, sys.getdlopenflags, 42)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
220 oldflags = sys.getdlopenflags()
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
221 self.assertRaises(TypeError, sys.setdlopenflags)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
222 sys.setdlopenflags(oldflags+1)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
223 self.assertEqual(sys.getdlopenflags(), oldflags+1)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
224 sys.setdlopenflags(oldflags)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
225
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
226 def test_refcount(self):
52629
1ac1193a61d2 Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents: 52233
diff changeset
227 # n here must be a global in order for this test to pass while
1ac1193a61d2 Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents: 52233
diff changeset
228 # tracing with a python function. Tracing calls PyFrame_FastToLocals
1ac1193a61d2 Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents: 52233
diff changeset
229 # which will add a copy of any locals to the frame object, causing
1ac1193a61d2 Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents: 52233
diff changeset
230 # the reference count to increase by 2 instead of 1.
1ac1193a61d2 Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents: 52233
diff changeset
231 global n
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
232 self.assertRaises(TypeError, sys.getrefcount)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
233 c = sys.getrefcount(None)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
234 n = None
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
235 self.assertEqual(sys.getrefcount(None), c+1)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
236 del n
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
237 self.assertEqual(sys.getrefcount(None), c)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
238 if hasattr(sys, "gettotalrefcount"):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
239 self.assertTrue(isinstance(sys.gettotalrefcount(), int))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
240
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
241 def test_getframe(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 self.assertRaises(TypeError, sys._getframe, 42, 42)
27934
f67ab6b66454 Fix SF bug #688424, 64-bit test problems
Neal Norwitz <nnorwitz@gmail.com>
parents: 27661
diff changeset
243 self.assertRaises(ValueError, sys._getframe, 2000000000)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
244 self.assertTrue(
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
245 SysModuleTest.test_getframe.im_func.func_code \
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
246 is sys._getframe().f_code
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
247 )
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
248
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
249 # sys._current_frames() is a CPython-only gimmick.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
250 def test_current_frames(self):
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
251 have_threads = True
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
252 try:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
253 import thread
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
254 except ImportError:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
255 have_threads = False
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
256
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
257 if have_threads:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
258 self.current_frames_with_threads()
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
259 else:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
260 self.current_frames_without_threads()
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
261
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
262 # Test sys._current_frames() in a WITH_THREADS build.
56873
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
263 @test.test_support.reap_threads
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
264 def current_frames_with_threads(self):
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
265 import threading, thread
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
266 import traceback
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
267
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
268 # Spawn a thread that blocks at a known place. Then the main
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
269 # thread does sys._current_frames(), and verifies that the frames
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
270 # returned make sense.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
271 entered_g = threading.Event()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
272 leave_g = threading.Event()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
273 thread_info = [] # the thread's id
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
274
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
275 def f123():
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
276 g456()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
277
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
278 def g456():
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
279 thread_info.append(thread.get_ident())
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
280 entered_g.set()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
281 leave_g.wait()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
282
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
283 t = threading.Thread(target=f123)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
284 t.start()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
285 entered_g.wait()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
286
39169
99aa84224f18 current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents: 39118
diff changeset
287 # At this point, t has finished its entered_g.set(), although it's
99aa84224f18 current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents: 39118
diff changeset
288 # impossible to guess whether it's still on that line or has moved on
99aa84224f18 current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents: 39118
diff changeset
289 # to its leave_g.wait().
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
290 self.assertEqual(len(thread_info), 1)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
291 thread_id = thread_info[0]
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
292
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
293 d = sys._current_frames()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
294
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
295 main_id = thread.get_ident()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
296 self.assertTrue(main_id in d)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
297 self.assertTrue(thread_id in d)
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
298
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
299 # Verify that the captured main-thread frame is _this_ frame.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
300 frame = d.pop(main_id)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
301 self.assertTrue(frame is sys._getframe())
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
302
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
303 # Verify that the captured thread frame is blocked in g456, called
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
304 # from f123. This is a litte tricky, since various bits of
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
305 # threading.py are also in the thread's call stack.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
306 frame = d.pop(thread_id)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
307 stack = traceback.extract_stack(frame)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
308 for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
309 if funcname == "f123":
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
310 break
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
311 else:
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
312 self.fail("didn't find f123() on thread's call stack")
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
313
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
314 self.assertEqual(sourceline, "g456()")
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
315
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
316 # And the next record must be for g456().
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
317 filename, lineno, funcname, sourceline = stack[i+1]
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
318 self.assertEqual(funcname, "g456")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
319 self.assertTrue(sourceline in ["leave_g.wait()", "entered_g.set()"])
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
320
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
321 # Reap the spawned thread.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
322 leave_g.set()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
323 t.join()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
324
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
325 # Test sys._current_frames() when thread support doesn't exist.
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
326 def current_frames_without_threads(self):
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
327 # Not much happens here: there is only one thread, with artificial
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
328 # "thread id" 0.
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
329 d = sys._current_frames()
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
330 self.assertEqual(len(d), 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
331 self.assertTrue(0 in d)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
332 self.assertTrue(d[0] is sys._getframe())
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
333
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
334 def test_attributes(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
335 self.assertTrue(isinstance(sys.api_version, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
336 self.assertTrue(isinstance(sys.argv, list))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
337 self.assertTrue(sys.byteorder in ("little", "big"))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
338 self.assertTrue(isinstance(sys.builtin_module_names, tuple))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
339 self.assertTrue(isinstance(sys.copyright, basestring))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
340 self.assertTrue(isinstance(sys.exec_prefix, basestring))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
341 self.assertTrue(isinstance(sys.executable, basestring))
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 39169
diff changeset
342 self.assertEqual(len(sys.float_info), 11)
44670
d66e5837aa57 Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
Christian Heimes <christian@cheimes.de>
parents: 44669
diff changeset
343 self.assertEqual(sys.float_info.radix, 2)
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
344 self.assertEqual(len(sys.long_info), 2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
345 self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
346 self.assertTrue(sys.long_info.sizeof_digit >= 1)
52739
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52629
diff changeset
347 self.assertEqual(type(sys.long_info.bits_per_digit), int)
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52629
diff changeset
348 self.assertEqual(type(sys.long_info.sizeof_digit), int)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
349 self.assertTrue(isinstance(sys.hexversion, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
350 self.assertTrue(isinstance(sys.maxint, int))
35309
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34591
diff changeset
351 if test.test_support.have_unicode:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
352 self.assertTrue(isinstance(sys.maxunicode, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
353 self.assertTrue(isinstance(sys.platform, basestring))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
354 self.assertTrue(isinstance(sys.prefix, basestring))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
355 self.assertTrue(isinstance(sys.version, basestring))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
356 vi = sys.version_info
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
357 self.assertTrue(isinstance(vi[:], tuple))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
358 self.assertEqual(len(vi), 5)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
359 self.assertTrue(isinstance(vi[0], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
360 self.assertTrue(isinstance(vi[1], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
361 self.assertTrue(isinstance(vi[2], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
362 self.assertTrue(vi[3] in ("alpha", "beta", "candidate", "final"))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
363 self.assertTrue(isinstance(vi[4], int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
364 self.assertTrue(isinstance(vi.major, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
365 self.assertTrue(isinstance(vi.minor, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
366 self.assertTrue(isinstance(vi.micro, int))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
367 self.assertTrue(vi.releaselevel in
51365
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
368 ("alpha", "beta", "candidate", "final"))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
369 self.assertTrue(isinstance(vi.serial, int))
51365
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
370 self.assertEqual(vi[0], vi.major)
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
371 self.assertEqual(vi[1], vi.minor)
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
372 self.assertEqual(vi[2], vi.micro)
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
373 self.assertEqual(vi[3], vi.releaselevel)
e239e977bb32 Implement issue #4285, convert sys.version_info to a named
Eric Smith <eric@trueblade.com>
parents: 50036
diff changeset
374 self.assertEqual(vi[4], vi.serial)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
375 self.assertTrue(vi > (1,0,0))
56715
aacee9749d15 Issue #7117 (backport py3k float repr) continued:
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
376 self.assertIsInstance(sys.float_repr_style, str)
aacee9749d15 Issue #7117 (backport py3k float repr) continued:
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
377 self.assertTrue(sys.float_repr_style in ('short', 'legacy'))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
378
37424
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
379 def test_43581(self):
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
380 # Can't use sys.stdout, as this is a cStringIO object when
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
381 # the test runs under regrtest.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
382 self.assertTrue(sys.__stdout__.encoding == sys.__stderr__.encoding)
37424
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
383
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
384 def test_sys_flags(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
385 self.assertTrue(sys.flags)
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
386 attrs = ("debug", "py3k_warning", "division_warning", "division_new",
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
387 "inspect", "interactive", "optimize", "dont_write_bytecode",
44678
5dfded9ad8a0 Typo fixes
Andrew M. Kuchling <amk@amk.ca>
parents: 44670
diff changeset
388 "no_site", "ignore_environment", "tabcheck", "verbose",
46759
b8e7a161611c Fix sys.flags to properly expose bytes_warning.
Brett Cannon <bcannon@gmail.com>
parents: 45253
diff changeset
389 "unicode", "bytes_warning")
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
390 for attr in attrs:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
391 self.assertTrue(hasattr(sys.flags, attr), attr)
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
392 self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52739
diff changeset
393 self.assertTrue(repr(sys.flags))
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
394
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
395 def test_clear_type_cache(self):
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
396 sys._clear_type_cache()
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
397
47413
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
398 def test_ioencoding(self):
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
399 import subprocess,os
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
400 env = dict(os.environ)
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
401
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
402 # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
403 # not representable in ASCII.
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
404
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
405 env["PYTHONIOENCODING"] = "cp424"
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
406 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
407 stdout = subprocess.PIPE, env=env)
56873
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
408 out = p.communicate()[0].strip()
47413
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
409 self.assertEqual(out, unichr(0xa2).encode("cp424"))
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
410
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
411 env["PYTHONIOENCODING"] = "ascii:replace"
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
412 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
413 stdout = subprocess.PIPE, env=env)
56873
b31fcbab0520 Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56715
diff changeset
414 out = p.communicate()[0].strip()
47413
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
415 self.assertEqual(out, '?')
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
416
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
417
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
418 class SizeofTest(unittest.TestCase):
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
419
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
420 TPFLAGS_HAVE_GC = 1<<14
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
421 TPFLAGS_HEAPTYPE = 1L<<9
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
422
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
423 def setUp(self):
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
424 self.c = len(struct.pack('c', ' '))
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
425 self.H = len(struct.pack('H', 0))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
426 self.i = len(struct.pack('i', 0))
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
427 self.l = len(struct.pack('l', 0))
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
428 self.P = len(struct.pack('P', 0))
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
429 # due to missing size_t information from struct, it is assumed that
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
430 # sizeof(Py_ssize_t) = sizeof(void*)
47829
7eb3737657d1 Corrected inconsistencies in sizeof tests and addressed issue pointed
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47824
diff changeset
431 self.header = 'PP'
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
432 self.vheader = self.header + 'P'
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
433 if hasattr(sys, "gettotalrefcount"):
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
434 self.header += '2P'
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
435 self.vheader += '2P'
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
436 self.longdigit = sys.long_info.sizeof_digit
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
437 import _testcapi
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
438 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
439 self.file = open(test.test_support.TESTFN, 'wb')
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
440
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
441 def tearDown(self):
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
442 self.file.close()
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
443 test.test_support.unlink(test.test_support.TESTFN)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
444
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
445 def check_sizeof(self, o, size):
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
446 result = sys.getsizeof(o)
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
447 if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
448 ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
449 size += self.gc_headsize
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
450 msg = 'wrong size for %s: got %d, expected %d' \
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
451 % (type(o), result, size)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
452 self.assertEqual(result, size, msg)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
453
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
454 def calcsize(self, fmt):
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
455 """Wrapper around struct.calcsize which enforces the alignment of the
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
456 end of a structure to the alignment requirement of pointer.
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
457
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
458 Note: This wrapper should only be used if a pointer member is included
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
459 and no member with a size larger than a pointer exists.
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
460 """
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
461 return struct.calcsize(fmt + '0P')
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
462
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
463 def test_gc_head_size(self):
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
464 # Check that the gc header size is added to objects tracked by the gc.
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
465 h = self.header
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
466 size = self.calcsize
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
467 gc_header_size = self.gc_headsize
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
468 # bool objects are not gc tracked
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
469 self.assertEqual(sys.getsizeof(True), size(h + 'l'))
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
470 # but lists are
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
471 self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
472
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
473 def test_default(self):
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
474 h = self.header
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
475 size = self.calcsize
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
476 self.assertEqual(sys.getsizeof(True, -1), size(h + 'l'))
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
477
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
478 def test_objecttypes(self):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
479 # check all types defined in Objects/
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
480 h = self.header
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
481 vh = self.vheader
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
482 size = self.calcsize
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
483 check = self.check_sizeof
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
484 # bool
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
485 check(True, size(h + 'l'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
486 # buffer
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
487 check(buffer(''), size(h + '2P2Pil'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
488 # builtin_function_or_method
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
489 check(len, size(h + '3P'))
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
490 # bytearray
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
491 samples = ['', 'u'*100000]
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
492 for sample in samples:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
493 x = bytearray(sample)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
494 check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
495 # bytearray_iterator
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
496 check(iter(bytearray()), size(h + 'PP'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
497 # cell
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
498 def get_cell():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
499 x = 42
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
500 def inner():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
501 return x
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
502 return inner
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
503 check(get_cell().func_closure[0], size(h + 'P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
504 # classobj (old-style class)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
505 class class_oldstyle():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
506 def method():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
507 pass
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
508 check(class_oldstyle, size(h + '6P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
509 # instance (old-style class)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
510 check(class_oldstyle(), size(h + '3P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
511 # instancemethod (old-style class)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
512 check(class_oldstyle().method, size(h + '4P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
513 # complex
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
514 check(complex(0,1), size(h + '2d'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
515 # code
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
516 check(get_cell().func_code, size(h + '4i8Pi2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
517 # BaseException
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
518 check(BaseException(), size(h + '3P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
519 # UnicodeEncodeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
520 check(UnicodeEncodeError("", u"", 0, 0, ""), size(h + '5P2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
521 # UnicodeDecodeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
522 check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
523 # UnicodeTranslateError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
524 check(UnicodeTranslateError(u"", 0, 1, ""), size(h + '5P2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
525 # method_descriptor (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
526 check(str.lower, size(h + '2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
527 # classmethod_descriptor (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
528 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
529 # member_descriptor (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
530 import datetime
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
531 check(datetime.timedelta.days, size(h + '2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
532 # getset_descriptor (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
533 import __builtin__
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
534 check(__builtin__.file.closed, size(h + '2PP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
535 # wrapper_descriptor (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
536 check(int.__add__, size(h + '2P2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
537 # dictproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
538 class C(object): pass
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
539 check(C.__dict__, size(h + 'P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
540 # method-wrapper (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
541 check({}.__iter__, size(h + '2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
542 # dict
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
543 check({}, size(h + '3P2P' + 8*'P2P'))
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
544 x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
545 check(x, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
546 # dictionary-keyiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
547 check({}.iterkeys(), size(h + 'P2PPP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
548 # dictionary-valueiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
549 check({}.itervalues(), size(h + 'P2PPP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
550 # dictionary-itemiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
551 check({}.iteritems(), size(h + 'P2PPP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
552 # ellipses
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
553 check(Ellipsis, size(h + ''))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
554 # EncodingMap
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
555 import codecs, encodings.iso8859_3
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
556 x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
557 check(x, size(h + '32B2iB'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
558 # enumerate
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
559 check(enumerate([]), size(h + 'l3P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
560 # file
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
561 check(self.file, size(h + '4P2i4P3i3Pi'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
562 # float
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
563 check(float(0), size(h + 'd'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
564 # sys.floatinfo
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
565 check(sys.float_info, size(vh) + self.P * len(sys.float_info))
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
566 # frame
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
567 import inspect
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
568 CO_MAXBLOCKS = 20
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
569 x = inspect.currentframe()
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
570 ncells = len(x.f_code.co_cellvars)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
571 nfrees = len(x.f_code.co_freevars)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
572 extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
573 ncells + nfrees - 1
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
574 check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
575 # function
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
576 def func(): pass
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
577 check(func, size(h + '9P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
578 class c():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
579 @staticmethod
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
580 def foo():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
581 pass
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
582 @classmethod
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
583 def bar(cls):
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
584 pass
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
585 # staticmethod
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
586 check(foo, size(h + 'P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
587 # classmethod
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
588 check(bar, size(h + 'P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
589 # generator
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
590 def get_gen(): yield 1
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
591 check(get_gen(), size(h + 'Pi2P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
592 # integer
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
593 check(1, size(h + 'l'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
594 check(100, size(h + 'l'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
595 # iterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
596 check(iter('abc'), size(h + 'lP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
597 # callable-iterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
598 import re
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
599 check(re.finditer('',''), size(h + '2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
600 # list
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
601 samples = [[], [1,2,3], ['1', '2', '3']]
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
602 for sample in samples:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
603 check(sample, size(vh + 'PP') + len(sample)*self.P)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
604 # sortwrapper (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
605 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
606 # cmpwrapper (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
607 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
608 # listiterator (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
609 check(iter([]), size(h + 'lP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
610 # listreverseiterator (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
611 check(reversed([]), size(h + 'lP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
612 # long
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51365
diff changeset
613 check(0L, size(vh))
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
614 check(1L, size(vh) + self.longdigit)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
615 check(-1L, size(vh) + self.longdigit)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
616 PyLong_BASE = 2**sys.long_info.bits_per_digit
52739
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52629
diff changeset
617 check(long(PyLong_BASE), size(vh) + 2*self.longdigit)
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52629
diff changeset
618 check(long(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52629
diff changeset
619 check(long(PyLong_BASE**2), size(vh) + 3*self.longdigit)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
620 # module
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
621 check(unittest, size(h + 'P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
622 # None
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
623 check(None, size(h + ''))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
624 # object
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
625 check(object(), size(h + ''))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
626 # property (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
627 class C(object):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
628 def getx(self): return self.__x
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
629 def setx(self, value): self.__x = value
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
630 def delx(self): del self.__x
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
631 x = property(getx, setx, delx, "")
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
632 check(x, size(h + '4Pi'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
633 # PyCObject
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
634 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
635 # rangeiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
636 check(iter(xrange(1)), size(h + '4l'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
637 # reverse
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
638 check(reversed(''), size(h + 'PP'))
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
639 # set
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
640 # frozenset
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
641 PySet_MINSIZE = 8
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
642 samples = [[], range(10), range(50)]
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
643 s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
644 for sample in samples:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
645 minused = len(sample)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
646 if minused == 0: tmp = 1
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
647 # the computation of minused is actually a bit more complicated
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
648 # but this suffices for the sizeof test
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
649 minused = minused*2
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
650 newsize = PySet_MINSIZE
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
651 while newsize <= minused:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
652 newsize = newsize << 1
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
653 if newsize <= 8:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
654 check(set(sample), s)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
655 check(frozenset(sample), s)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
656 else:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
657 check(set(sample), s + newsize*struct.calcsize('lP'))
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
658 check(frozenset(sample), s + newsize*struct.calcsize('lP'))
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
659 # setiterator
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
660 check(iter(set()), size(h + 'P3P'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
661 # slice
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
662 check(slice(1), size(h + '3P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
663 # str
50036
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 47957
diff changeset
664 check('', struct.calcsize(vh + 'li') + 1)
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 47957
diff changeset
665 check('abc', struct.calcsize(vh + 'li') + 1 + 3*self.c)
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
666 # super
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
667 check(super(int), size(h + '3P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
668 # tuple
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
669 check((), size(vh))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
670 check((1,2,3), size(vh) + 3*self.P)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
671 # tupleiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
672 check(iter(()), size(h + 'lP'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
673 # type
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
674 # (PyTypeObject + PyNumberMethods + PyMappingMethods +
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
675 # PySequenceMethods + PyBufferProcs)
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
676 s = size(vh + 'P2P15Pl4PP9PP11PI') + size('41P 10P 3P 6P')
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
677 class newstyleclass(object):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
678 pass
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
679 check(newstyleclass, s)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
680 # builtin type
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
681 check(int, s)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
682 # NotImplementedType
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
683 import types
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
684 check(types.NotImplementedType, s)
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
685 # unicode
47743
b8c9a3543381 Issue 3048: Fixed sys.sizeof test fails with wide unicode.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47682
diff changeset
686 usize = len(u'\0'.encode('unicode-internal'))
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
687 samples = [u'', u'1'*100]
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
688 # we need to test for both sizes, because we don't know if the string
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
689 # has been cached
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
690 for s in samples:
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
691 check(s, size(h + 'PPlP') + usize * (len(s) + 1))
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
692 # weakref
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
693 import weakref
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
694 check(weakref.ref(int), size(h + '2Pl2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
695 # weakproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
696 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
697 # weakcallableproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
698 check(weakref.proxy(int), size(h + '2Pl2P'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
699 # xrange
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
700 check(xrange(1), size(h + '3l'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
701 check(xrange(66000), size(h + '3l'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
702
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
703 def test_pythontypes(self):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
704 # check all types defined in Python/
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
705 h = self.header
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
706 vh = self.vheader
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
707 size = self.calcsize
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
708 check = self.check_sizeof
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
709 # _ast.AST
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
710 import _ast
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
711 check(_ast.AST(), size(h + ''))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
712 # imp.NullImporter
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
713 import imp
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
714 check(imp.NullImporter(self.file.name), size(h + ''))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
715 try:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
716 raise TypeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
717 except TypeError:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
718 tb = sys.exc_info()[2]
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
719 # traceback
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
720 if tb != None:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
721 check(tb, size(h + '2P2i'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
722 # symtable entry
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
723 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
724 # sys.flags
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
725 check(sys.flags, size(vh) + self.P * len(sys.flags))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
726
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
727
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
728 def test_main():
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
729 test_classes = (SysModuleTest, SizeofTest)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
730
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
731 test.test_support.run_unittest(*test_classes)
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
732
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
733 if __name__ == "__main__":
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
734 test_main()