aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_sys.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_sys.py')
-rw-r--r--Lib/test/test_sys.py515
1 files changed, 289 insertions, 226 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index fbea655547a..a52767a1106 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1,57 +1,69 @@
-# -*- coding: iso-8859-1 -*-
-import unittest, test.test_support
-import sys, os, cStringIO
+import unittest, test.support
+import sys, io, os
import struct
+import subprocess
+import textwrap
+import warnings
import operator
+import codecs
+
+# count the number of test runs, used to create unique
+# strings to intern in test_intern()
+numruns = 0
+
+try:
+ import threading
+except ImportError:
+ threading = None
class SysModuleTest(unittest.TestCase):
+ def setUp(self):
+ self.orig_stdout = sys.stdout
+ self.orig_stderr = sys.stderr
+ self.orig_displayhook = sys.displayhook
+
def tearDown(self):
- test.test_support.reap_children()
+ sys.stdout = self.orig_stdout
+ sys.stderr = self.orig_stderr
+ sys.displayhook = self.orig_displayhook
+ test.support.reap_children()
def test_original_displayhook(self):
- import __builtin__
- savestdout = sys.stdout
- out = cStringIO.StringIO()
+ import builtins
+ out = io.StringIO()
sys.stdout = out
dh = sys.__displayhook__
self.assertRaises(TypeError, dh)
- if hasattr(__builtin__, "_"):
- del __builtin__._
+ if hasattr(builtins, "_"):
+ del builtins._
dh(None)
self.assertEqual(out.getvalue(), "")
- self.assertTrue(not hasattr(__builtin__, "_"))
+ self.assertTrue(not hasattr(builtins, "_"))
dh(42)
self.assertEqual(out.getvalue(), "42\n")
- self.assertEqual(__builtin__._, 42)
+ self.assertEqual(builtins._, 42)
del sys.stdout
self.assertRaises(RuntimeError, dh, 42)
- sys.stdout = savestdout
-
def test_lost_displayhook(self):
- olddisplayhook = sys.displayhook
del sys.displayhook
code = compile("42", "<string>", "single")
self.assertRaises(RuntimeError, eval, code)
- sys.displayhook = olddisplayhook
def test_custom_displayhook(self):
- olddisplayhook = sys.displayhook
def baddisplayhook(obj):
raise ValueError
sys.displayhook = baddisplayhook
code = compile("42", "<string>", "single")
self.assertRaises(ValueError, eval, code)
- sys.displayhook = olddisplayhook
def test_original_excepthook(self):
- savestderr = sys.stderr
- err = cStringIO.StringIO()
+ err = io.StringIO()
sys.stderr = err
eh = sys.__excepthook__
@@ -59,67 +71,28 @@ class SysModuleTest(unittest.TestCase):
self.assertRaises(TypeError, eh)
try:
raise ValueError(42)
- except ValueError, exc:
+ except ValueError as exc:
eh(*sys.exc_info())
- sys.stderr = savestderr
self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
+ def test_excepthook(self):
+ with test.support.captured_output("stderr") as stderr:
+ sys.excepthook(1, '1', 1)
+ self.assertTrue("TypeError: print_exception(): Exception expected for " \
+ "value, str found" in stderr.getvalue())
+
# FIXME: testing the code for a lost or replaced excepthook in
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
- def test_exc_clear(self):
- self.assertRaises(TypeError, sys.exc_clear, 42)
-
- # Verify that exc_info is present and matches exc, then clear it, and
- # check that it worked.
- def clear_check(exc):
- typ, value, traceback = sys.exc_info()
- self.assertTrue(typ is not None)
- self.assertTrue(value is exc)
- self.assertTrue(traceback is not None)
-
- with test.test_support.check_py3k_warnings():
- sys.exc_clear()
-
- typ, value, traceback = sys.exc_info()
- self.assertTrue(typ is None)
- self.assertTrue(value is None)
- self.assertTrue(traceback is None)
-
- def clear():
- try:
- raise ValueError, 42
- except ValueError, exc:
- clear_check(exc)
-
- # Raise an exception and check that it can be cleared
- clear()
-
- # Verify that a frame currently handling an exception is
- # unaffected by calling exc_clear in a nested frame.
- try:
- raise ValueError, 13
- except ValueError, exc:
- typ1, value1, traceback1 = sys.exc_info()
- clear()
- typ2, value2, traceback2 = sys.exc_info()
-
- self.assertTrue(typ1 is typ2)
- self.assertTrue(value1 is exc)
- self.assertTrue(value1 is value2)
- self.assertTrue(traceback1 is traceback2)
-
- # Check that an exception can be cleared outside of an except block
- clear_check(exc)
-
def test_exit(self):
+
self.assertRaises(TypeError, sys.exit, 42, 42)
# call without argument
try:
sys.exit(0)
- except SystemExit, exc:
+ except SystemExit as exc:
self.assertEqual(exc.code, 0)
except:
self.fail("wrong exception")
@@ -130,7 +103,7 @@ class SysModuleTest(unittest.TestCase):
# entry will be unpacked
try:
sys.exit(42)
- except SystemExit, exc:
+ except SystemExit as exc:
self.assertEqual(exc.code, 42)
except:
self.fail("wrong exception")
@@ -140,7 +113,7 @@ class SysModuleTest(unittest.TestCase):
# call with integer argument
try:
sys.exit((42,))
- except SystemExit, exc:
+ except SystemExit as exc:
self.assertEqual(exc.code, 42)
except:
self.fail("wrong exception")
@@ -150,7 +123,7 @@ class SysModuleTest(unittest.TestCase):
# call with string argument
try:
sys.exit("exit")
- except SystemExit, exc:
+ except SystemExit as exc:
self.assertEqual(exc.code, "exit")
except:
self.fail("wrong exception")
@@ -160,7 +133,7 @@ class SysModuleTest(unittest.TestCase):
# call with tuple argument with two entries
try:
sys.exit((17, 23))
- except SystemExit, exc:
+ except SystemExit as exc:
self.assertEqual(exc.code, (17, 23))
except:
self.fail("wrong exception")
@@ -168,12 +141,6 @@ class SysModuleTest(unittest.TestCase):
self.fail("no exception")
# test that the exit machinery handles SystemExits properly
- import subprocess
- # both unnormalized...
- rc = subprocess.call([sys.executable, "-c",
- "raise SystemExit, 46"])
- self.assertEqual(rc, 46)
- # ... and normalized
rc = subprocess.call([sys.executable, "-c",
"raise SystemExit(47)"])
self.assertEqual(rc, 47)
@@ -184,7 +151,7 @@ class SysModuleTest(unittest.TestCase):
stdout, stderr = process.communicate()
self.assertEqual(process.returncode, 1)
self.assertTrue(stderr.startswith(expected),
- "%s doesn't start with %s" % (repr(stderr), repr(expected)))
+ "%s doesn't start with %s" % (ascii(stderr), ascii(expected)))
# test that stderr buffer if flushed before the exit message is written
# into stderr
@@ -192,28 +159,52 @@ class SysModuleTest(unittest.TestCase):
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
b"unflushed,message")
+ # test that the exit message is written with backslashreplace error
+ # handler to stderr
+ check_exit_message(
+ r'import sys; sys.exit("surrogates:\uDCFF")',
+ b"surrogates:\\udcff")
+
# test that the unicode message is encoded to the stderr encoding
+ # instead of the default encoding (utf8)
env = os.environ.copy()
env['PYTHONIOENCODING'] = 'latin-1'
check_exit_message(
- r'import sys; sys.exit(u"h\xe9")',
+ r'import sys; sys.exit("h\xe9")',
b"h\xe9", env=env)
def test_getdefaultencoding(self):
- if test.test_support.have_unicode:
- self.assertRaises(TypeError, sys.getdefaultencoding, 42)
- # can't check more than the type, as the user might have changed it
- self.assertIsInstance(sys.getdefaultencoding(), str)
+ self.assertRaises(TypeError, sys.getdefaultencoding, 42)
+ # can't check more than the type, as the user might have changed it
+ self.assertIsInstance(sys.getdefaultencoding(), str)
# testing sys.settrace() is done in test_sys_settrace.py
# testing sys.setprofile() is done in test_sys_setprofile.py
def test_setcheckinterval(self):
- self.assertRaises(TypeError, sys.setcheckinterval)
- orig = sys.getcheckinterval()
- for n in 0, 100, 120, orig: # orig last to restore starting state
- sys.setcheckinterval(n)
- self.assertEqual(sys.getcheckinterval(), n)
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ self.assertRaises(TypeError, sys.setcheckinterval)
+ orig = sys.getcheckinterval()
+ for n in 0, 100, 120, orig: # orig last to restore starting state
+ sys.setcheckinterval(n)
+ self.assertEqual(sys.getcheckinterval(), n)
+
+ @unittest.skipUnless(threading, 'Threading required for this test.')
+ def test_switchinterval(self):
+ self.assertRaises(TypeError, sys.setswitchinterval)
+ self.assertRaises(TypeError, sys.setswitchinterval, "a")
+ self.assertRaises(ValueError, sys.setswitchinterval, -1.0)
+ self.assertRaises(ValueError, sys.setswitchinterval, 0.0)
+ orig = sys.getswitchinterval()
+ # sanity check
+ self.assertTrue(orig < 0.5, orig)
+ try:
+ for n in 0.00001, 0.05, 3.0, orig:
+ sys.setswitchinterval(n)
+ self.assertAlmostEqual(sys.getswitchinterval(), n)
+ finally:
+ sys.setswitchinterval(orig)
def test_recursionlimit(self):
self.assertRaises(TypeError, sys.getrecursionlimit, 42)
@@ -224,21 +215,54 @@ class SysModuleTest(unittest.TestCase):
self.assertEqual(sys.getrecursionlimit(), 10000)
sys.setrecursionlimit(oldlimit)
- self.assertRaises(OverflowError, sys.setrecursionlimit, 1 << 31)
+ @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
+ 'fatal error if run with a trace function')
+ def test_recursionlimit_recovery(self):
+ # NOTE: this test is slightly fragile in that it depends on the current
+ # recursion count when executing the test being low enough so as to
+ # trigger the recursion recovery detection in the _Py_MakeEndRecCheck
+ # macro (see ceval.h).
+ oldlimit = sys.getrecursionlimit()
+ def f():
+ f()
try:
- sys.setrecursionlimit((1 << 31) - 5)
- try:
- # issue13546: isinstance(e, ValueError) used to fail
- # when the recursion limit is close to 1<<31
- raise ValueError()
- except ValueError, e:
- pass
+ for i in (50, 1000):
+ # Issue #5392: stack overflow after hitting recursion limit twice
+ sys.setrecursionlimit(i)
+ self.assertRaises(RuntimeError, f)
+ self.assertRaises(RuntimeError, f)
finally:
sys.setrecursionlimit(oldlimit)
+ def test_recursionlimit_fatalerror(self):
+ # A fatal error occurs if a second recursion limit is hit when recovering
+ # from a first one.
+ if os.name == "nt":
+ raise unittest.SkipTest(
+ "under Windows, test would generate a spurious crash dialog")
+ code = textwrap.dedent("""
+ import sys
+
+ def f():
+ try:
+ f()
+ except RuntimeError:
+ f()
+
+ sys.setrecursionlimit(%d)
+ f()""")
+ for i in (50, 1000):
+ sub = subprocess.Popen([sys.executable, '-c', code % i],
+ stderr=subprocess.PIPE)
+ err = sub.communicate()[1]
+ self.assertTrue(sub.returncode, sub.returncode)
+ self.assertTrue(
+ b"Fatal Python error: Cannot recover from stack overflow" in err,
+ err)
+
def test_getwindowsversion(self):
# Raise SkipTest if sys doesn't have getwindowsversion attribute
- test.test_support.get_attribute(sys, "getwindowsversion")
+ test.support.get_attribute(sys, "getwindowsversion")
v = sys.getwindowsversion()
self.assertEqual(len(v), 5)
self.assertIsInstance(v[0], int)
@@ -266,6 +290,9 @@ class SysModuleTest(unittest.TestCase):
# still has 5 elements
maj, min, buildno, plat, csd = sys.getwindowsversion()
+ def test_call_tracing(self):
+ self.assertRaises(TypeError, sys.call_tracing, type, 2)
+
def test_dlopenflags(self):
if hasattr(sys, "setdlopenflags"):
self.assertTrue(hasattr(sys, "getdlopenflags"))
@@ -295,7 +322,7 @@ class SysModuleTest(unittest.TestCase):
self.assertRaises(TypeError, sys._getframe, 42, 42)
self.assertRaises(ValueError, sys._getframe, 2000000000)
self.assertTrue(
- SysModuleTest.test_getframe.im_func.func_code \
+ SysModuleTest.test_getframe.__code__ \
is sys._getframe().f_code
)
@@ -303,7 +330,7 @@ class SysModuleTest(unittest.TestCase):
def test_current_frames(self):
have_threads = True
try:
- import thread
+ import _thread
except ImportError:
have_threads = False
@@ -313,9 +340,9 @@ class SysModuleTest(unittest.TestCase):
self.current_frames_without_threads()
# Test sys._current_frames() in a WITH_THREADS build.
- @test.test_support.reap_threads
+ @test.support.reap_threads
def current_frames_with_threads(self):
- import threading, thread
+ import threading, _thread
import traceback
# Spawn a thread that blocks at a known place. Then the main
@@ -329,7 +356,7 @@ class SysModuleTest(unittest.TestCase):
g456()
def g456():
- thread_info.append(thread.get_ident())
+ thread_info.append(_thread.get_ident())
entered_g.set()
leave_g.wait()
@@ -345,7 +372,7 @@ class SysModuleTest(unittest.TestCase):
d = sys._current_frames()
- main_id = thread.get_ident()
+ main_id = _thread.get_ident()
self.assertIn(main_id, d)
self.assertIn(thread_id, d)
@@ -389,23 +416,39 @@ class SysModuleTest(unittest.TestCase):
self.assertIsInstance(sys.argv, list)
self.assertIn(sys.byteorder, ("little", "big"))
self.assertIsInstance(sys.builtin_module_names, tuple)
- self.assertIsInstance(sys.copyright, basestring)
- self.assertIsInstance(sys.exec_prefix, basestring)
- self.assertIsInstance(sys.executable, basestring)
+ self.assertIsInstance(sys.copyright, str)
+ self.assertIsInstance(sys.exec_prefix, str)
+ self.assertIsInstance(sys.executable, str)
self.assertEqual(len(sys.float_info), 11)
self.assertEqual(sys.float_info.radix, 2)
- self.assertEqual(len(sys.long_info), 2)
- self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
- self.assertTrue(sys.long_info.sizeof_digit >= 1)
- self.assertEqual(type(sys.long_info.bits_per_digit), int)
- self.assertEqual(type(sys.long_info.sizeof_digit), int)
+ self.assertEqual(len(sys.int_info), 2)
+ self.assertTrue(sys.int_info.bits_per_digit % 5 == 0)
+ self.assertTrue(sys.int_info.sizeof_digit >= 1)
+ self.assertEqual(type(sys.int_info.bits_per_digit), int)
+ self.assertEqual(type(sys.int_info.sizeof_digit), int)
self.assertIsInstance(sys.hexversion, int)
- self.assertIsInstance(sys.maxint, int)
- if test.test_support.have_unicode:
- self.assertIsInstance(sys.maxunicode, int)
- self.assertIsInstance(sys.platform, basestring)
- self.assertIsInstance(sys.prefix, basestring)
- self.assertIsInstance(sys.version, basestring)
+
+ self.assertEqual(len(sys.hash_info), 5)
+ self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
+ # sys.hash_info.modulus should be a prime; we do a quick
+ # probable primality test (doesn't exclude the possibility of
+ # a Carmichael number)
+ for x in range(1, 100):
+ self.assertEqual(
+ pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus),
+ 1,
+ "sys.hash_info.modulus {} is a non-prime".format(
+ sys.hash_info.modulus)
+ )
+ self.assertIsInstance(sys.hash_info.inf, int)
+ self.assertIsInstance(sys.hash_info.nan, int)
+ self.assertIsInstance(sys.hash_info.imag, int)
+
+ self.assertIsInstance(sys.maxsize, int)
+ self.assertIsInstance(sys.maxunicode, int)
+ self.assertIsInstance(sys.platform, str)
+ self.assertIsInstance(sys.prefix, str)
+ self.assertIsInstance(sys.version, str)
vi = sys.version_info
self.assertIsInstance(vi[:], tuple)
self.assertEqual(len(vi), 5)
@@ -427,48 +470,67 @@ class SysModuleTest(unittest.TestCase):
self.assertTrue(vi > (1,0,0))
self.assertIsInstance(sys.float_repr_style, str)
self.assertIn(sys.float_repr_style, ('short', 'legacy'))
+ if not sys.platform.startswith('win'):
+ self.assertIsInstance(sys.abiflags, str)
def test_43581(self):
- # Can't use sys.stdout, as this is a cStringIO object when
+ # Can't use sys.stdout, as this is a StringIO object when
# the test runs under regrtest.
- self.assertTrue(sys.__stdout__.encoding == sys.__stderr__.encoding)
+ self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
+
+ def test_intern(self):
+ global numruns
+ numruns += 1
+ self.assertRaises(TypeError, sys.intern)
+ s = "never interned before" + str(numruns)
+ self.assertTrue(sys.intern(s) is s)
+ s2 = s.swapcase().swapcase()
+ self.assertTrue(sys.intern(s2) is s)
+
+ # Subclasses of string can't be interned, because they
+ # provide too much opportunity for insane things to happen.
+ # We don't want them in the interned dict and if they aren't
+ # actually interned, we don't want to create the appearance
+ # that they are by allowing intern() to succeed.
+ class S(str):
+ def __hash__(self):
+ return 123
+
+ self.assertRaises(TypeError, sys.intern, S("abc"))
def test_sys_flags(self):
self.assertTrue(sys.flags)
- attrs = ("debug", "py3k_warning", "division_warning", "division_new",
+ attrs = ("debug", "division_warning",
"inspect", "interactive", "optimize", "dont_write_bytecode",
- "no_site", "ignore_environment", "tabcheck", "verbose",
- "unicode", "bytes_warning", "hash_randomization")
+ "no_user_site", "no_site", "ignore_environment", "verbose",
+ "bytes_warning", "quiet", "hash_randomization")
for attr in attrs:
self.assertTrue(hasattr(sys.flags, attr), attr)
self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
self.assertTrue(repr(sys.flags))
+ self.assertEqual(len(sys.flags), len(attrs))
def test_clear_type_cache(self):
sys._clear_type_cache()
def test_ioencoding(self):
- import subprocess
env = dict(os.environ)
# Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
# not representable in ASCII.
env["PYTHONIOENCODING"] = "cp424"
- p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
+ p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
stdout = subprocess.PIPE, env=env)
out = p.communicate()[0].strip()
- self.assertEqual(out, unichr(0xa2).encode("cp424"))
+ expected = ("\xa2" + os.linesep).encode("cp424")
+ self.assertEqual(out, expected)
env["PYTHONIOENCODING"] = "ascii:replace"
- p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
+ p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
stdout = subprocess.PIPE, env=env)
out = p.communicate()[0].strip()
- self.assertEqual(out, '?')
-
- def test_call_tracing(self):
- self.assertEqual(sys.call_tracing(str, (2,)), "2")
- self.assertRaises(TypeError, sys.call_tracing, str, 2)
+ self.assertEqual(out, b'?')
def test_executable(self):
# sys.executable should be absolute
@@ -477,59 +539,78 @@ class SysModuleTest(unittest.TestCase):
# Issue #7774: Ensure that sys.executable is an empty string if argv[0]
# has been set to an non existent program name and Python is unable to
# retrieve the real program name
- import subprocess
+
# For a normal installation, it should work without 'cwd'
# argument. For test runs in the build directory, see #7774.
python_dir = os.path.dirname(os.path.realpath(sys.executable))
p = subprocess.Popen(
- ["nonexistent", "-c", 'import sys; print repr(sys.executable)'],
+ ["nonexistent", "-c",
+ 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'],
executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
- executable = p.communicate()[0].strip()
+ stdout = p.communicate()[0]
+ executable = stdout.strip().decode("ASCII")
p.wait()
- self.assertIn(executable, ["''", repr(sys.executable)])
+ self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))])
+
+ def check_fsencoding(self, fs_encoding, expected=None):
+ self.assertIsNotNone(fs_encoding)
+ codecs.lookup(fs_encoding)
+ if expected:
+ self.assertEqual(fs_encoding, expected)
+
+ def test_getfilesystemencoding(self):
+ fs_encoding = sys.getfilesystemencoding()
+ if sys.platform == 'darwin':
+ expected = 'utf-8'
+ elif sys.platform == 'win32':
+ expected = 'mbcs'
+ else:
+ expected = None
+ self.check_fsencoding(fs_encoding, expected)
+
class SizeofTest(unittest.TestCase):
def setUp(self):
self.P = struct.calcsize('P')
- self.longdigit = sys.long_info.sizeof_digit
+ self.longdigit = sys.int_info.sizeof_digit
import _testcapi
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
- self.file = open(test.test_support.TESTFN, 'wb')
+ self.file = open(test.support.TESTFN, 'wb')
def tearDown(self):
self.file.close()
- test.test_support.unlink(test.test_support.TESTFN)
+ test.support.unlink(test.support.TESTFN)
- check_sizeof = test.test_support.check_sizeof
+ check_sizeof = test.support.check_sizeof
def test_gc_head_size(self):
# Check that the gc header size is added to objects tracked by the gc.
- size = test.test_support.calcobjsize
+ vsize = test.support.calcvobjsize
gc_header_size = self.gc_headsize
# bool objects are not gc tracked
- self.assertEqual(sys.getsizeof(True), size('l'))
+ self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
# but lists are
- self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)
+ self.assertEqual(sys.getsizeof([]), vsize('PP') + gc_header_size)
def test_default(self):
- size = test.test_support.calcobjsize
- self.assertEqual(sys.getsizeof(True, -1), size('l'))
+ vsize = test.support.calcvobjsize
+ self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
+ self.assertEqual(sys.getsizeof(True, -1), vsize('') + self.longdigit)
def test_objecttypes(self):
# check all types defined in Objects/
- size = test.test_support.calcobjsize
- vsize = test.test_support.calcvobjsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# bool
- check(True, size('l'))
+ check(True, vsize('') + self.longdigit)
# buffer
- with test.test_support.check_py3k_warnings():
- check(buffer(''), size('2P2Pil'))
+ # XXX
# builtin_function_or_method
- check(len, size('3P'))
+ check(len, size('3P')) # XXX check layout
# bytearray
- samples = ['', 'u'*100000]
+ samples = [b'', b'u'*100000]
for sample in samples:
x = bytearray(sample)
check(x, vsize('iPP') + x.__alloc__())
@@ -541,28 +622,11 @@ class SizeofTest(unittest.TestCase):
def inner():
return x
return inner
- check(get_cell().func_closure[0], size('P'))
- # classobj (old-style class)
- class class_oldstyle():
- def method():
- pass
- check(class_oldstyle, size('7P'))
- # instance (old-style class)
- check(class_oldstyle(), size('3P'))
- # instancemethod (old-style class)
- check(class_oldstyle().method, size('4P'))
+ check(get_cell().__closure__[0], size('P'))
+ # code
+ check(get_cell().__code__, size('5i8Pi3P'))
# complex
check(complex(0,1), size('2d'))
- # code
- check(get_cell().func_code, size('4i8Pi3P'))
- # BaseException
- check(BaseException(), size('3P'))
- # UnicodeEncodeError
- check(UnicodeEncodeError("", u"", 0, 0, ""), size('5P2PP'))
- # UnicodeDecodeError
- check(UnicodeDecodeError("", "", 0, 0, ""), size('5P2PP'))
- # UnicodeTranslateError
- check(UnicodeTranslateError(u"", 0, 1, ""), size('5P2PP'))
# method_descriptor (descriptor object)
check(str.lower, size('2PP'))
# classmethod_descriptor (descriptor object)
@@ -571,25 +635,35 @@ class SizeofTest(unittest.TestCase):
import datetime
check(datetime.timedelta.days, size('2PP'))
# getset_descriptor (descriptor object)
- import __builtin__
- check(__builtin__.file.closed, size('2PP'))
+ import collections
+ check(collections.defaultdict.default_factory, size('2PP'))
# wrapper_descriptor (descriptor object)
check(int.__add__, size('2P2P'))
- # dictproxy
- class C(object): pass
- check(C.__dict__, size('P'))
# method-wrapper (descriptor object)
check({}.__iter__, size('2P'))
# dict
check({}, size('3P2P' + 8*'P2P'))
- x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
- check(x, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
+ longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
+ check(longdict, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
# dictionary-keyiterator
- check({}.iterkeys(), size('P2PPP'))
+ check({}.keys(), size('P'))
# dictionary-valueiterator
- check({}.itervalues(), size('P2PPP'))
+ check({}.values(), size('P'))
# dictionary-itemiterator
- check({}.iteritems(), size('P2PPP'))
+ check({}.items(), size('P'))
+ # dictionary iterator
+ check(iter({}), size('P2PPP'))
+ # dictproxy
+ class C(object): pass
+ check(C.__dict__, size('P'))
+ # BaseException
+ check(BaseException(), size('5P'))
+ # UnicodeEncodeError
+ check(UnicodeEncodeError("", "", 0, 0, ""), size('5P 2P2PP'))
+ # UnicodeDecodeError
+ check(UnicodeDecodeError("", b"", 0, 0, ""), size('5P 2P2PP'))
+ # UnicodeTranslateError
+ check(UnicodeTranslateError("", 0, 1, ""), size('5P 2P2PP'))
# ellipses
check(Ellipsis, size(''))
# EncodingMap
@@ -598,8 +672,8 @@ class SizeofTest(unittest.TestCase):
check(x, size('32B2iB'))
# enumerate
check(enumerate([]), size('l3P'))
- # file
- check(self.file, size('4P2i4P3i3P3i'))
+ # reverse
+ check(reversed(''), size('PP'))
# float
check(float(0), size('d'))
# sys.floatinfo
@@ -611,11 +685,11 @@ class SizeofTest(unittest.TestCase):
ncells = len(x.f_code.co_cellvars)
nfrees = len(x.f_code.co_freevars)
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
- ncells + nfrees - 1
+ ncells + nfrees - 1
check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
# function
def func(): pass
- check(func, size('9P'))
+ check(func, size('11P'))
class c():
@staticmethod
def foo():
@@ -630,9 +704,6 @@ class SizeofTest(unittest.TestCase):
# generator
def get_gen(): yield 1
check(get_gen(), size('Pi2P'))
- # integer
- check(1, size('l'))
- check(100, size('l'))
# iterator
check(iter('abc'), size('lP'))
# callable-iterator
@@ -651,17 +722,21 @@ class SizeofTest(unittest.TestCase):
# listreverseiterator (list)
check(reversed([]), size('lP'))
# long
- check(0L, vsize(''))
- check(1L, vsize('') + self.longdigit)
- check(-1L, vsize('') + self.longdigit)
- PyLong_BASE = 2**sys.long_info.bits_per_digit
- check(long(PyLong_BASE), vsize('') + 2*self.longdigit)
- check(long(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
- check(long(PyLong_BASE**2), vsize('') + 3*self.longdigit)
+ check(0, vsize(''))
+ check(1, vsize('') + self.longdigit)
+ check(-1, vsize('') + self.longdigit)
+ PyLong_BASE = 2**sys.int_info.bits_per_digit
+ check(int(PyLong_BASE), vsize('') + 2*self.longdigit)
+ check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
+ check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
+ # memoryview
+ check(memoryview(b''), size('PP2P2i7P'))
# module
- check(unittest, size('P'))
+ check(unittest, size('3P'))
# None
check(None, size(''))
+ # NotImplementedType
+ check(NotImplemented, size(''))
# object
check(object(), size(''))
# property (descriptor object)
@@ -671,18 +746,20 @@ class SizeofTest(unittest.TestCase):
def delx(self): del self.__x
x = property(getx, setx, delx, "")
check(x, size('4Pi'))
- # PyCObject
# PyCapsule
# XXX
# rangeiterator
- check(iter(xrange(1)), size('4l'))
+ check(iter(range(1)), size('4l'))
# reverse
check(reversed(''), size('PP'))
+ # range
+ check(range(1), size('4P'))
+ check(range(66000), size('4P'))
# set
# frozenset
PySet_MINSIZE = 8
samples = [[], range(10), range(50)]
- s = size('3P2P' + PySet_MINSIZE*'lP' + 'lP')
+ s = size('3P2P' + PySet_MINSIZE*'PP' + 'PP')
for sample in samples:
minused = len(sample)
if minused == 0: tmp = 1
@@ -701,37 +778,28 @@ class SizeofTest(unittest.TestCase):
# setiterator
check(iter(set()), size('P3P'))
# slice
- check(slice(1), size('3P'))
- # str
- vh = test.test_support._vheader
- check('', struct.calcsize(vh + 'lic'))
- check('abc', struct.calcsize(vh + 'lic') + 3)
+ check(slice(0), size('3P'))
# super
check(super(int), size('3P'))
# tuple
check((), vsize(''))
check((1,2,3), vsize('') + 3*self.P)
- # tupleiterator
- check(iter(()), size('lP'))
# type
- # (PyTypeObject + PyNumberMethods + PyMappingMethods +
+ # (PyTypeObject + PyNumberMethods + PyMappingMethods +
# PySequenceMethods + PyBufferProcs)
- s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('41P 10P 3P 6P')
- class newstyleclass(object):
- pass
- check(newstyleclass, s)
- # builtin type
+ s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('16Pi17P 3P 10P 2P 2P')
check(int, s)
- # NotImplementedType
- import types
- check(types.NotImplementedType, s)
+ # class
+ class newstyleclass(object): pass
+ check(newstyleclass, s)
# unicode
- usize = len(u'\0'.encode('unicode-internal'))
- samples = [u'', u'1'*100]
+ usize = len('\0'.encode('unicode-internal'))
+ samples = ['', '1'*100]
# we need to test for both sizes, because we don't know if the string
# has been cached
for s in samples:
- check(s, size('PPlP') + usize * (len(s) + 1))
+ basicsize = size('PPPiP') + usize * (len(s) + 1)
+ check(s, basicsize)
# weakref
import weakref
check(weakref.ref(int), size('2Pl2P'))
@@ -739,14 +807,11 @@ class SizeofTest(unittest.TestCase):
# XXX
# weakcallableproxy
check(weakref.proxy(int), size('2Pl2P'))
- # xrange
- check(xrange(1), size('3l'))
- check(xrange(66000), size('3l'))
def test_pythontypes(self):
# check all types defined in Python/
- size = test.test_support.calcobjsize
- vsize = test.test_support.calcvobjsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
@@ -768,9 +833,7 @@ class SizeofTest(unittest.TestCase):
def test_main():
- test_classes = (SysModuleTest, SizeofTest)
-
- test.test_support.run_unittest(*test_classes)
+ test.support.run_unittest(SysModuleTest, SizeofTest)
if __name__ == "__main__":
test_main()