test_sys.py revision 48006cf1a8b8d1cadb422619c3b51bb17b4e9405
1import unittest, test.support
2import sys, io, os
3import struct
4import subprocess
5import textwrap
6import warnings
7import operator
8import codecs
9
10# count the number of test runs, used to create unique
11# strings to intern in test_intern()
12numruns = 0
13
14try:
15    import threading
16except ImportError:
17    threading = None
18
19class SysModuleTest(unittest.TestCase):
20
21    def setUp(self):
22        self.orig_stdout = sys.stdout
23        self.orig_stderr = sys.stderr
24        self.orig_displayhook = sys.displayhook
25
26    def tearDown(self):
27        sys.stdout = self.orig_stdout
28        sys.stderr = self.orig_stderr
29        sys.displayhook = self.orig_displayhook
30        test.support.reap_children()
31
32    def test_original_displayhook(self):
33        import builtins
34        out = io.StringIO()
35        sys.stdout = out
36
37        dh = sys.__displayhook__
38
39        self.assertRaises(TypeError, dh)
40        if hasattr(builtins, "_"):
41            del builtins._
42
43        dh(None)
44        self.assertEqual(out.getvalue(), "")
45        self.assertTrue(not hasattr(builtins, "_"))
46        dh(42)
47        self.assertEqual(out.getvalue(), "42\n")
48        self.assertEqual(builtins._, 42)
49
50        del sys.stdout
51        self.assertRaises(RuntimeError, dh, 42)
52
53    def test_lost_displayhook(self):
54        del sys.displayhook
55        code = compile("42", "<string>", "single")
56        self.assertRaises(RuntimeError, eval, code)
57
58    def test_custom_displayhook(self):
59        def baddisplayhook(obj):
60            raise ValueError
61        sys.displayhook = baddisplayhook
62        code = compile("42", "<string>", "single")
63        self.assertRaises(ValueError, eval, code)
64
65    def test_original_excepthook(self):
66        err = io.StringIO()
67        sys.stderr = err
68
69        eh = sys.__excepthook__
70
71        self.assertRaises(TypeError, eh)
72        try:
73            raise ValueError(42)
74        except ValueError as exc:
75            eh(*sys.exc_info())
76
77        self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
78
79    def test_excepthook(self):
80        with test.support.captured_output("stderr") as stderr:
81            sys.excepthook(1, '1', 1)
82        self.assertTrue("TypeError: print_exception(): Exception expected for " \
83                         "value, str found" in stderr.getvalue())
84
85    # FIXME: testing the code for a lost or replaced excepthook in
86    # Python/pythonrun.c::PyErr_PrintEx() is tricky.
87
88    def test_exit(self):
89
90        self.assertRaises(TypeError, sys.exit, 42, 42)
91
92        # call without argument
93        try:
94            sys.exit(0)
95        except SystemExit as exc:
96            self.assertEqual(exc.code, 0)
97        except:
98            self.fail("wrong exception")
99        else:
100            self.fail("no exception")
101
102        # call with tuple argument with one entry
103        # entry will be unpacked
104        try:
105            sys.exit(42)
106        except SystemExit as exc:
107            self.assertEqual(exc.code, 42)
108        except:
109            self.fail("wrong exception")
110        else:
111            self.fail("no exception")
112
113        # call with integer argument
114        try:
115            sys.exit((42,))
116        except SystemExit as exc:
117            self.assertEqual(exc.code, 42)
118        except:
119            self.fail("wrong exception")
120        else:
121            self.fail("no exception")
122
123        # call with string argument
124        try:
125            sys.exit("exit")
126        except SystemExit as exc:
127            self.assertEqual(exc.code, "exit")
128        except:
129            self.fail("wrong exception")
130        else:
131            self.fail("no exception")
132
133        # call with tuple argument with two entries
134        try:
135            sys.exit((17, 23))
136        except SystemExit as exc:
137            self.assertEqual(exc.code, (17, 23))
138        except:
139            self.fail("wrong exception")
140        else:
141            self.fail("no exception")
142
143        # test that the exit machinery handles SystemExits properly
144        rc = subprocess.call([sys.executable, "-c",
145                              "raise SystemExit(47)"])
146        self.assertEqual(rc, 47)
147
148        def check_exit_message(code, expected, env=None):
149            process = subprocess.Popen([sys.executable, "-c", code],
150                                       stderr=subprocess.PIPE, env=env)
151            stdout, stderr = process.communicate()
152            self.assertEqual(process.returncode, 1)
153            self.assertTrue(stderr.startswith(expected),
154                "%s doesn't start with %s" % (ascii(stderr), ascii(expected)))
155
156        # test that stderr buffer if flushed before the exit message is written
157        # into stderr
158        check_exit_message(
159            r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
160            b"unflushed,message")
161
162        # test that the exit message is written with backslashreplace error
163        # handler to stderr
164        check_exit_message(
165            r'import sys; sys.exit("surrogates:\uDCFF")',
166            b"surrogates:\\udcff")
167
168        # test that the unicode message is encoded to the stderr encoding
169        # instead of the default encoding (utf8)
170        env = os.environ.copy()
171        env['PYTHONIOENCODING'] = 'latin-1'
172        check_exit_message(
173            r'import sys; sys.exit("h\xe9")',
174            b"h\xe9", env=env)
175
176    def test_getdefaultencoding(self):
177        self.assertRaises(TypeError, sys.getdefaultencoding, 42)
178        # can't check more than the type, as the user might have changed it
179        self.assertIsInstance(sys.getdefaultencoding(), str)
180
181    # testing sys.settrace() is done in test_sys_settrace.py
182    # testing sys.setprofile() is done in test_sys_setprofile.py
183
184    def test_setcheckinterval(self):
185        with warnings.catch_warnings():
186            warnings.simplefilter("ignore")
187            self.assertRaises(TypeError, sys.setcheckinterval)
188            orig = sys.getcheckinterval()
189            for n in 0, 100, 120, orig: # orig last to restore starting state
190                sys.setcheckinterval(n)
191                self.assertEqual(sys.getcheckinterval(), n)
192
193    @unittest.skipUnless(threading, 'Threading required for this test.')
194    def test_switchinterval(self):
195        self.assertRaises(TypeError, sys.setswitchinterval)
196        self.assertRaises(TypeError, sys.setswitchinterval, "a")
197        self.assertRaises(ValueError, sys.setswitchinterval, -1.0)
198        self.assertRaises(ValueError, sys.setswitchinterval, 0.0)
199        orig = sys.getswitchinterval()
200        # sanity check
201        self.assertTrue(orig < 0.5, orig)
202        try:
203            for n in 0.00001, 0.05, 3.0, orig:
204                sys.setswitchinterval(n)
205                self.assertAlmostEqual(sys.getswitchinterval(), n)
206        finally:
207            sys.setswitchinterval(orig)
208
209    def test_recursionlimit(self):
210        self.assertRaises(TypeError, sys.getrecursionlimit, 42)
211        oldlimit = sys.getrecursionlimit()
212        self.assertRaises(TypeError, sys.setrecursionlimit)
213        self.assertRaises(ValueError, sys.setrecursionlimit, -42)
214        sys.setrecursionlimit(10000)
215        self.assertEqual(sys.getrecursionlimit(), 10000)
216        sys.setrecursionlimit(oldlimit)
217
218    @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
219                     'fatal error if run with a trace function')
220    def test_recursionlimit_recovery(self):
221        # NOTE: this test is slightly fragile in that it depends on the current
222        # recursion count when executing the test being low enough so as to
223        # trigger the recursion recovery detection in the _Py_MakeEndRecCheck
224        # macro (see ceval.h).
225        oldlimit = sys.getrecursionlimit()
226        def f():
227            f()
228        try:
229            for i in (50, 1000):
230                # Issue #5392: stack overflow after hitting recursion limit twice
231                sys.setrecursionlimit(i)
232                self.assertRaises(RuntimeError, f)
233                self.assertRaises(RuntimeError, f)
234        finally:
235            sys.setrecursionlimit(oldlimit)
236
237    def test_recursionlimit_fatalerror(self):
238        # A fatal error occurs if a second recursion limit is hit when recovering
239        # from a first one.
240        if os.name == "nt":
241            raise unittest.SkipTest(
242                "under Windows, test would generate a spurious crash dialog")
243        code = textwrap.dedent("""
244            import sys
245
246            def f():
247                try:
248                    f()
249                except RuntimeError:
250                    f()
251
252            sys.setrecursionlimit(%d)
253            f()""")
254        for i in (50, 1000):
255            sub = subprocess.Popen([sys.executable, '-c', code % i],
256                stderr=subprocess.PIPE)
257            err = sub.communicate()[1]
258            self.assertTrue(sub.returncode, sub.returncode)
259            self.assertTrue(
260                b"Fatal Python error: Cannot recover from stack overflow" in err,
261                err)
262
263    def test_getwindowsversion(self):
264        # Raise SkipTest if sys doesn't have getwindowsversion attribute
265        test.support.get_attribute(sys, "getwindowsversion")
266        v = sys.getwindowsversion()
267        self.assertEqual(len(v), 5)
268        self.assertIsInstance(v[0], int)
269        self.assertIsInstance(v[1], int)
270        self.assertIsInstance(v[2], int)
271        self.assertIsInstance(v[3], int)
272        self.assertIsInstance(v[4], str)
273        self.assertRaises(IndexError, operator.getitem, v, 5)
274        self.assertIsInstance(v.major, int)
275        self.assertIsInstance(v.minor, int)
276        self.assertIsInstance(v.build, int)
277        self.assertIsInstance(v.platform, int)
278        self.assertIsInstance(v.service_pack, str)
279        self.assertIsInstance(v.service_pack_minor, int)
280        self.assertIsInstance(v.service_pack_major, int)
281        self.assertIsInstance(v.suite_mask, int)
282        self.assertIsInstance(v.product_type, int)
283        self.assertEqual(v[0], v.major)
284        self.assertEqual(v[1], v.minor)
285        self.assertEqual(v[2], v.build)
286        self.assertEqual(v[3], v.platform)
287        self.assertEqual(v[4], v.service_pack)
288
289        # This is how platform.py calls it. Make sure tuple
290        #  still has 5 elements
291        maj, min, buildno, plat, csd = sys.getwindowsversion()
292
293    def test_call_tracing(self):
294        self.assertRaises(TypeError, sys.call_tracing, type, 2)
295
296    def test_dlopenflags(self):
297        if hasattr(sys, "setdlopenflags"):
298            self.assertTrue(hasattr(sys, "getdlopenflags"))
299            self.assertRaises(TypeError, sys.getdlopenflags, 42)
300            oldflags = sys.getdlopenflags()
301            self.assertRaises(TypeError, sys.setdlopenflags)
302            sys.setdlopenflags(oldflags+1)
303            self.assertEqual(sys.getdlopenflags(), oldflags+1)
304            sys.setdlopenflags(oldflags)
305
306    @test.support.refcount_test
307    def test_refcount(self):
308        # n here must be a global in order for this test to pass while
309        # tracing with a python function.  Tracing calls PyFrame_FastToLocals
310        # which will add a copy of any locals to the frame object, causing
311        # the reference count to increase by 2 instead of 1.
312        global n
313        self.assertRaises(TypeError, sys.getrefcount)
314        c = sys.getrefcount(None)
315        n = None
316        self.assertEqual(sys.getrefcount(None), c+1)
317        del n
318        self.assertEqual(sys.getrefcount(None), c)
319        if hasattr(sys, "gettotalrefcount"):
320            self.assertIsInstance(sys.gettotalrefcount(), int)
321
322    def test_getframe(self):
323        self.assertRaises(TypeError, sys._getframe, 42, 42)
324        self.assertRaises(ValueError, sys._getframe, 2000000000)
325        self.assertTrue(
326            SysModuleTest.test_getframe.__code__ \
327            is sys._getframe().f_code
328        )
329
330    # sys._current_frames() is a CPython-only gimmick.
331    def test_current_frames(self):
332        have_threads = True
333        try:
334            import _thread
335        except ImportError:
336            have_threads = False
337
338        if have_threads:
339            self.current_frames_with_threads()
340        else:
341            self.current_frames_without_threads()
342
343    # Test sys._current_frames() in a WITH_THREADS build.
344    @test.support.reap_threads
345    def current_frames_with_threads(self):
346        import threading, _thread
347        import traceback
348
349        # Spawn a thread that blocks at a known place.  Then the main
350        # thread does sys._current_frames(), and verifies that the frames
351        # returned make sense.
352        entered_g = threading.Event()
353        leave_g = threading.Event()
354        thread_info = []  # the thread's id
355
356        def f123():
357            g456()
358
359        def g456():
360            thread_info.append(_thread.get_ident())
361            entered_g.set()
362            leave_g.wait()
363
364        t = threading.Thread(target=f123)
365        t.start()
366        entered_g.wait()
367
368        # At this point, t has finished its entered_g.set(), although it's
369        # impossible to guess whether it's still on that line or has moved on
370        # to its leave_g.wait().
371        self.assertEqual(len(thread_info), 1)
372        thread_id = thread_info[0]
373
374        d = sys._current_frames()
375
376        main_id = _thread.get_ident()
377        self.assertIn(main_id, d)
378        self.assertIn(thread_id, d)
379
380        # Verify that the captured main-thread frame is _this_ frame.
381        frame = d.pop(main_id)
382        self.assertTrue(frame is sys._getframe())
383
384        # Verify that the captured thread frame is blocked in g456, called
385        # from f123.  This is a litte tricky, since various bits of
386        # threading.py are also in the thread's call stack.
387        frame = d.pop(thread_id)
388        stack = traceback.extract_stack(frame)
389        for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
390            if funcname == "f123":
391                break
392        else:
393            self.fail("didn't find f123() on thread's call stack")
394
395        self.assertEqual(sourceline, "g456()")
396
397        # And the next record must be for g456().
398        filename, lineno, funcname, sourceline = stack[i+1]
399        self.assertEqual(funcname, "g456")
400        self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"])
401
402        # Reap the spawned thread.
403        leave_g.set()
404        t.join()
405
406    # Test sys._current_frames() when thread support doesn't exist.
407    def current_frames_without_threads(self):
408        # Not much happens here:  there is only one thread, with artificial
409        # "thread id" 0.
410        d = sys._current_frames()
411        self.assertEqual(len(d), 1)
412        self.assertIn(0, d)
413        self.assertTrue(d[0] is sys._getframe())
414
415    def test_attributes(self):
416        self.assertIsInstance(sys.api_version, int)
417        self.assertIsInstance(sys.argv, list)
418        self.assertIn(sys.byteorder, ("little", "big"))
419        self.assertIsInstance(sys.builtin_module_names, tuple)
420        self.assertIsInstance(sys.copyright, str)
421        self.assertIsInstance(sys.exec_prefix, str)
422        self.assertIsInstance(sys.executable, str)
423        self.assertEqual(len(sys.float_info), 11)
424        self.assertEqual(sys.float_info.radix, 2)
425        self.assertEqual(len(sys.int_info), 2)
426        self.assertTrue(sys.int_info.bits_per_digit % 5 == 0)
427        self.assertTrue(sys.int_info.sizeof_digit >= 1)
428        self.assertEqual(type(sys.int_info.bits_per_digit), int)
429        self.assertEqual(type(sys.int_info.sizeof_digit), int)
430        self.assertIsInstance(sys.hexversion, int)
431
432        self.assertEqual(len(sys.hash_info), 5)
433        self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
434        # sys.hash_info.modulus should be a prime; we do a quick
435        # probable primality test (doesn't exclude the possibility of
436        # a Carmichael number)
437        for x in range(1, 100):
438            self.assertEqual(
439                pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus),
440                1,
441                "sys.hash_info.modulus {} is a non-prime".format(
442                    sys.hash_info.modulus)
443                )
444        self.assertIsInstance(sys.hash_info.inf, int)
445        self.assertIsInstance(sys.hash_info.nan, int)
446        self.assertIsInstance(sys.hash_info.imag, int)
447
448        self.assertIsInstance(sys.maxsize, int)
449        self.assertIsInstance(sys.maxunicode, int)
450        self.assertIsInstance(sys.platform, str)
451        self.assertIsInstance(sys.prefix, str)
452        self.assertIsInstance(sys.version, str)
453        vi = sys.version_info
454        self.assertIsInstance(vi[:], tuple)
455        self.assertEqual(len(vi), 5)
456        self.assertIsInstance(vi[0], int)
457        self.assertIsInstance(vi[1], int)
458        self.assertIsInstance(vi[2], int)
459        self.assertIn(vi[3], ("alpha", "beta", "candidate", "final"))
460        self.assertIsInstance(vi[4], int)
461        self.assertIsInstance(vi.major, int)
462        self.assertIsInstance(vi.minor, int)
463        self.assertIsInstance(vi.micro, int)
464        self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final"))
465        self.assertIsInstance(vi.serial, int)
466        self.assertEqual(vi[0], vi.major)
467        self.assertEqual(vi[1], vi.minor)
468        self.assertEqual(vi[2], vi.micro)
469        self.assertEqual(vi[3], vi.releaselevel)
470        self.assertEqual(vi[4], vi.serial)
471        self.assertTrue(vi > (1,0,0))
472        self.assertIsInstance(sys.float_repr_style, str)
473        self.assertIn(sys.float_repr_style, ('short', 'legacy'))
474        if not sys.platform.startswith('win'):
475            self.assertIsInstance(sys.abiflags, str)
476
477    @unittest.skipUnless(hasattr(sys, 'thread_info'),
478                         'Threading required for this test.')
479    def test_thread_info(self):
480        info = sys.thread_info
481        self.assertEqual(len(info), 3)
482        self.assertIn(info.name, ('nt', 'os2', 'pthread', 'solaris', None))
483        self.assertIn(info.lock, ('semaphore', 'mutex+cond', None))
484
485    def test_43581(self):
486        # Can't use sys.stdout, as this is a StringIO object when
487        # the test runs under regrtest.
488        self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
489
490    def test_intern(self):
491        global numruns
492        numruns += 1
493        self.assertRaises(TypeError, sys.intern)
494        s = "never interned before" + str(numruns)
495        self.assertTrue(sys.intern(s) is s)
496        s2 = s.swapcase().swapcase()
497        self.assertTrue(sys.intern(s2) is s)
498
499        # Subclasses of string can't be interned, because they
500        # provide too much opportunity for insane things to happen.
501        # We don't want them in the interned dict and if they aren't
502        # actually interned, we don't want to create the appearance
503        # that they are by allowing intern() to succeed.
504        class S(str):
505            def __hash__(self):
506                return 123
507
508        self.assertRaises(TypeError, sys.intern, S("abc"))
509
510    def test_sys_flags(self):
511        self.assertTrue(sys.flags)
512        attrs = ("debug",
513                 "inspect", "interactive", "optimize", "dont_write_bytecode",
514                 "no_user_site", "no_site", "ignore_environment", "verbose",
515                 "bytes_warning", "quiet")
516        for attr in attrs:
517            self.assertTrue(hasattr(sys.flags, attr), attr)
518            self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
519        self.assertTrue(repr(sys.flags))
520        self.assertEqual(len(sys.flags), len(attrs))
521
522    def test_clear_type_cache(self):
523        sys._clear_type_cache()
524
525    def test_ioencoding(self):
526        env = dict(os.environ)
527
528        # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
529        # not representable in ASCII.
530
531        env["PYTHONIOENCODING"] = "cp424"
532        p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
533                             stdout = subprocess.PIPE, env=env)
534        out = p.communicate()[0].strip()
535        self.assertEqual(out, "\xa2\n".encode("cp424"))
536
537        env["PYTHONIOENCODING"] = "ascii:replace"
538        p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
539                             stdout = subprocess.PIPE, env=env)
540        out = p.communicate()[0].strip()
541        self.assertEqual(out, b'?')
542
543    def test_executable(self):
544        # Issue #7774: Ensure that sys.executable is an empty string if argv[0]
545        # has been set to an non existent program name and Python is unable to
546        # retrieve the real program name
547
548        # For a normal installation, it should work without 'cwd'
549        # argument. For test runs in the build directory, see #7774.
550        python_dir = os.path.dirname(os.path.realpath(sys.executable))
551        p = subprocess.Popen(
552            ["nonexistent", "-c",
553             'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'],
554            executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
555        stdout = p.communicate()[0]
556        executable = stdout.strip().decode("ASCII")
557        p.wait()
558        self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))])
559
560    def check_fsencoding(self, fs_encoding, expected=None):
561        self.assertIsNotNone(fs_encoding)
562        codecs.lookup(fs_encoding)
563        if expected:
564            self.assertEqual(fs_encoding, expected)
565
566    def test_getfilesystemencoding(self):
567        fs_encoding = sys.getfilesystemencoding()
568        if sys.platform == 'darwin':
569            expected = 'utf-8'
570        elif sys.platform == 'win32':
571            expected = 'mbcs'
572        else:
573            expected = None
574        self.check_fsencoding(fs_encoding, expected)
575
576
577class SizeofTest(unittest.TestCase):
578
579    TPFLAGS_HAVE_GC = 1<<14
580    TPFLAGS_HEAPTYPE = 1<<9
581
582    def setUp(self):
583        self.c = len(struct.pack('c', b' '))
584        self.H = len(struct.pack('H', 0))
585        self.i = len(struct.pack('i', 0))
586        self.l = len(struct.pack('l', 0))
587        self.P = len(struct.pack('P', 0))
588        # due to missing size_t information from struct, it is assumed that
589        # sizeof(Py_ssize_t) = sizeof(void*)
590        self.header = 'PP'
591        self.vheader = self.header + 'P'
592        if hasattr(sys, "gettotalrefcount"):
593            self.header += '2P'
594            self.vheader += '2P'
595        self.longdigit = sys.int_info.sizeof_digit
596        import _testcapi
597        self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
598        self.file = open(test.support.TESTFN, 'wb')
599
600    def tearDown(self):
601        self.file.close()
602        test.support.unlink(test.support.TESTFN)
603
604    def check_sizeof(self, o, size):
605        result = sys.getsizeof(o)
606        # add GC header size
607        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
608           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
609            size += self.gc_headsize
610        msg = 'wrong size for %s: got %d, expected %d' \
611              % (type(o), result, size)
612        self.assertEqual(result, size, msg)
613
614    def calcsize(self, fmt):
615        """Wrapper around struct.calcsize which enforces the alignment of the
616        end of a structure to the alignment requirement of pointer.
617
618        Note: This wrapper should only be used if a pointer member is included
619        and no member with a size larger than a pointer exists.
620        """
621        return struct.calcsize(fmt + '0P')
622
623    def test_gc_head_size(self):
624        # Check that the gc header size is added to objects tracked by the gc.
625        h = self.header
626        vh = self.vheader
627        size = self.calcsize
628        gc_header_size = self.gc_headsize
629        # bool objects are not gc tracked
630        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
631        # but lists are
632        self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)
633
634    def test_default(self):
635        h = self.header
636        vh = self.vheader
637        size = self.calcsize
638        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
639        self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)
640
641    def test_objecttypes(self):
642        # check all types defined in Objects/
643        h = self.header
644        vh = self.vheader
645        size = self.calcsize
646        check = self.check_sizeof
647        # bool
648        check(True, size(vh) + self.longdigit)
649        # buffer
650        # XXX
651        # builtin_function_or_method
652        check(len, size(h + '3P'))
653        # bytearray
654        samples = [b'', b'u'*100000]
655        for sample in samples:
656            x = bytearray(sample)
657            check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
658        # bytearray_iterator
659        check(iter(bytearray()), size(h + 'PP'))
660        # cell
661        def get_cell():
662            x = 42
663            def inner():
664                return x
665            return inner
666        check(get_cell().__closure__[0], size(h + 'P'))
667        # code
668        check(get_cell().__code__, size(h + '5i8Pi3P'))
669        # complex
670        check(complex(0,1), size(h + '2d'))
671        # method_descriptor (descriptor object)
672        check(str.lower, size(h + '2PP'))
673        # classmethod_descriptor (descriptor object)
674        # XXX
675        # member_descriptor (descriptor object)
676        import datetime
677        check(datetime.timedelta.days, size(h + '2PP'))
678        # getset_descriptor (descriptor object)
679        import collections
680        check(collections.defaultdict.default_factory, size(h + '2PP'))
681        # wrapper_descriptor (descriptor object)
682        check(int.__add__, size(h + '2P2P'))
683        # method-wrapper (descriptor object)
684        check({}.__iter__, size(h + '2P'))
685        # dict
686        check({}, size(h + '3P2P' + 8*'P2P'))
687        longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
688        check(longdict, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
689        # dictionary-keyiterator
690        check({}.keys(), size(h + 'P'))
691        # dictionary-valueiterator
692        check({}.values(), size(h + 'P'))
693        # dictionary-itemiterator
694        check({}.items(), size(h + 'P'))
695        # dictproxy
696        class C(object): pass
697        check(C.__dict__, size(h + 'P'))
698        # BaseException
699        check(BaseException(), size(h + '5P'))
700        # UnicodeEncodeError
701        check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5P 2P2PP'))
702        # UnicodeDecodeError
703        # XXX
704#        check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
705        # UnicodeTranslateError
706        check(UnicodeTranslateError("", 0, 1, ""), size(h + '5P 2P2PP'))
707        # ellipses
708        check(Ellipsis, size(h + ''))
709        # EncodingMap
710        import codecs, encodings.iso8859_3
711        x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
712        check(x, size(h + '32B2iB'))
713        # enumerate
714        check(enumerate([]), size(h + 'l3P'))
715        # reverse
716        check(reversed(''), size(h + 'PP'))
717        # float
718        check(float(0), size(h + 'd'))
719        # sys.floatinfo
720        check(sys.float_info, size(vh) + self.P * len(sys.float_info))
721        # frame
722        import inspect
723        CO_MAXBLOCKS = 20
724        x = inspect.currentframe()
725        ncells = len(x.f_code.co_cellvars)
726        nfrees = len(x.f_code.co_freevars)
727        extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
728                  ncells + nfrees - 1
729        check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
730        # function
731        def func(): pass
732        check(func, size(h + '11P'))
733        class c():
734            @staticmethod
735            def foo():
736                pass
737            @classmethod
738            def bar(cls):
739                pass
740            # staticmethod
741            check(foo, size(h + 'P'))
742            # classmethod
743            check(bar, size(h + 'P'))
744        # generator
745        def get_gen(): yield 1
746        check(get_gen(), size(h + 'Pi2P'))
747        # iterator
748        check(iter('abc'), size(h + 'lP'))
749        # callable-iterator
750        import re
751        check(re.finditer('',''), size(h + '2P'))
752        # list
753        samples = [[], [1,2,3], ['1', '2', '3']]
754        for sample in samples:
755            check(sample, size(vh + 'PP') + len(sample)*self.P)
756        # sortwrapper (list)
757        # XXX
758        # cmpwrapper (list)
759        # XXX
760        # listiterator (list)
761        check(iter([]), size(h + 'lP'))
762        # listreverseiterator (list)
763        check(reversed([]), size(h + 'lP'))
764        # long
765        check(0, size(vh))
766        check(1, size(vh) + self.longdigit)
767        check(-1, size(vh) + self.longdigit)
768        PyLong_BASE = 2**sys.int_info.bits_per_digit
769        check(int(PyLong_BASE), size(vh) + 2*self.longdigit)
770        check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
771        check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
772        # memory
773        check(memoryview(b''), size(h + 'PP2P2i7P'))
774        # module
775        check(unittest, size(h + '3P'))
776        # None
777        check(None, size(h + ''))
778        # NotImplementedType
779        check(NotImplemented, size(h))
780        # object
781        check(object(), size(h + ''))
782        # property (descriptor object)
783        class C(object):
784            def getx(self): return self.__x
785            def setx(self, value): self.__x = value
786            def delx(self): del self.__x
787            x = property(getx, setx, delx, "")
788            check(x, size(h + '4Pi'))
789        # PyCapsule
790        # XXX
791        # rangeiterator
792        check(iter(range(1)), size(h + '4l'))
793        # reverse
794        check(reversed(''), size(h + 'PP'))
795        # range
796        check(range(1), size(h + '4P'))
797        check(range(66000), size(h + '4P'))
798        # set
799        # frozenset
800        PySet_MINSIZE = 8
801        samples = [[], range(10), range(50)]
802        s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
803        for sample in samples:
804            minused = len(sample)
805            if minused == 0: tmp = 1
806            # the computation of minused is actually a bit more complicated
807            # but this suffices for the sizeof test
808            minused = minused*2
809            newsize = PySet_MINSIZE
810            while newsize <= minused:
811                newsize = newsize << 1
812            if newsize <= 8:
813                check(set(sample), s)
814                check(frozenset(sample), s)
815            else:
816                check(set(sample), s + newsize*struct.calcsize('lP'))
817                check(frozenset(sample), s + newsize*struct.calcsize('lP'))
818        # setiterator
819        check(iter(set()), size(h + 'P3P'))
820        # slice
821        check(slice(0), size(h + '3P'))
822        # super
823        check(super(int), size(h + '3P'))
824        # tuple
825        check((), size(vh))
826        check((1,2,3), size(vh) + 3*self.P)
827        # type
828        # (PyTypeObject + PyNumberMethods + PyMappingMethods +
829        #  PySequenceMethods + PyBufferProcs)
830        s = size(vh + 'P2P15Pl4PP9PP11PI') + size('16Pi17P 3P 10P 2P 2P')
831        check(int, s)
832        # class
833        class newstyleclass(object): pass
834        check(newstyleclass, s)
835        # unicode
836        usize = len('\0'.encode('unicode-internal'))
837        samples = ['', '1'*100]
838        # we need to test for both sizes, because we don't know if the string
839        # has been cached
840        for s in samples:
841            basicsize =  size(h + 'PPPiP') + usize * (len(s) + 1)
842            check(s, basicsize)
843        # weakref
844        import weakref
845        check(weakref.ref(int), size(h + '2Pl2P'))
846        # weakproxy
847        # XXX
848        # weakcallableproxy
849        check(weakref.proxy(int), size(h + '2Pl2P'))
850
851    def test_pythontypes(self):
852        # check all types defined in Python/
853        h = self.header
854        vh = self.vheader
855        size = self.calcsize
856        check = self.check_sizeof
857        # _ast.AST
858        import _ast
859        check(_ast.AST(), size(h + ''))
860        # imp.NullImporter
861        import imp
862        check(imp.NullImporter(self.file.name), size(h + ''))
863        try:
864            raise TypeError
865        except TypeError:
866            tb = sys.exc_info()[2]
867            # traceback
868            if tb != None:
869                check(tb, size(h + '2P2i'))
870        # symtable entry
871        # XXX
872        # sys.flags
873        check(sys.flags, size(vh) + self.P * len(sys.flags))
874
875
876def test_main():
877    test.support.run_unittest(SysModuleTest, SizeofTest)
878
879if __name__ == "__main__":
880    test_main()
881