Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_sys.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 6d31b60372c4 |
| children | 614c84670c63 |
| 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 |
|
58817
70e115c34f97
Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents:
58713
diff
changeset
|
3 import sys, os, cStringIO |
|
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 |
|
59663
5b888f590fe2
#7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59510
diff
changeset
|
82 with test.test_support.check_py3k_warnings(): |
|
5b888f590fe2
#7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59510
diff
changeset
|
83 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
|
84 |
|
28120
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
85 typ, value, traceback = sys.exc_info() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
86 self.assertTrue(typ is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
87 self.assertTrue(value is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
88 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
|
89 |
|
869695cba2cd
- New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents:
27963
diff
changeset
|
90 def clear(): |
|
28120
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
91 try: |
|
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
92 raise ValueError, 42 |
|
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
93 except ValueError, exc: |
|
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
94 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
|
95 |
|
869695cba2cd
- New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents:
27963
diff
changeset
|
96 # 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
|
97 clear() |
|
869695cba2cd
- New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents:
27963
diff
changeset
|
98 |
|
869695cba2cd
- New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents:
27963
diff
changeset
|
99 # 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
|
100 # 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
|
101 try: |
|
28120
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
102 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
|
103 except ValueError, exc: |
|
28120
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
104 typ1, value1, traceback1 = sys.exc_info() |
|
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
105 clear() |
|
d24564e7e999
Reindent the new code properly.
Guido van Rossum <guido@python.org>
parents:
28119
diff
changeset
|
106 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
|
107 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
108 self.assertTrue(typ1 is typ2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
109 self.assertTrue(value1 is exc) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
110 self.assertTrue(value1 is value2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
111 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
|
112 |
|
869695cba2cd
- New function sys.exc_clear() clears the current exception. This is
Guido van Rossum <guido@python.org>
parents:
27963
diff
changeset
|
113 # 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
|
114 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
|
115 |
|
27660
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
116 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
|
117 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
|
118 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
119 # 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
|
120 try: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 except: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
125 self.fail("wrong exception") |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
126 else: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
127 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
|
128 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
129 # 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
|
130 # 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
|
131 try: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 except: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
136 self.fail("wrong exception") |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
137 else: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
138 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
|
139 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
140 # 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
|
141 try: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 except: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
146 self.fail("wrong exception") |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
147 else: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
148 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
|
149 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
150 # 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
|
151 try: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 except: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
156 self.fail("wrong exception") |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
157 else: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
158 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
|
159 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
160 # 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
|
161 try: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 except: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
166 self.fail("wrong exception") |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
167 else: |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
168 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
|
169 |
|
34589
a899d690f264
Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents:
29765
diff
changeset
|
170 # 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
|
171 import subprocess |
|
a899d690f264
Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents:
29765
diff
changeset
|
172 # both unnormalized... |
|
a899d690f264
Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents:
29765
diff
changeset
|
173 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
|
174 "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
|
175 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
|
176 # ... and normalized |
|
a899d690f264
Test that SystemExits are handled properly by the exit machinery. I
Michael W. Hudson <mwh@python.net>
parents:
29765
diff
changeset
|
177 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
|
178 "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
|
179 self.assertEqual(rc, 47) |
|
34591
1ae2c3b66e57
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34589
diff
changeset
|
180 |
|
61920
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
181 def check_exit_message(code, expected, env=None): |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
182 process = subprocess.Popen([sys.executable, "-c", code], |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
183 stderr=subprocess.PIPE, env=env) |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
184 stdout, stderr = process.communicate() |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
185 self.assertEqual(process.returncode, 1) |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
186 self.assertTrue(stderr.startswith(expected), |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
187 "%s doesn't start with %s" % (repr(stderr), repr(expected))) |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
188 |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
189 # test that stderr buffer if flushed before the exit message is written |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
190 # into stderr |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
191 check_exit_message( |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
192 r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
193 b"unflushed,message") |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
194 |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
195 # test that the unicode message is encoded to the stderr encoding |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
196 env = os.environ.copy() |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
197 env['PYTHONIOENCODING'] = 'latin-1' |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
198 check_exit_message( |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
199 r'import sys; sys.exit(u"h\xe9")', |
|
bd5f5df7ed78
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60112
diff
changeset
|
200 b"h\xe9", env=env) |
|
34591
1ae2c3b66e57
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34589
diff
changeset
|
201 |
|
27660
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
202 def test_getdefaultencoding(self): |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
203 if 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
|
204 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
|
205 # 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
|
206 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
|
207 |
|
63267
6d31b60372c4
Redo r83142 merge manually. QOTD: svnmerge isn't really your best friend
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63265
diff
changeset
|
208 # testing sys.settrace() is done in test_sys_settrace.py |
|
6d31b60372c4
Redo r83142 merge manually. QOTD: svnmerge isn't really your best friend
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63265
diff
changeset
|
209 # testing sys.setprofile() is done in test_sys_setprofile.py |
|
27660
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
210 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
211 def test_setcheckinterval(self): |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
212 self.assertRaises(TypeError, sys.setcheckinterval) |
|
29765
e9e5832d706f
New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents:
28917
diff
changeset
|
213 orig = sys.getcheckinterval() |
|
e9e5832d706f
New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents:
28917
diff
changeset
|
214 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
|
215 sys.setcheckinterval(n) |
|
e9e5832d706f
New function sys.getcheckinterval(), to complement setcheckinterval().
Tim Peters <tim.peters@gmail.com>
parents:
28917
diff
changeset
|
216 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
|
217 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
218 def test_recursionlimit(self): |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
219 self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
220 oldlimit = sys.getrecursionlimit() |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
221 self.assertRaises(TypeError, sys.setrecursionlimit) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
222 self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
223 sys.setrecursionlimit(10000) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
224 self.assertEqual(sys.getrecursionlimit(), 10000) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27934
diff
changeset
|
225 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
|
226 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
227 def test_getwindowsversion(self): |
|
58517
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
228 # 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
|
229 test.test_support.get_attribute(sys, "getwindowsversion") |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
230 v = sys.getwindowsversion() |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
231 self.assertEqual(len(v), 5) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
232 self.assertIsInstance(v[0], int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
233 self.assertIsInstance(v[1], int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
234 self.assertIsInstance(v[2], int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
235 self.assertIsInstance(v[3], int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
236 self.assertIsInstance(v[4], str) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
237 self.assertRaises(IndexError, operator.getitem, v, 5) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
238 self.assertIsInstance(v.major, int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
239 self.assertIsInstance(v.minor, int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
240 self.assertIsInstance(v.build, int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
241 self.assertIsInstance(v.platform, int) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
242 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
|
243 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
|
244 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
|
245 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
|
246 self.assertIsInstance(v.product_type, int) |
|
58517
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
247 self.assertEqual(v[0], v.major) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
248 self.assertEqual(v[1], v.minor) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
249 self.assertEqual(v[2], v.build) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
250 self.assertEqual(v[3], v.platform) |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
251 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
|
252 |
|
58517
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
253 # 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
|
254 # still has 5 elements |
|
6726aeb867b9
Switch to test_support.get_attribute.
Eric Smith <eric@trueblade.com>
parents:
58513
diff
changeset
|
255 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
|
256 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
257 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
|
258 if hasattr(sys, "setdlopenflags"): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
267 def test_refcount(self): |
|
52629
1ac1193a61d2
Issue #5635: Fix running test_sys with tracing enabled.
Georg Brandl <georg@python.org>
parents:
52233
diff
changeset
|
268 # 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
|
269 # 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
|
270 # 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
|
271 # 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
|
272 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
|
273 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
|
274 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
|
275 n = None |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
276 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
|
277 del n |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
278 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
|
279 if hasattr(sys, "gettotalrefcount"): |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
280 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
|
281 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
282 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
|
283 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
|
284 self.assertRaises(ValueError, sys._getframe, 2000000000) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
285 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
|
286 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
|
287 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
|
288 ) |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
289 |
|
39056
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
290 # 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
|
291 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
|
292 have_threads = True |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
293 try: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
294 import thread |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
295 except ImportError: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
296 have_threads = False |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
297 |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
298 if have_threads: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
299 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
|
300 else: |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
301 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
|
302 |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
303 # 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
|
304 @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
|
305 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
|
306 import threading, thread |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
307 import traceback |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
308 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
309 # 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
|
310 # 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
|
311 # returned make sense. |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
312 entered_g = threading.Event() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
313 leave_g = threading.Event() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
314 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
|
315 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
316 def f123(): |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
317 g456() |
|
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 def g456(): |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
320 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
|
321 entered_g.set() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
322 leave_g.wait() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
323 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
324 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
|
325 t.start() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
326 entered_g.wait() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
327 |
|
39169
99aa84224f18
current_frames_with_threads(): There's actually no way
Tim Peters <tim.peters@gmail.com>
parents:
39118
diff
changeset
|
328 # 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
|
329 # 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
|
330 # 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
|
331 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
|
332 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
|
333 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
334 d = sys._current_frames() |
|
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 main_id = thread.get_ident() |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
337 self.assertIn(main_id, d) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
338 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
|
339 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
340 # 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
|
341 frame = d.pop(main_id) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
342 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
|
343 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
344 # 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
|
345 # 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
|
346 # 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
|
347 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
|
348 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
|
349 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
|
350 if funcname == "f123": |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
351 break |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
352 else: |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
353 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
|
354 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
355 self.assertEqual(sourceline, "g456()") |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
356 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
357 # 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
|
358 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
|
359 self.assertEqual(funcname, "g456") |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
360 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
|
361 |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
362 # Reap the spawned thread. |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
363 leave_g.set() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
364 t.join() |
|
b6fd499e4343
After approval from Anthony, merge the tim-current_frames
Tim Peters <tim.peters@gmail.com>
parents:
37424
diff
changeset
|
365 |
|
39118
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
366 # 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
|
367 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
|
368 # 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
|
369 # "thread id" 0. |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
370 d = sys._current_frames() |
|
85b23965ec4c
SF bug 1524317: configure --without-threads fails to build
Tim Peters <tim.peters@gmail.com>
parents:
39056
diff
changeset
|
371 self.assertEqual(len(d), 1) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
372 self.assertIn(0, d) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
373 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
|
374 |
|
27660
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
375 def test_attributes(self): |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
376 self.assertIsInstance(sys.api_version, int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
377 self.assertIsInstance(sys.argv, list) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
378 self.assertIn(sys.byteorder, ("little", "big")) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
379 self.assertIsInstance(sys.builtin_module_names, tuple) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
380 self.assertIsInstance(sys.copyright, basestring) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
381 self.assertIsInstance(sys.exec_prefix, basestring) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
382 self.assertIsInstance(sys.executable, basestring) |
| 44085 | 383 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
|
384 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
|
385 self.assertEqual(len(sys.long_info), 2) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
386 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
|
387 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
|
388 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
|
389 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
|
390 self.assertIsInstance(sys.hexversion, int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
391 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
|
392 if test.test_support.have_unicode: |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
393 self.assertIsInstance(sys.maxunicode, int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
394 self.assertIsInstance(sys.platform, basestring) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
395 self.assertIsInstance(sys.prefix, basestring) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
396 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
|
397 vi = sys.version_info |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
398 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
|
399 self.assertEqual(len(vi), 5) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
400 self.assertIsInstance(vi[0], int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
401 self.assertIsInstance(vi[1], int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
402 self.assertIsInstance(vi[2], int) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
403 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
|
404 self.assertIsInstance(vi[4], int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
405 self.assertIsInstance(vi.major, int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
406 self.assertIsInstance(vi.minor, int) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
407 self.assertIsInstance(vi.micro, int) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
408 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 self.assertEqual(vi[4], vi.serial) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
415 self.assertTrue(vi > (1,0,0)) |
|
56715
aacee9749d15
Issue #7117 (backport py3k float repr) continued:
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
416 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
|
417 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
|
418 |
|
37424
3eea65bb896e
Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents:
35309
diff
changeset
|
419 def test_43581(self): |
|
3eea65bb896e
Add test case for #43581.
Martin v. Löwis <martin@v.loewis.de>
parents:
35309
diff
changeset
|
420 # 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
|
421 # the test runs under regrtest. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
422 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
|
423 |
|
44669
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
424 def test_sys_flags(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
425 self.assertTrue(sys.flags) |
|
44669
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
426 attrs = ("debug", "py3k_warning", "division_warning", "division_new", |
|
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
427 "inspect", "interactive", "optimize", "dont_write_bytecode", |
| 44678 | 428 "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
|
429 "unicode", "bytes_warning") |
|
44669
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
430 for attr in attrs: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52739
diff
changeset
|
431 self.assertTrue(hasattr(sys.flags, attr), attr) |
|
44669
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
432 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
|
433 self.assertTrue(repr(sys.flags)) |
|
44669
9b6d348cec87
Applied patch #1816: sys.flags patch
Christian Heimes <christian@cheimes.de>
parents:
44085
diff
changeset
|
434 |
| 45143 | 435 def test_clear_type_cache(self): |
| 436 sys._clear_type_cache() | |
| 437 | |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
438 def test_ioencoding(self): |
|
58817
70e115c34f97
Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents:
58713
diff
changeset
|
439 import subprocess |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
440 env = dict(os.environ) |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
441 |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
442 # 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
|
443 # not representable in ASCII. |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
444 |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
445 env["PYTHONIOENCODING"] = "cp424" |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
446 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
|
447 stdout = subprocess.PIPE, env=env) |
|
56873
b31fcbab0520
Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56715
diff
changeset
|
448 out = p.communicate()[0].strip() |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
449 self.assertEqual(out, unichr(0xa2).encode("cp424")) |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
450 |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
451 env["PYTHONIOENCODING"] = "ascii:replace" |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
452 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
|
453 stdout = subprocess.PIPE, env=env) |
|
56873
b31fcbab0520
Fix transient refleak in test_sys.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56715
diff
changeset
|
454 out = p.communicate()[0].strip() |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
455 self.assertEqual(out, '?') |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
456 |
|
58626
24665a9d881a
Issue #7819: Check sys.call_tracing() arguments types.
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58526
diff
changeset
|
457 def test_call_tracing(self): |
|
24665a9d881a
Issue #7819: Check sys.call_tracing() arguments types.
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58526
diff
changeset
|
458 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
|
459 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
|
460 |
|
59508
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
461 def test_executable(self): |
|
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
462 # Issue #7774: Ensure that sys.executable is an empty string if argv[0] |
|
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
463 # has been set to an non existent program name and Python is unable to |
|
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
464 # retrieve the real program name |
|
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
465 import subprocess |
|
59510
b8ede0fee61a
Another fix to test_executable() of test_sys: set the current working to avoid
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59509
diff
changeset
|
466 # For a normal installation, it should work without 'cwd' |
|
b8ede0fee61a
Another fix to test_executable() of test_sys: set the current working to avoid
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59509
diff
changeset
|
467 # argument. For test runs in the build directory, see #7774. |
|
b8ede0fee61a
Another fix to test_executable() of test_sys: set the current working to avoid
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59509
diff
changeset
|
468 python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
|
59508
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
469 p = subprocess.Popen( |
|
59509
e5367d67a7f8
Fix test_executable introduce in previous commit (r78835): Windows is able to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59508
diff
changeset
|
470 ["nonexistent", "-c", 'import sys; print repr(sys.executable)'], |
|
59510
b8ede0fee61a
Another fix to test_executable() of test_sys: set the current working to avoid
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59509
diff
changeset
|
471 executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) |
|
59508
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
472 executable = p.communicate()[0].strip() |
|
8b246e8560db
Issue #7774: Set sys.executable to an empty string if argv[0] has been
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
473 p.wait() |
|
59509
e5367d67a7f8
Fix test_executable introduce in previous commit (r78835): Windows is able to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59508
diff
changeset
|
474 self.assertIn(executable, ["''", repr(sys.executable)]) |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
46759
diff
changeset
|
475 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
476 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
|
477 |
|
47925
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
478 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
|
479 TPFLAGS_HEAPTYPE = 1L<<9 |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
480 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
481 def setUp(self): |
|
47824
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
482 self.c = len(struct.pack('c', ' ')) |
|
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
483 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
|
484 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
|
485 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
|
486 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
|
487 # 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
|
488 # 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
|
489 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
|
490 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
|
491 if hasattr(sys, "gettotalrefcount"): |
|
47824
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
492 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
|
493 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
|
494 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
|
495 import _testcapi |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
496 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
|
497 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
|
498 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
499 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
|
500 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
|
501 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
|
502 |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
503 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
|
504 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
|
505 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
|
506 ((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
|
507 size += self.gc_headsize |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
508 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
|
509 % (type(o), result, size) |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
510 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
|
511 |
|
47824
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
512 def calcsize(self, fmt): |
|
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
513 """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
|
514 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
|
515 |
|
47824
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
516 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
|
517 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
|
518 """ |
|
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
519 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
|
520 |
|
47925
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
521 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
|
522 # 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
|
523 h = self.header |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
524 size = self.calcsize |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
525 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
|
526 # 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
|
527 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
|
528 # but lists are |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
529 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
|
530 |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
531 def test_default(self): |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
532 h = self.header |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
533 size = self.calcsize |
|
fd054e73e2f2
Added garbage collector overhead and optional default return value to
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47922
diff
changeset
|
534 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
|
535 |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
536 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
|
537 # 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
|
538 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
|
539 vh = self.vheader |
|
47824
380aa2d161d3
Issue 3147: Fixed SizeofTest failure for LLP64 systems.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47822
diff
changeset
|
540 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
|
541 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
|
542 # bool |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
543 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
|
544 # buffer |
|
59663
5b888f590fe2
#7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59510
diff
changeset
|
545 with test.test_support.check_py3k_warnings(): |
|
5b888f590fe2
#7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59510
diff
changeset
|
546 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
|
547 # 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
|
548 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
|
549 # bytearray |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
550 samples = ['', 'u'*100000] |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
551 for sample in samples: |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
552 x = bytearray(sample) |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
553 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
|
554 # bytearray_iterator |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
555 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
|
556 # cell |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
557 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
|
558 x = 42 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
559 def inner(): |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
560 return x |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
561 return inner |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
562 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
|
563 # 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
|
564 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
|
565 def method(): |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
566 pass |
|
60112
7b8ba019cd15
Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents:
59998
diff
changeset
|
567 check(class_oldstyle, size(h + '7P')) |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
568 # 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
|
569 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
|
570 # 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
|
571 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
|
572 # complex |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
573 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
|
574 # code |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
59663
diff
changeset
|
575 check(get_cell().func_code, size(h + '4i8Pi3P')) |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
576 # BaseException |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
577 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
|
578 # UnicodeEncodeError |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
579 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
|
580 # UnicodeDecodeError |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
581 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
|
582 # UnicodeTranslateError |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
583 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
|
584 # 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
|
585 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
|
586 # 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
|
587 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
588 # 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
|
589 import datetime |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
590 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
|
591 # 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
|
592 import __builtin__ |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
593 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
|
594 # 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
|
595 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
|
596 # dictproxy |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
597 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
|
598 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
|
599 # 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
|
600 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
|
601 # dict |
|
47957
2cc4e1c475f8
Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47925
diff
changeset
|
602 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
|
603 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
|
604 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
|
605 # dictionary-keyiterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
606 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
|
607 # dictionary-valueiterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
608 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
|
609 # dictionary-itemiterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
610 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
|
611 # ellipses |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
612 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
|
613 # EncodingMap |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
614 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
|
615 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
|
616 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
|
617 # enumerate |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
618 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
|
619 # file |
|
58713
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
58626
diff
changeset
|
620 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
|
621 # float |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
622 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
|
623 # sys.floatinfo |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
624 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
|
625 # frame |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
626 import inspect |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
627 CO_MAXBLOCKS = 20 |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
628 x = inspect.currentframe() |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
629 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
|
630 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
|
631 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
|
632 ncells + nfrees - 1 |
|
47957
2cc4e1c475f8
Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47925
diff
changeset
|
633 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
|
634 # function |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
635 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
|
636 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
|
637 class c(): |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
638 @staticmethod |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
639 def foo(): |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
640 pass |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
641 @classmethod |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
642 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
|
643 pass |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
644 # staticmethod |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
645 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
|
646 # classmethod |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
647 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
|
648 # generator |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
649 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
|
650 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
|
651 # integer |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
652 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
|
653 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
|
654 # iterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
655 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
|
656 # callable-iterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
657 import re |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
658 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
|
659 # list |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
660 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
|
661 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
|
662 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
|
663 # sortwrapper (list) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
664 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
665 # cmpwrapper (list) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
666 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
667 # listiterator (list) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
668 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
|
669 # listreverseiterator (list) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
670 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
|
671 # long |
|
51641
b55a3b5d7008
Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents:
51365
diff
changeset
|
672 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
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 # module |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
680 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
|
681 # None |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
682 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
|
683 # object |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
684 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
|
685 # property (descriptor object) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
686 class C(object): |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
687 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
|
688 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
|
689 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
|
690 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
|
691 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
|
692 # PyCObject |
|
59998
d11fbc796499
Backported PyCapsule from 3.1, and converted most uses of
Larry Hastings <larry@hastings.org>
parents:
59694
diff
changeset
|
693 # PyCapsule |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
694 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
695 # rangeiterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
696 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
|
697 # reverse |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
698 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
|
699 # set |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
700 # frozenset |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
701 PySet_MINSIZE = 8 |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
702 samples = [[], range(10), range(50)] |
|
47957
2cc4e1c475f8
Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47925
diff
changeset
|
703 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
|
704 for sample in samples: |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
705 minused = len(sample) |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
706 if minused == 0: tmp = 1 |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
707 # 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
|
708 # 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
|
709 minused = minused*2 |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
710 newsize = PySet_MINSIZE |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
711 while newsize <= minused: |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
712 newsize = newsize << 1 |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
713 if newsize <= 8: |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
714 check(set(sample), s) |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
715 check(frozenset(sample), s) |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
716 else: |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
717 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
|
718 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
|
719 # setiterator |
|
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
720 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
|
721 # slice |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
722 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
|
723 # str |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
47957
diff
changeset
|
724 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
|
725 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
|
726 # super |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
727 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
|
728 # tuple |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
729 check((), size(vh)) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
730 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
|
731 # tupleiterator |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
732 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
|
733 # type |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
734 # (PyTypeObject + PyNumberMethods + PyMappingMethods + |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
735 # PySequenceMethods + PyBufferProcs) |
|
47957
2cc4e1c475f8
Fixed test failure on Win64 machines.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47925
diff
changeset
|
736 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
|
737 class newstyleclass(object): |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
738 pass |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
739 check(newstyleclass, s) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
740 # builtin type |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
741 check(int, s) |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
742 # NotImplementedType |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
743 import types |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
744 check(types.NotImplementedType, s) |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47423
diff
changeset
|
745 # unicode |
|
47743
b8c9a3543381
Issue 3048: Fixed sys.sizeof test fails with wide unicode.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47682
diff
changeset
|
746 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
|
747 samples = [u'', u'1'*100] |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47423
diff
changeset
|
748 # 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
|
749 # has been cached |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47423
diff
changeset
|
750 for s in samples: |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47918
diff
changeset
|
751 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
|
752 # weakref |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
753 import weakref |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
754 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
|
755 # weakproxy |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
756 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
757 # weakcallableproxy |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
758 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
|
759 # xrange |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
760 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
|
761 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
|
762 |
|
47918
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
763 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
|
764 # 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
|
765 h = self.header |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
766 vh = self.vheader |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
767 size = self.calcsize |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
768 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
|
769 # _ast.AST |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
770 import _ast |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
771 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
|
772 # imp.NullImporter |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
773 import imp |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
774 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
|
775 try: |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
776 raise TypeError |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
777 except TypeError: |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
778 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
|
779 # traceback |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
780 if tb != None: |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
781 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
|
782 # symtable entry |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
783 # XXX |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
784 # sys.flags |
|
68c7964d2534
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47900
diff
changeset
|
785 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
|
786 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
787 |
|
27660
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
788 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
|
789 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
|
790 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47413
diff
changeset
|
791 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
|
792 |
|
6a1394660270
Add a new test script that tests various features of the sys
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
793 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
|
794 test_main() |
