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