2003-02-03 19:03:49 -04:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
import unittest, test.test_support
|
2014-02-18 10:36:14 -04:00
|
|
|
from test.script_helper import assert_python_ok, assert_python_failure
|
2010-02-07 13:03:15 -04:00
|
|
|
import sys, os, cStringIO
|
2008-06-25 06:20:03 -03:00
|
|
|
import struct
|
2010-01-26 20:28:29 -04:00
|
|
|
import operator
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
class SysModuleTest(unittest.TestCase):
|
|
|
|
|
2009-10-27 16:23:56 -03:00
|
|
|
def tearDown(self):
|
|
|
|
test.test_support.reap_children()
|
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_original_displayhook(self):
|
|
|
|
import __builtin__
|
|
|
|
savestdout = sys.stdout
|
|
|
|
out = cStringIO.StringIO()
|
|
|
|
sys.stdout = out
|
|
|
|
|
|
|
|
dh = sys.__displayhook__
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, dh)
|
|
|
|
if hasattr(__builtin__, "_"):
|
|
|
|
del __builtin__._
|
|
|
|
|
|
|
|
dh(None)
|
|
|
|
self.assertEqual(out.getvalue(), "")
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(not hasattr(__builtin__, "_"))
|
2003-02-03 19:03:49 -04:00
|
|
|
dh(42)
|
|
|
|
self.assertEqual(out.getvalue(), "42\n")
|
|
|
|
self.assertEqual(__builtin__._, 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()
|
|
|
|
sys.stderr = err
|
|
|
|
|
|
|
|
eh = sys.__excepthook__
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, eh)
|
|
|
|
try:
|
|
|
|
raise ValueError(42)
|
|
|
|
except ValueError, exc:
|
|
|
|
eh(*sys.exc_info())
|
|
|
|
|
|
|
|
sys.stderr = savestderr
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2003-02-03 19:05:27 -04:00
|
|
|
# FIXME: testing the code for a lost or replaced excepthook in
|
2003-02-03 19:03:49 -04:00
|
|
|
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
|
|
|
|
|
2003-02-28 23:20:41 -04:00
|
|
|
def test_exc_clear(self):
|
2010-01-08 15:04:16 -04:00
|
|
|
self.assertRaises(TypeError, sys.exc_clear, 42)
|
2003-02-28 23:20:41 -04:00
|
|
|
|
|
|
|
# Verify that exc_info is present and matches exc, then clear it, and
|
|
|
|
# check that it worked.
|
|
|
|
def clear_check(exc):
|
2003-02-28 23:25:41 -04:00
|
|
|
typ, value, traceback = sys.exc_info()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(typ is not None)
|
|
|
|
self.assertTrue(value is exc)
|
|
|
|
self.assertTrue(traceback is not None)
|
2003-02-28 23:20:41 -04:00
|
|
|
|
2010-03-17 10:52:48 -03:00
|
|
|
with test.test_support.check_py3k_warnings():
|
|
|
|
sys.exc_clear()
|
2003-02-28 23:20:41 -04:00
|
|
|
|
2003-02-28 23:25:41 -04:00
|
|
|
typ, value, traceback = sys.exc_info()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(typ is None)
|
|
|
|
self.assertTrue(value is None)
|
|
|
|
self.assertTrue(traceback is None)
|
2003-02-28 23:20:41 -04:00
|
|
|
|
|
|
|
def clear():
|
2003-02-28 23:25:41 -04:00
|
|
|
try:
|
|
|
|
raise ValueError, 42
|
|
|
|
except ValueError, exc:
|
|
|
|
clear_check(exc)
|
2003-02-28 23:20:41 -04:00
|
|
|
|
|
|
|
# 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:
|
2003-02-28 23:25:41 -04:00
|
|
|
raise ValueError, 13
|
2003-02-28 23:20:41 -04:00
|
|
|
except ValueError, exc:
|
2003-02-28 23:25:41 -04:00
|
|
|
typ1, value1, traceback1 = sys.exc_info()
|
|
|
|
clear()
|
|
|
|
typ2, value2, traceback2 = sys.exc_info()
|
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(typ1 is typ2)
|
|
|
|
self.assertTrue(value1 is exc)
|
|
|
|
self.assertTrue(value1 is value2)
|
|
|
|
self.assertTrue(traceback1 is traceback2)
|
2003-02-28 23:20:41 -04:00
|
|
|
|
|
|
|
# Check that an exception can be cleared outside of an except block
|
|
|
|
clear_check(exc)
|
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_exit(self):
|
2014-02-18 10:36:14 -04:00
|
|
|
# call with two arguments
|
2003-02-03 19:03:49 -04:00
|
|
|
self.assertRaises(TypeError, sys.exit, 42, 42)
|
|
|
|
|
|
|
|
# call without argument
|
2014-02-19 12:43:13 -04:00
|
|
|
with self.assertRaises(SystemExit) as cm:
|
|
|
|
sys.exit()
|
|
|
|
self.assertIsNone(cm.exception.code)
|
|
|
|
|
2014-02-18 10:36:14 -04:00
|
|
|
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()')
|
|
|
|
self.assertEqual(rc, 0)
|
|
|
|
self.assertEqual(out, b'')
|
|
|
|
self.assertEqual(err, b'')
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2014-02-18 10:36:14 -04:00
|
|
|
# call with integer argument
|
|
|
|
with self.assertRaises(SystemExit) as cm:
|
2003-02-03 19:03:49 -04:00
|
|
|
sys.exit(42)
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(cm.exception.code, 42)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2014-02-18 10:36:14 -04:00
|
|
|
# call with tuple argument with one entry
|
|
|
|
# entry will be unpacked
|
|
|
|
with self.assertRaises(SystemExit) as cm:
|
2003-02-03 19:03:49 -04:00
|
|
|
sys.exit((42,))
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(cm.exception.code, 42)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
# call with string argument
|
2014-02-18 10:36:14 -04:00
|
|
|
with self.assertRaises(SystemExit) as cm:
|
2003-02-03 19:03:49 -04:00
|
|
|
sys.exit("exit")
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(cm.exception.code, "exit")
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
# call with tuple argument with two entries
|
2014-02-18 10:36:14 -04:00
|
|
|
with self.assertRaises(SystemExit) as cm:
|
2003-02-03 19:03:49 -04:00
|
|
|
sys.exit((17, 23))
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(cm.exception.code, (17, 23))
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2005-02-15 11:26:11 -04:00
|
|
|
# test that the exit machinery handles SystemExits properly
|
|
|
|
# both unnormalized...
|
2014-02-18 10:36:14 -04:00
|
|
|
rc, out, err = assert_python_failure('-c', 'raise SystemExit, 46')
|
2005-02-15 11:26:11 -04:00
|
|
|
self.assertEqual(rc, 46)
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(out, b'')
|
|
|
|
self.assertEqual(err, b'')
|
2005-02-15 11:26:11 -04:00
|
|
|
# ... and normalized
|
2014-02-18 10:36:14 -04:00
|
|
|
rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)')
|
2005-02-15 11:26:11 -04:00
|
|
|
self.assertEqual(rc, 47)
|
2014-02-18 10:36:14 -04:00
|
|
|
self.assertEqual(out, b'')
|
|
|
|
self.assertEqual(err, b'')
|
2005-02-15 17:50:12 -04:00
|
|
|
|
2014-02-18 10:36:14 -04:00
|
|
|
def check_exit_message(code, expected, **env_vars):
|
|
|
|
rc, out, err = assert_python_failure('-c', code, **env_vars)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
self.assertEqual(out, b'')
|
|
|
|
self.assertTrue(err.startswith(expected),
|
|
|
|
"%s doesn't start with %s" % (repr(err), repr(expected)))
|
2010-05-25 19:30:32 -03:00
|
|
|
|
2014-02-18 10:36:14 -04:00
|
|
|
# test that stderr buffer is flushed before the exit message is written
|
2010-05-25 19:30:32 -03:00
|
|
|
# into stderr
|
|
|
|
check_exit_message(
|
|
|
|
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
|
|
|
|
b"unflushed,message")
|
|
|
|
|
|
|
|
# test that the unicode message is encoded to the stderr encoding
|
|
|
|
check_exit_message(
|
|
|
|
r'import sys; sys.exit(u"h\xe9")',
|
2014-02-18 10:36:14 -04:00
|
|
|
b"h\xe9", PYTHONIOENCODING='latin-1')
|
2005-02-15 17:50:12 -04:00
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
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
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.getdefaultencoding(), str)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2010-07-28 11:33:17 -03:00
|
|
|
# testing sys.settrace() is done in test_sys_settrace.py
|
|
|
|
# testing sys.setprofile() is done in test_sys_setprofile.py
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
def test_setcheckinterval(self):
|
2003-02-18 22:35:07 -04:00
|
|
|
self.assertRaises(TypeError, sys.setcheckinterval)
|
2003-07-06 15:36:54 -03:00
|
|
|
orig = sys.getcheckinterval()
|
|
|
|
for n in 0, 100, 120, orig: # orig last to restore starting state
|
|
|
|
sys.setcheckinterval(n)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(sys.getcheckinterval(), n)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
def test_recursionlimit(self):
|
2003-02-18 22:35:07 -04:00
|
|
|
self.assertRaises(TypeError, sys.getrecursionlimit, 42)
|
|
|
|
oldlimit = sys.getrecursionlimit()
|
|
|
|
self.assertRaises(TypeError, sys.setrecursionlimit)
|
|
|
|
self.assertRaises(ValueError, sys.setrecursionlimit, -42)
|
|
|
|
sys.setrecursionlimit(10000)
|
|
|
|
self.assertEqual(sys.getrecursionlimit(), 10000)
|
|
|
|
sys.setrecursionlimit(oldlimit)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2011-12-07 16:46:48 -04:00
|
|
|
self.assertRaises(OverflowError, sys.setrecursionlimit, 1 << 31)
|
|
|
|
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
|
|
|
|
finally:
|
|
|
|
sys.setrecursionlimit(oldlimit)
|
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_getwindowsversion(self):
|
2010-01-26 21:21:15 -04:00
|
|
|
# Raise SkipTest if sys doesn't have getwindowsversion attribute
|
|
|
|
test.test_support.get_attribute(sys, "getwindowsversion")
|
|
|
|
v = sys.getwindowsversion()
|
|
|
|
self.assertEqual(len(v), 5)
|
|
|
|
self.assertIsInstance(v[0], int)
|
|
|
|
self.assertIsInstance(v[1], int)
|
|
|
|
self.assertIsInstance(v[2], int)
|
|
|
|
self.assertIsInstance(v[3], int)
|
|
|
|
self.assertIsInstance(v[4], str)
|
|
|
|
self.assertRaises(IndexError, operator.getitem, v, 5)
|
|
|
|
self.assertIsInstance(v.major, int)
|
|
|
|
self.assertIsInstance(v.minor, int)
|
|
|
|
self.assertIsInstance(v.build, int)
|
|
|
|
self.assertIsInstance(v.platform, int)
|
|
|
|
self.assertIsInstance(v.service_pack, str)
|
2010-01-26 22:06:25 -04:00
|
|
|
self.assertIsInstance(v.service_pack_minor, int)
|
|
|
|
self.assertIsInstance(v.service_pack_major, int)
|
|
|
|
self.assertIsInstance(v.suite_mask, int)
|
|
|
|
self.assertIsInstance(v.product_type, int)
|
2010-01-26 21:21:15 -04:00
|
|
|
self.assertEqual(v[0], v.major)
|
|
|
|
self.assertEqual(v[1], v.minor)
|
|
|
|
self.assertEqual(v[2], v.build)
|
|
|
|
self.assertEqual(v[3], v.platform)
|
|
|
|
self.assertEqual(v[4], v.service_pack)
|
|
|
|
|
|
|
|
# This is how platform.py calls it. Make sure tuple
|
|
|
|
# still has 5 elements
|
|
|
|
maj, min, buildno, plat, csd = sys.getwindowsversion()
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(sys, "setdlopenflags"),
|
|
|
|
'test needs sys.setdlopenflags()')
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_dlopenflags(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.assertTrue(hasattr(sys, "getdlopenflags"))
|
|
|
|
self.assertRaises(TypeError, sys.getdlopenflags, 42)
|
|
|
|
oldflags = sys.getdlopenflags()
|
|
|
|
self.assertRaises(TypeError, sys.setdlopenflags)
|
|
|
|
sys.setdlopenflags(oldflags+1)
|
|
|
|
self.assertEqual(sys.getdlopenflags(), oldflags+1)
|
|
|
|
sys.setdlopenflags(oldflags)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
def test_refcount(self):
|
2009-03-31 21:04:33 -03:00
|
|
|
# n here must be a global in order for this test to pass while
|
|
|
|
# tracing with a python function. Tracing calls PyFrame_FastToLocals
|
|
|
|
# which will add a copy of any locals to the frame object, causing
|
|
|
|
# the reference count to increase by 2 instead of 1.
|
|
|
|
global n
|
2003-02-03 19:03:49 -04:00
|
|
|
self.assertRaises(TypeError, sys.getrefcount)
|
|
|
|
c = sys.getrefcount(None)
|
|
|
|
n = None
|
|
|
|
self.assertEqual(sys.getrefcount(None), c+1)
|
|
|
|
del n
|
|
|
|
self.assertEqual(sys.getrefcount(None), c)
|
|
|
|
if hasattr(sys, "gettotalrefcount"):
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.gettotalrefcount(), int)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
def test_getframe(self):
|
|
|
|
self.assertRaises(TypeError, sys._getframe, 42, 42)
|
2003-02-18 11:22:10 -04:00
|
|
|
self.assertRaises(ValueError, sys._getframe, 2000000000)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(
|
2003-02-03 19:03:49 -04:00
|
|
|
SysModuleTest.test_getframe.im_func.func_code \
|
|
|
|
is sys._getframe().f_code
|
|
|
|
)
|
|
|
|
|
2006-07-10 18:08:24 -03:00
|
|
|
# sys._current_frames() is a CPython-only gimmick.
|
|
|
|
def test_current_frames(self):
|
2006-07-18 21:03:19 -03:00
|
|
|
have_threads = True
|
|
|
|
try:
|
|
|
|
import thread
|
|
|
|
except ImportError:
|
|
|
|
have_threads = False
|
|
|
|
|
|
|
|
if have_threads:
|
|
|
|
self.current_frames_with_threads()
|
|
|
|
else:
|
|
|
|
self.current_frames_without_threads()
|
|
|
|
|
|
|
|
# Test sys._current_frames() in a WITH_THREADS build.
|
2009-10-27 16:23:56 -03:00
|
|
|
@test.test_support.reap_threads
|
2006-07-18 21:03:19 -03:00
|
|
|
def current_frames_with_threads(self):
|
2006-07-10 18:08:24 -03:00
|
|
|
import threading, thread
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
# Spawn a thread that blocks at a known place. Then the main
|
|
|
|
# thread does sys._current_frames(), and verifies that the frames
|
|
|
|
# returned make sense.
|
|
|
|
entered_g = threading.Event()
|
|
|
|
leave_g = threading.Event()
|
|
|
|
thread_info = [] # the thread's id
|
|
|
|
|
|
|
|
def f123():
|
|
|
|
g456()
|
|
|
|
|
|
|
|
def g456():
|
|
|
|
thread_info.append(thread.get_ident())
|
|
|
|
entered_g.set()
|
|
|
|
leave_g.wait()
|
|
|
|
|
|
|
|
t = threading.Thread(target=f123)
|
|
|
|
t.start()
|
|
|
|
entered_g.wait()
|
|
|
|
|
2006-07-25 01:07:22 -03:00
|
|
|
# At this point, t has finished its entered_g.set(), although it's
|
|
|
|
# impossible to guess whether it's still on that line or has moved on
|
|
|
|
# to its leave_g.wait().
|
2006-07-10 18:08:24 -03:00
|
|
|
self.assertEqual(len(thread_info), 1)
|
|
|
|
thread_id = thread_info[0]
|
|
|
|
|
|
|
|
d = sys._current_frames()
|
|
|
|
|
|
|
|
main_id = thread.get_ident()
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(main_id, d)
|
|
|
|
self.assertIn(thread_id, d)
|
2006-07-10 18:08:24 -03:00
|
|
|
|
|
|
|
# Verify that the captured main-thread frame is _this_ frame.
|
|
|
|
frame = d.pop(main_id)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(frame is sys._getframe())
|
2006-07-10 18:08:24 -03:00
|
|
|
|
|
|
|
# Verify that the captured thread frame is blocked in g456, called
|
|
|
|
# from f123. This is a litte tricky, since various bits of
|
|
|
|
# threading.py are also in the thread's call stack.
|
|
|
|
frame = d.pop(thread_id)
|
|
|
|
stack = traceback.extract_stack(frame)
|
|
|
|
for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
|
|
|
|
if funcname == "f123":
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("didn't find f123() on thread's call stack")
|
|
|
|
|
|
|
|
self.assertEqual(sourceline, "g456()")
|
|
|
|
|
|
|
|
# And the next record must be for g456().
|
|
|
|
filename, lineno, funcname, sourceline = stack[i+1]
|
|
|
|
self.assertEqual(funcname, "g456")
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"])
|
2006-07-10 18:08:24 -03:00
|
|
|
|
|
|
|
# Reap the spawned thread.
|
|
|
|
leave_g.set()
|
|
|
|
t.join()
|
|
|
|
|
2006-07-18 21:03:19 -03:00
|
|
|
# Test sys._current_frames() when thread support doesn't exist.
|
|
|
|
def current_frames_without_threads(self):
|
|
|
|
# Not much happens here: there is only one thread, with artificial
|
|
|
|
# "thread id" 0.
|
|
|
|
d = sys._current_frames()
|
|
|
|
self.assertEqual(len(d), 1)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(0, d)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(d[0] is sys._getframe())
|
2006-07-18 21:03:19 -03:00
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_attributes(self):
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.api_version, int)
|
|
|
|
self.assertIsInstance(sys.argv, list)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(sys.byteorder, ("little", "big"))
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.builtin_module_names, tuple)
|
|
|
|
self.assertIsInstance(sys.copyright, basestring)
|
|
|
|
self.assertIsInstance(sys.exec_prefix, basestring)
|
|
|
|
self.assertIsInstance(sys.executable, basestring)
|
2007-12-01 07:20:10 -04:00
|
|
|
self.assertEqual(len(sys.float_info), 11)
|
2008-01-14 00:13:37 -04:00
|
|
|
self.assertEqual(sys.float_info.radix, 2)
|
2009-03-20 12:51:55 -03:00
|
|
|
self.assertEqual(len(sys.long_info), 2)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
|
|
|
|
self.assertTrue(sys.long_info.sizeof_digit >= 1)
|
2009-04-02 15:39:37 -03:00
|
|
|
self.assertEqual(type(sys.long_info.bits_per_digit), int)
|
|
|
|
self.assertEqual(type(sys.long_info.sizeof_digit), int)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.hexversion, int)
|
|
|
|
self.assertIsInstance(sys.maxint, int)
|
2005-08-03 14:09:04 -03:00
|
|
|
if test.test_support.have_unicode:
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(sys.maxunicode, int)
|
|
|
|
self.assertIsInstance(sys.platform, basestring)
|
|
|
|
self.assertIsInstance(sys.prefix, basestring)
|
|
|
|
self.assertIsInstance(sys.version, basestring)
|
2003-02-03 19:03:49 -04:00
|
|
|
vi = sys.version_info
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(vi[:], tuple)
|
2003-02-03 19:03:49 -04:00
|
|
|
self.assertEqual(len(vi), 5)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(vi[0], int)
|
|
|
|
self.assertIsInstance(vi[1], int)
|
|
|
|
self.assertIsInstance(vi[2], int)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(vi[3], ("alpha", "beta", "candidate", "final"))
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(vi[4], int)
|
|
|
|
self.assertIsInstance(vi.major, int)
|
|
|
|
self.assertIsInstance(vi.minor, int)
|
|
|
|
self.assertIsInstance(vi.micro, int)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final"))
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(vi.serial, int)
|
2009-02-05 20:48:26 -04:00
|
|
|
self.assertEqual(vi[0], vi.major)
|
|
|
|
self.assertEqual(vi[1], vi.minor)
|
|
|
|
self.assertEqual(vi[2], vi.micro)
|
|
|
|
self.assertEqual(vi[3], vi.releaselevel)
|
|
|
|
self.assertEqual(vi[4], vi.serial)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(vi > (1,0,0))
|
2009-10-24 11:01:08 -03:00
|
|
|
self.assertIsInstance(sys.float_repr_style, str)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(sys.float_repr_style, ('short', 'legacy'))
|
2003-02-03 19:03:49 -04:00
|
|
|
|
2006-04-03 08:05:39 -03:00
|
|
|
def test_43581(self):
|
|
|
|
# Can't use sys.stdout, as this is a cStringIO object when
|
|
|
|
# the test runs under regrtest.
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(sys.__stdout__.encoding == sys.__stderr__.encoding)
|
2006-04-03 08:05:39 -03:00
|
|
|
|
2008-01-13 23:42:48 -04:00
|
|
|
def test_sys_flags(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(sys.flags)
|
2008-01-13 23:42:48 -04:00
|
|
|
attrs = ("debug", "py3k_warning", "division_warning", "division_new",
|
|
|
|
"inspect", "interactive", "optimize", "dont_write_bytecode",
|
2008-01-14 21:29:16 -04:00
|
|
|
"no_site", "ignore_environment", "tabcheck", "verbose",
|
2012-02-20 21:42:21 -04:00
|
|
|
"unicode", "bytes_warning", "hash_randomization")
|
2008-01-13 23:42:48 -04:00
|
|
|
for attr in attrs:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(sys.flags, attr), attr)
|
2008-01-13 23:42:48 -04:00
|
|
|
self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(repr(sys.flags))
|
2008-01-13 23:42:48 -04:00
|
|
|
|
2015-02-02 15:06:11 -04:00
|
|
|
@test.test_support.cpython_only
|
2008-02-04 14:00:12 -04:00
|
|
|
def test_clear_type_cache(self):
|
|
|
|
sys._clear_type_cache()
|
|
|
|
|
2008-06-01 04:20:46 -03:00
|
|
|
def test_ioencoding(self):
|
2010-02-07 13:03:15 -04:00
|
|
|
import subprocess
|
2008-06-01 04:20:46 -03:00
|
|
|
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)'],
|
|
|
|
stdout = subprocess.PIPE, env=env)
|
2009-10-27 16:23:56 -03:00
|
|
|
out = p.communicate()[0].strip()
|
2008-06-01 04:20:46 -03:00
|
|
|
self.assertEqual(out, unichr(0xa2).encode("cp424"))
|
|
|
|
|
|
|
|
env["PYTHONIOENCODING"] = "ascii:replace"
|
|
|
|
p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'],
|
|
|
|
stdout = subprocess.PIPE, env=env)
|
2009-10-27 16:23:56 -03:00
|
|
|
out = p.communicate()[0].strip()
|
2008-06-01 04:20:46 -03:00
|
|
|
self.assertEqual(out, '?')
|
|
|
|
|
2010-01-31 18:32:15 -04:00
|
|
|
def test_call_tracing(self):
|
|
|
|
self.assertEqual(sys.call_tracing(str, (2,)), "2")
|
|
|
|
self.assertRaises(TypeError, sys.call_tracing, str, 2)
|
|
|
|
|
2010-03-11 08:34:39 -04:00
|
|
|
def test_executable(self):
|
2012-02-02 15:11:28 -04:00
|
|
|
# sys.executable should be absolute
|
|
|
|
self.assertEqual(os.path.abspath(sys.executable), sys.executable)
|
|
|
|
|
2010-03-11 08:34:39 -04:00
|
|
|
# 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
|
2010-03-11 09:46:06 -04:00
|
|
|
# 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))
|
2010-03-11 08:34:39 -04:00
|
|
|
p = subprocess.Popen(
|
2010-03-11 09:27:35 -04:00
|
|
|
["nonexistent", "-c", 'import sys; print repr(sys.executable)'],
|
2010-03-11 09:46:06 -04:00
|
|
|
executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
|
2010-03-11 08:34:39 -04:00
|
|
|
executable = p.communicate()[0].strip()
|
|
|
|
p.wait()
|
2010-03-11 09:27:35 -04:00
|
|
|
self.assertIn(executable, ["''", repr(sys.executable)])
|
2008-06-01 04:20:46 -03:00
|
|
|
|
2014-02-07 04:06:05 -04:00
|
|
|
@test.test_support.cpython_only
|
2008-06-01 13:16:17 -03:00
|
|
|
class SizeofTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2012-07-29 11:30:50 -03:00
|
|
|
self.P = struct.calcsize('P')
|
2009-03-20 12:51:55 -03:00
|
|
|
self.longdigit = sys.long_info.sizeof_digit
|
2008-07-10 14:13:55 -03:00
|
|
|
import _testcapi
|
|
|
|
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
|
2008-06-01 13:16:17 -03:00
|
|
|
self.file = open(test.test_support.TESTFN, 'wb')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.file.close()
|
2008-06-01 13:42:16 -03:00
|
|
|
test.test_support.unlink(test.test_support.TESTFN)
|
2008-06-01 13:16:17 -03:00
|
|
|
|
2012-07-29 11:30:50 -03:00
|
|
|
check_sizeof = test.test_support.check_sizeof
|
2008-06-01 13:16:17 -03:00
|
|
|
|
2008-07-10 14:13:55 -03:00
|
|
|
def test_gc_head_size(self):
|
|
|
|
# Check that the gc header size is added to objects tracked by the gc.
|
2012-07-29 11:30:50 -03:00
|
|
|
size = test.test_support.calcobjsize
|
2008-07-10 14:13:55 -03:00
|
|
|
gc_header_size = self.gc_headsize
|
|
|
|
# bool objects are not gc tracked
|
2012-07-29 11:30:50 -03:00
|
|
|
self.assertEqual(sys.getsizeof(True), size('l'))
|
2008-07-10 14:13:55 -03:00
|
|
|
# but lists are
|
2012-07-29 11:30:50 -03:00
|
|
|
self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)
|
2008-07-10 14:13:55 -03:00
|
|
|
|
2014-11-15 07:21:01 -04:00
|
|
|
def test_errors(self):
|
|
|
|
class BadSizeof(object):
|
|
|
|
def __sizeof__(self):
|
|
|
|
raise ValueError
|
|
|
|
self.assertRaises(ValueError, sys.getsizeof, BadSizeof())
|
|
|
|
|
|
|
|
class InvalidSizeof(object):
|
|
|
|
def __sizeof__(self):
|
|
|
|
return None
|
|
|
|
self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof())
|
|
|
|
sentinel = ["sentinel"]
|
|
|
|
self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel)
|
|
|
|
|
2014-11-23 15:17:57 -04:00
|
|
|
class OverflowSizeof(long):
|
2014-11-15 07:21:01 -04:00
|
|
|
def __sizeof__(self):
|
|
|
|
return int(self)
|
|
|
|
self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)),
|
|
|
|
sys.maxsize + self.gc_headsize)
|
|
|
|
with self.assertRaises(OverflowError):
|
|
|
|
sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
sys.getsizeof(OverflowSizeof(-1))
|
|
|
|
with self.assertRaises((ValueError, OverflowError)):
|
|
|
|
sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))
|
|
|
|
|
2008-07-10 14:13:55 -03:00
|
|
|
def test_default(self):
|
2012-07-29 11:30:50 -03:00
|
|
|
size = test.test_support.calcobjsize
|
|
|
|
self.assertEqual(sys.getsizeof(True, -1), size('l'))
|
2008-07-10 14:13:55 -03:00
|
|
|
|
2008-07-10 10:43:26 -03:00
|
|
|
def test_objecttypes(self):
|
|
|
|
# check all types defined in Objects/
|
2012-07-29 11:30:50 -03:00
|
|
|
size = test.test_support.calcobjsize
|
|
|
|
vsize = test.test_support.calcvobjsize
|
2008-07-10 10:43:26 -03:00
|
|
|
check = self.check_sizeof
|
2008-06-01 13:16:17 -03:00
|
|
|
# bool
|
2012-07-29 11:30:50 -03:00
|
|
|
check(True, size('l'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# buffer
|
2010-03-17 10:52:48 -03:00
|
|
|
with test.test_support.check_py3k_warnings():
|
2012-07-29 11:30:50 -03:00
|
|
|
check(buffer(''), size('2P2Pil'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# builtin_function_or_method
|
2012-07-29 11:30:50 -03:00
|
|
|
check(len, size('3P'))
|
2008-07-10 12:24:04 -03:00
|
|
|
# bytearray
|
|
|
|
samples = ['', 'u'*100000]
|
|
|
|
for sample in samples:
|
|
|
|
x = bytearray(sample)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(x, vsize('iPP') + x.__alloc__())
|
2008-07-10 12:24:04 -03:00
|
|
|
# bytearray_iterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter(bytearray()), size('PP'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# cell
|
|
|
|
def get_cell():
|
|
|
|
x = 42
|
|
|
|
def inner():
|
|
|
|
return x
|
|
|
|
return inner
|
2012-07-29 11:30:50 -03:00
|
|
|
check(get_cell().func_closure[0], size('P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# classobj (old-style class)
|
2008-06-01 13:16:17 -03:00
|
|
|
class class_oldstyle():
|
|
|
|
def method():
|
|
|
|
pass
|
2012-07-29 11:30:50 -03:00
|
|
|
check(class_oldstyle, size('7P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# instance (old-style class)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(class_oldstyle(), size('3P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# instancemethod (old-style class)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(class_oldstyle().method, size('4P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# complex
|
2012-07-29 11:30:50 -03:00
|
|
|
check(complex(0,1), size('2d'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# code
|
2012-07-29 11:30:50 -03:00
|
|
|
check(get_cell().func_code, size('4i8Pi3P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# BaseException
|
2012-07-29 11:30:50 -03:00
|
|
|
check(BaseException(), size('3P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# UnicodeEncodeError
|
2012-07-29 11:30:50 -03:00
|
|
|
check(UnicodeEncodeError("", u"", 0, 0, ""), size('5P2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# UnicodeDecodeError
|
2012-07-29 11:30:50 -03:00
|
|
|
check(UnicodeDecodeError("", "", 0, 0, ""), size('5P2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# UnicodeTranslateError
|
2012-07-29 11:30:50 -03:00
|
|
|
check(UnicodeTranslateError(u"", 0, 1, ""), size('5P2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# method_descriptor (descriptor object)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(str.lower, size('2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# classmethod_descriptor (descriptor object)
|
|
|
|
# XXX
|
|
|
|
# member_descriptor (descriptor object)
|
|
|
|
import datetime
|
2012-07-29 11:30:50 -03:00
|
|
|
check(datetime.timedelta.days, size('2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# getset_descriptor (descriptor object)
|
|
|
|
import __builtin__
|
2012-07-29 11:30:50 -03:00
|
|
|
check(__builtin__.file.closed, size('2PP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# wrapper_descriptor (descriptor object)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(int.__add__, size('2P2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dictproxy
|
|
|
|
class C(object): pass
|
2012-07-29 11:30:50 -03:00
|
|
|
check(C.__dict__, size('P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# method-wrapper (descriptor object)
|
2012-07-29 11:30:50 -03:00
|
|
|
check({}.__iter__, size('2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dict
|
2012-07-29 11:30:50 -03:00
|
|
|
check({}, size('3P2P' + 8*'P2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
|
2012-07-29 11:30:50 -03:00
|
|
|
check(x, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dictionary-keyiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check({}.iterkeys(), size('P2PPP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dictionary-valueiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check({}.itervalues(), size('P2PPP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dictionary-itemiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check({}.iteritems(), size('P2PPP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# ellipses
|
2012-07-29 11:30:50 -03:00
|
|
|
check(Ellipsis, size(''))
|
2008-07-10 10:43:26 -03:00
|
|
|
# EncodingMap
|
|
|
|
import codecs, encodings.iso8859_3
|
|
|
|
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(x, size('32B2iB'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# enumerate
|
2012-07-29 11:30:50 -03:00
|
|
|
check(enumerate([]), size('l3P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# file
|
2012-07-29 11:30:50 -03:00
|
|
|
check(self.file, size('4P2i4P3i3P3i'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# float
|
2012-07-29 11:30:50 -03:00
|
|
|
check(float(0), size('d'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# sys.floatinfo
|
2012-07-29 11:30:50 -03:00
|
|
|
check(sys.float_info, vsize('') + self.P * len(sys.float_info))
|
2008-07-10 12:24:04 -03:00
|
|
|
# frame
|
|
|
|
import inspect
|
|
|
|
CO_MAXBLOCKS = 20
|
|
|
|
x = inspect.currentframe()
|
|
|
|
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
|
2012-07-29 11:30:50 -03:00
|
|
|
check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# function
|
|
|
|
def func(): pass
|
2012-07-29 11:30:50 -03:00
|
|
|
check(func, size('9P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
class c():
|
|
|
|
@staticmethod
|
|
|
|
def foo():
|
|
|
|
pass
|
|
|
|
@classmethod
|
|
|
|
def bar(cls):
|
|
|
|
pass
|
|
|
|
# staticmethod
|
2012-07-29 11:30:50 -03:00
|
|
|
check(foo, size('P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# classmethod
|
2012-07-29 11:30:50 -03:00
|
|
|
check(bar, size('P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# generator
|
|
|
|
def get_gen(): yield 1
|
2012-07-29 11:30:50 -03:00
|
|
|
check(get_gen(), size('Pi2P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# integer
|
2012-07-29 11:30:50 -03:00
|
|
|
check(1, size('l'))
|
|
|
|
check(100, size('l'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# iterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter('abc'), size('lP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# callable-iterator
|
|
|
|
import re
|
2012-07-29 11:30:50 -03:00
|
|
|
check(re.finditer('',''), size('2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# list
|
|
|
|
samples = [[], [1,2,3], ['1', '2', '3']]
|
|
|
|
for sample in samples:
|
2012-07-29 11:30:50 -03:00
|
|
|
check(sample, vsize('PP') + len(sample)*self.P)
|
2008-07-10 10:43:26 -03:00
|
|
|
# sortwrapper (list)
|
|
|
|
# XXX
|
|
|
|
# cmpwrapper (list)
|
|
|
|
# XXX
|
|
|
|
# listiterator (list)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter([]), size('lP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# listreverseiterator (list)
|
2012-07-29 11:30:50 -03:00
|
|
|
check(reversed([]), size('lP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# long
|
2012-07-29 11:30:50 -03:00
|
|
|
check(0L, vsize(''))
|
|
|
|
check(1L, vsize('') + self.longdigit)
|
|
|
|
check(-1L, vsize('') + self.longdigit)
|
2009-03-20 12:51:55 -03:00
|
|
|
PyLong_BASE = 2**sys.long_info.bits_per_digit
|
2012-07-29 11:30:50 -03:00
|
|
|
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)
|
2008-06-01 13:16:17 -03:00
|
|
|
# module
|
2012-07-29 11:30:50 -03:00
|
|
|
check(unittest, size('P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# None
|
2012-07-29 11:30:50 -03:00
|
|
|
check(None, size(''))
|
2008-07-10 10:43:26 -03:00
|
|
|
# object
|
2012-07-29 11:30:50 -03:00
|
|
|
check(object(), size(''))
|
2008-07-10 10:43:26 -03:00
|
|
|
# property (descriptor object)
|
|
|
|
class C(object):
|
|
|
|
def getx(self): return self.__x
|
|
|
|
def setx(self, value): self.__x = value
|
|
|
|
def delx(self): del self.__x
|
|
|
|
x = property(getx, setx, delx, "")
|
2012-07-29 11:30:50 -03:00
|
|
|
check(x, size('4Pi'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# PyCObject
|
2010-03-24 21:54:54 -03:00
|
|
|
# PyCapsule
|
2008-07-10 10:43:26 -03:00
|
|
|
# XXX
|
|
|
|
# rangeiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter(xrange(1)), size('4l'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# reverse
|
2012-07-29 11:30:50 -03:00
|
|
|
check(reversed(''), size('PP'))
|
2008-07-10 12:24:04 -03:00
|
|
|
# set
|
|
|
|
# frozenset
|
|
|
|
PySet_MINSIZE = 8
|
|
|
|
samples = [[], range(10), range(50)]
|
2012-07-29 11:30:50 -03:00
|
|
|
s = size('3P2P' + PySet_MINSIZE*'lP' + 'lP')
|
2008-07-10 12:24:04 -03:00
|
|
|
for sample in samples:
|
|
|
|
minused = len(sample)
|
|
|
|
if minused == 0: tmp = 1
|
|
|
|
# the computation of minused is actually a bit more complicated
|
|
|
|
# but this suffices for the sizeof test
|
|
|
|
minused = minused*2
|
|
|
|
newsize = PySet_MINSIZE
|
|
|
|
while newsize <= minused:
|
|
|
|
newsize = newsize << 1
|
|
|
|
if newsize <= 8:
|
|
|
|
check(set(sample), s)
|
|
|
|
check(frozenset(sample), s)
|
|
|
|
else:
|
|
|
|
check(set(sample), s + newsize*struct.calcsize('lP'))
|
|
|
|
check(frozenset(sample), s + newsize*struct.calcsize('lP'))
|
|
|
|
# setiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter(set()), size('P3P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# slice
|
2012-07-29 11:30:50 -03:00
|
|
|
check(slice(1), size('3P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# str
|
2012-07-29 11:30:50 -03:00
|
|
|
vh = test.test_support._vheader
|
|
|
|
check('', struct.calcsize(vh + 'lic'))
|
|
|
|
check('abc', struct.calcsize(vh + 'lic') + 3)
|
2008-07-10 10:43:26 -03:00
|
|
|
# super
|
2012-07-29 11:30:50 -03:00
|
|
|
check(super(int), size('3P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# tuple
|
2012-07-29 11:30:50 -03:00
|
|
|
check((), vsize(''))
|
|
|
|
check((1,2,3), vsize('') + 3*self.P)
|
2008-07-10 10:43:26 -03:00
|
|
|
# tupleiterator
|
2012-07-29 11:30:50 -03:00
|
|
|
check(iter(()), size('lP'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# type
|
|
|
|
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
|
|
|
|
# PySequenceMethods + PyBufferProcs)
|
2012-07-29 11:30:50 -03:00
|
|
|
s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('41P 10P 3P 6P')
|
2008-07-10 10:43:26 -03:00
|
|
|
class newstyleclass(object):
|
|
|
|
pass
|
|
|
|
check(newstyleclass, s)
|
|
|
|
# builtin type
|
|
|
|
check(int, s)
|
|
|
|
# NotImplementedType
|
|
|
|
import types
|
|
|
|
check(types.NotImplementedType, s)
|
2008-06-10 07:10:31 -03:00
|
|
|
# unicode
|
2008-06-17 05:42:15 -03:00
|
|
|
usize = len(u'\0'.encode('unicode-internal'))
|
2008-06-10 07:10:31 -03:00
|
|
|
samples = [u'', u'1'*100]
|
|
|
|
# we need to test for both sizes, because we don't know if the string
|
|
|
|
# has been cached
|
|
|
|
for s in samples:
|
2012-07-29 11:30:50 -03:00
|
|
|
check(s, size('PPlP') + usize * (len(s) + 1))
|
2008-07-10 10:43:26 -03:00
|
|
|
# weakref
|
|
|
|
import weakref
|
2012-07-29 11:30:50 -03:00
|
|
|
check(weakref.ref(int), size('2Pl2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# weakproxy
|
|
|
|
# XXX
|
|
|
|
# weakcallableproxy
|
2012-07-29 11:30:50 -03:00
|
|
|
check(weakref.proxy(int), size('2Pl2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# xrange
|
2012-07-29 11:30:50 -03:00
|
|
|
check(xrange(1), size('3l'))
|
|
|
|
check(xrange(66000), size('3l'))
|
2008-06-01 13:16:17 -03:00
|
|
|
|
2008-07-10 10:43:26 -03:00
|
|
|
def test_pythontypes(self):
|
|
|
|
# check all types defined in Python/
|
2012-07-29 11:30:50 -03:00
|
|
|
size = test.test_support.calcobjsize
|
|
|
|
vsize = test.test_support.calcvobjsize
|
2008-07-10 10:43:26 -03:00
|
|
|
check = self.check_sizeof
|
|
|
|
# _ast.AST
|
|
|
|
import _ast
|
2012-07-29 11:30:50 -03:00
|
|
|
check(_ast.AST(), size(''))
|
2008-07-10 10:43:26 -03:00
|
|
|
# imp.NullImporter
|
|
|
|
import imp
|
2012-07-29 11:30:50 -03:00
|
|
|
check(imp.NullImporter(self.file.name), size(''))
|
2008-07-10 10:43:26 -03:00
|
|
|
try:
|
|
|
|
raise TypeError
|
|
|
|
except TypeError:
|
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
# traceback
|
|
|
|
if tb != None:
|
2012-07-29 11:30:50 -03:00
|
|
|
check(tb, size('2P2i'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# symtable entry
|
|
|
|
# XXX
|
|
|
|
# sys.flags
|
2012-07-29 11:30:50 -03:00
|
|
|
check(sys.flags, vsize('') + self.P * len(sys.flags))
|
2008-06-01 13:16:17 -03:00
|
|
|
|
|
|
|
|
2003-02-03 19:03:49 -04:00
|
|
|
def test_main():
|
2008-06-01 13:16:17 -03:00
|
|
|
test_classes = (SysModuleTest, SizeofTest)
|
|
|
|
|
|
|
|
test.test_support.run_unittest(*test_classes)
|
2003-02-03 19:03:49 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|