annotate Lib/test/test_sys.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents eb550e839460
children 12e2075dbbe0
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
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
8 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
9 import __builtin__
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
10 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
11 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
12 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
13
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
14 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
15
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
16 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
17 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
18 del __builtin__._
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
19
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
20 dh(None)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
21 self.assertEqual(out.getvalue(), "")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
22 self.assert_(not hasattr(__builtin__, "_"))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
23 dh(42)
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(), "42\n")
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
25 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
26
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
27 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
28 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
29
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
30 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
31
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
32 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
33 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
34 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
35 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
36 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
37 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
38
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
39 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
40 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
41 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
42 raise ValueError
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
43 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
44 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
45 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
46 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
47
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
48 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
49 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
50 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
51 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
52
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
53 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
54
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
55 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
56 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
57 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
58 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
59 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
60
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
61 sys.stderr = savestderr
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
62 self.assert_(err.getvalue().endswith("ValueError: 42\n"))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
63
27661
35278aaae775 Fix typo.
Walter Dörwald <walter@livinglogic.de>
parents: 27660
diff changeset
64 # 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
65 # 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
66
28119
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
67 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
68 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
69
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
70 # 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
71 # 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
72 def clear_check(exc):
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
73 typ, value, traceback = sys.exc_info()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
74 self.assert_(typ is not None)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
75 self.assert_(value is exc)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
76 self.assert_(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
77
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
78 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
79
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
80 typ, value, traceback = sys.exc_info()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
81 self.assert_(typ is None)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
82 self.assert_(value is None)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
83 self.assert_(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
84
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
85 def clear():
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
86 try:
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
87 raise ValueError, 42
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
88 except ValueError, exc:
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
89 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
90
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
91 # 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
92 clear()
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 # 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
95 # 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
96 try:
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
97 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
98 except ValueError, exc:
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
99 typ1, value1, traceback1 = sys.exc_info()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
100 clear()
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
101 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
102
28120
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
103 self.assert_(typ1 is typ2)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
104 self.assert_(value1 is exc)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
105 self.assert_(value1 is value2)
d24564e7e999 Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents: 28119
diff changeset
106 self.assert_(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
107
869695cba2cd - New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
108 # 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
109 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
110
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
111 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
112 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
113
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 # 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
115 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116 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
117 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
118 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
119 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
120 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
121 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
122 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
123
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
124 # 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
125 # 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
126 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
127 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
128 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
129 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
130 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
131 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
132 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
133 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
134
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
135 # 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
136 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
137 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
138 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
139 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
140 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
141 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
142 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
143 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
144
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145 # 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
146 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
147 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
148 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
149 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
150 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
151 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
152 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
153 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
154
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
155 # 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
156 try:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
157 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
158 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
159 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
160 except:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
161 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
162 else:
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
163 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
164
34589
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
165 # 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
166 import subprocess
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
167 # both unnormalized...
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
168 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
169 "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
170 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
171 # ... and normalized
a899d690f264 Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents: 29765
diff changeset
172 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
173 "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
174 self.assertEqual(rc, 47)
34591
1ae2c3b66e57 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34589
diff changeset
175
1ae2c3b66e57 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34589
diff changeset
176
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
177 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
178 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
179 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
180 # can't check more than the type, as the user might have changed it
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 self.assert_(isinstance(sys.getdefaultencoding(), str))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
182
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
183 # 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
184 # 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
185
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
186 def test_setcheckinterval(self):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
187 self.assertRaises(TypeError, sys.setcheckinterval)
29765
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
188 orig = sys.getcheckinterval()
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
189 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
190 sys.setcheckinterval(n)
e9e5832d706f New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
191 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
192
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
193 def test_recursionlimit(self):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
194 self.assertRaises(TypeError, sys.getrecursionlimit, 42)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
195 oldlimit = sys.getrecursionlimit()
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
196 self.assertRaises(TypeError, sys.setrecursionlimit)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
197 self.assertRaises(ValueError, sys.setrecursionlimit, -42)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
198 sys.setrecursionlimit(10000)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
199 self.assertEqual(sys.getrecursionlimit(), 10000)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27934
diff changeset
200 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
201
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
202 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
203 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
204 v = sys.getwindowsversion()
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
205 self.assert_(isinstance(v, tuple))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
206 self.assertEqual(len(v), 5)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
207 self.assert_(isinstance(v[0], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
208 self.assert_(isinstance(v[1], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
209 self.assert_(isinstance(v[2], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
210 self.assert_(isinstance(v[3], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
211 self.assert_(isinstance(v[4], str))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
212
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
213 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
214 if hasattr(sys, "setdlopenflags"):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
215 self.assert_(hasattr(sys, "getdlopenflags"))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
216 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
217 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
218 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
219 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
220 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
221 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
222
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
223 def test_refcount(self):
52900
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47957
diff changeset
224 # n here must be a global in order for this test to pass while
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47957
diff changeset
225 # tracing with a python function. Tracing calls PyFrame_FastToLocals
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47957
diff changeset
226 # which will add a copy of any locals to the frame object, causing
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47957
diff changeset
227 # the reference count to increase by 2 instead of 1.
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47957
diff changeset
228 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
229 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
230 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
231 n = None
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
232 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
233 del n
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
234 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
235 if hasattr(sys, "gettotalrefcount"):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
236 self.assert_(isinstance(sys.gettotalrefcount(), int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
237
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
238 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
239 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
240 self.assertRaises(ValueError, sys._getframe, 2000000000)
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
241 self.assert_(
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 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
243 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
244 )
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
245
39056
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
246 # 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
247 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
248 have_threads = True
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
249 try:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
250 import thread
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
251 except ImportError:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
252 have_threads = False
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
253
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
254 if have_threads:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
255 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
256 else:
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
257 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
258
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
259 # Test sys._current_frames() in a WITH_THREADS build.
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
260 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
261 import threading, thread
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
262 import traceback
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
263
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
264 # 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
265 # 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
266 # returned make sense.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
267 entered_g = threading.Event()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
268 leave_g = threading.Event()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
269 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
270
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
271 def f123():
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
272 g456()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
273
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
274 def g456():
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
275 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
276 entered_g.set()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
277 leave_g.wait()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
278
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
279 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
280 t.start()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
281 entered_g.wait()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
282
39169
99aa84224f18 current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents: 39118
diff changeset
283 # 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
284 # 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
285 # 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
286 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
287 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
288
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
289 d = sys._current_frames()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
290
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
291 main_id = thread.get_ident()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
292 self.assert_(main_id in d)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
293 self.assert_(thread_id in d)
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 # 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
296 frame = d.pop(main_id)
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
297 self.assert_(frame is sys._getframe())
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 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
300 # 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
301 # 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
302 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
303 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
304 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
305 if funcname == "f123":
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
306 break
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
307 else:
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
308 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
309
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
310 self.assertEqual(sourceline, "g456()")
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
311
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
312 # 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
313 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
314 self.assertEqual(funcname, "g456")
39169
99aa84224f18 current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents: 39118
diff changeset
315 self.assert_(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
316
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
317 # Reap the spawned thread.
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
318 leave_g.set()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
319 t.join()
b6fd499e4343 After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents: 37424
diff changeset
320
39118
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
321 # 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
322 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
323 # 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
324 # "thread id" 0.
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
325 d = sys._current_frames()
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
326 self.assertEqual(len(d), 1)
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
327 self.assert_(0 in d)
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
328 self.assert_(d[0] is sys._getframe())
85b23965ec4c SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents: 39056
diff changeset
329
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
330 def test_attributes(self):
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
331 self.assert_(isinstance(sys.api_version, int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
332 self.assert_(isinstance(sys.argv, list))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
333 self.assert_(sys.byteorder in ("little", "big"))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
334 self.assert_(isinstance(sys.builtin_module_names, tuple))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
335 self.assert_(isinstance(sys.copyright, basestring))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
336 self.assert_(isinstance(sys.exec_prefix, basestring))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
337 self.assert_(isinstance(sys.executable, basestring))
44085
c218d1ac916e Feature #1534
Christian Heimes <christian@cheimes.de>
parents: 39169
diff changeset
338 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
339 self.assertEqual(sys.float_info.radix, 2)
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
340 self.assert_(isinstance(sys.hexversion, int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
341 self.assert_(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
342 if test.test_support.have_unicode:
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
343 self.assert_(isinstance(sys.maxunicode, int))
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
344 self.assert_(isinstance(sys.platform, basestring))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
345 self.assert_(isinstance(sys.prefix, basestring))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
346 self.assert_(isinstance(sys.version, basestring))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
347 vi = sys.version_info
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
348 self.assert_(isinstance(vi, tuple))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
349 self.assertEqual(len(vi), 5)
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
350 self.assert_(isinstance(vi[0], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
351 self.assert_(isinstance(vi[1], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
352 self.assert_(isinstance(vi[2], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
353 self.assert_(vi[3] in ("alpha", "beta", "candidate", "final"))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
354 self.assert_(isinstance(vi[4], int))
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
355
37424
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
356 def test_43581(self):
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
357 # 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
358 # the test runs under regrtest.
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
359 self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding)
3eea65bb896e Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents: 35309
diff changeset
360
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
361 def test_sys_flags(self):
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
362 self.failUnless(sys.flags)
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
363 attrs = ("debug", "py3k_warning", "division_warning", "division_new",
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
364 "inspect", "interactive", "optimize", "dont_write_bytecode",
44678
5dfded9ad8a0 Typo fixes
Andrew M. Kuchling <amk@amk.ca>
parents: 44670
diff changeset
365 "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
366 "unicode", "bytes_warning")
44669
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
367 for attr in attrs:
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
368 self.assert_(hasattr(sys.flags, attr), attr)
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
369 self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
370 self.assert_(repr(sys.flags))
9b6d348cec87 Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents: 44085
diff changeset
371
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
372 def test_clear_type_cache(self):
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
373 sys._clear_type_cache()
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44678
diff changeset
374
47413
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
375 def test_ioencoding(self):
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
376 import subprocess,os
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
377 env = dict(os.environ)
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
378
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
379 # 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
380 # not representable in ASCII.
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
381
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
382 env["PYTHONIOENCODING"] = "cp424"
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
383 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
384 stdout = subprocess.PIPE, env=env)
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
385 out = p.stdout.read().strip()
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
386 self.assertEqual(out, unichr(0xa2).encode("cp424"))
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
387
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
388 env["PYTHONIOENCODING"] = "ascii:replace"
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
389 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
390 stdout = subprocess.PIPE, env=env)
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
391 out = p.stdout.read().strip()
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
392 self.assertEqual(out, '?')
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
393
58627
75bb2f1691c0 Merged revisions 77892 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 52900
diff changeset
394 def test_call_tracing(self):
75bb2f1691c0 Merged revisions 77892 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 52900
diff changeset
395 self.assertEqual(sys.call_tracing(str, (2,)), "2")
75bb2f1691c0 Merged revisions 77892 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 52900
diff changeset
396 self.assertRaises(TypeError, sys.call_tracing, str, 2)
75bb2f1691c0 Merged revisions 77892 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 52900
diff changeset
397
47413
1062b7dec95b New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents: 46759
diff changeset
398
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
399 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
400
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
401 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
402 TPFLAGS_HEAPTYPE = 1L<<9
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
403
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
404 def setUp(self):
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
405 self.c = len(struct.pack('c', ' '))
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
406 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
407 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
408 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
409 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
410 # 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
411 # 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
412 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
413 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
414 if hasattr(sys, "gettotalrefcount"):
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
415 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
416 self.vheader += '2P'
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
417 import _testcapi
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
418 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
419 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
420
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
421 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
422 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
423 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
424
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
425 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
426 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
427 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
428 ((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
429 size += self.gc_headsize
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
430 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
431 % (type(o), result, size)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
432 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
433
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
434 def calcsize(self, fmt):
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
435 """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
436 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
437
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
438 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
439 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
440 """
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
441 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
442
47925
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
443 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
444 # 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
445 h = self.header
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
446 size = self.calcsize
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
447 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
448 # 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
449 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
450 # but lists are
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
451 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
452
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
453 def test_default(self):
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
454 h = self.header
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
455 size = self.calcsize
fd054e73e2f2 Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47922
diff changeset
456 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
457
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
458 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
459 # 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
460 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
461 vh = self.vheader
47824
380aa2d161d3 Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47822
diff changeset
462 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
463 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
464 # bool
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
465 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
466 # buffer
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
467 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
468 # 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
469 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
470 # bytearray
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
471 samples = ['', 'u'*100000]
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
472 for sample in samples:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
473 x = bytearray(sample)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
474 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
475 # bytearray_iterator
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
476 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
477 # cell
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
478 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
479 x = 42
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
480 def inner():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
481 return x
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
482 return inner
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
483 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
484 # 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
485 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
486 def method():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
487 pass
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
488 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
489 # 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
490 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
491 # 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
492 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
493 # complex
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
494 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
495 # code
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
496 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
497 # BaseException
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
498 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
499 # UnicodeEncodeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
500 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
501 # UnicodeDecodeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
502 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
503 # UnicodeTranslateError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
504 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
505 # 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
506 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
507 # 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
508 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
509 # 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
510 import datetime
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
511 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
512 # 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
513 import __builtin__
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
514 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
515 # 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
516 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
517 # dictproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
518 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
519 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
520 # 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
521 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
522 # dict
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
523 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
524 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
525 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
526 # dictionary-keyiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
527 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
528 # dictionary-valueiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
529 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
530 # dictionary-itemiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
531 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
532 # ellipses
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
533 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
534 # EncodingMap
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
535 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
536 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
537 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
538 # enumerate
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
539 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
540 # file
58714
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58627
diff changeset
541 check(self.file, size(h + '4P2i4P3i3P3i'))
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
542 # float
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
543 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
544 # sys.floatinfo
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
545 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
546 # frame
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
547 import inspect
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
548 CO_MAXBLOCKS = 20
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
549 x = inspect.currentframe()
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
550 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
551 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
552 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
553 ncells + nfrees - 1
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
554 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
555 # function
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
556 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
557 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
558 class c():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
559 @staticmethod
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
560 def foo():
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
561 pass
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
562 @classmethod
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
563 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
564 pass
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
565 # staticmethod
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
566 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
567 # classmethod
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
568 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
569 # generator
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
570 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
571 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
572 # integer
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
573 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
574 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
575 # iterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
576 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
577 # callable-iterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
578 import re
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
579 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
580 # list
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
581 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
582 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
583 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
584 # sortwrapper (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
585 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
586 # cmpwrapper (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
587 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
588 # listiterator (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
589 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
590 # listreverseiterator (list)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
591 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
592 # long
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
593 check(0L, size(vh + 'H') - self.H)
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
594 check(1L, size(vh + 'H'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
595 check(-1L, size(vh + 'H'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
596 check(32768L, size(vh + 'H') + self.H)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
597 check(32768L*32768L-1, size(vh + 'H') + self.H)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
598 check(32768L*32768L, size(vh + 'H') + 2*self.H)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
599 # module
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
600 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
601 # None
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
602 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
603 # object
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
604 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
605 # property (descriptor object)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
606 class C(object):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
607 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
608 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
609 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
610 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
611 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
612 # PyCObject
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
613 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
614 # rangeiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
615 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
616 # reverse
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
617 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
618 # set
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
619 # frozenset
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
620 PySet_MINSIZE = 8
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
621 samples = [[], range(10), range(50)]
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
622 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
623 for sample in samples:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
624 minused = len(sample)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
625 if minused == 0: tmp = 1
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
626 # 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
627 # 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
628 minused = minused*2
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
629 newsize = PySet_MINSIZE
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
630 while newsize <= minused:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
631 newsize = newsize << 1
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
632 if newsize <= 8:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
633 check(set(sample), s)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
634 check(frozenset(sample), s)
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
635 else:
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
636 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
637 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
638 # setiterator
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
639 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
640 # slice
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
641 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
642 # str
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
643 check('', size(vh + 'lic'))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
644 check('abc', size(vh + 'lic') + 3*self.c)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
645 # super
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
646 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
647 # tuple
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
648 check((), size(vh))
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
649 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
650 # tupleiterator
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
651 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
652 # type
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
653 # (PyTypeObject + PyNumberMethods + PyMappingMethods +
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
654 # PySequenceMethods + PyBufferProcs)
47957
2cc4e1c475f8 Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47925
diff changeset
655 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
656 class newstyleclass(object):
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
657 pass
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
658 check(newstyleclass, s)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
659 # builtin type
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
660 check(int, s)
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
661 # NotImplementedType
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
662 import types
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
663 check(types.NotImplementedType, s)
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
664 # unicode
47743
b8c9a3543381 Issue 3048: Fixed sys.sizeof test fails with wide unicode.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47682
diff changeset
665 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
666 samples = [u'', u'1'*100]
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
667 # 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
668 # has been cached
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47423
diff changeset
669 for s in samples:
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47918
diff changeset
670 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
671 # weakref
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
672 import weakref
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
673 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
674 # weakproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
675 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
676 # weakcallableproxy
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
677 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
678 # xrange
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
679 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
680 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
681
47918
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
682 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
683 # 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
684 h = self.header
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
685 vh = self.vheader
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
686 size = self.calcsize
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
687 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
688 # _ast.AST
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
689 import _ast
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
690 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
691 # imp.NullImporter
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
692 import imp
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
693 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
694 try:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
695 raise TypeError
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
696 except TypeError:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
697 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
698 # traceback
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
699 if tb != None:
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
700 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
701 # symtable entry
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
702 # XXX
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
703 # sys.flags
68c7964d2534 Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47900
diff changeset
704 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
705
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
706
27660
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
707 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
708 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
709
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47413
diff changeset
710 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
711
6a1394660270 Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
712 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
713 test_main()