2003-02-03 19:03:49 -04:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
import unittest, test.test_support
|
2008-06-01 13:16:17 -03:00
|
|
|
import sys, cStringIO, os
|
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-01-08 15:04:16 -04:00
|
|
|
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):
|
|
|
|
self.assertRaises(TypeError, sys.exit, 42, 42)
|
|
|
|
|
|
|
|
# call without argument
|
|
|
|
try:
|
|
|
|
sys.exit(0)
|
|
|
|
except SystemExit, exc:
|
|
|
|
self.assertEquals(exc.code, 0)
|
|
|
|
except:
|
|
|
|
self.fail("wrong exception")
|
|
|
|
else:
|
|
|
|
self.fail("no exception")
|
|
|
|
|
|
|
|
# call with tuple argument with one entry
|
|
|
|
# entry will be unpacked
|
|
|
|
try:
|
|
|
|
sys.exit(42)
|
|
|
|
except SystemExit, exc:
|
|
|
|
self.assertEquals(exc.code, 42)
|
|
|
|
except:
|
|
|
|
self.fail("wrong exception")
|
|
|
|
else:
|
|
|
|
self.fail("no exception")
|
|
|
|
|
|
|
|
# call with integer argument
|
|
|
|
try:
|
|
|
|
sys.exit((42,))
|
|
|
|
except SystemExit, exc:
|
|
|
|
self.assertEquals(exc.code, 42)
|
|
|
|
except:
|
|
|
|
self.fail("wrong exception")
|
|
|
|
else:
|
|
|
|
self.fail("no exception")
|
|
|
|
|
|
|
|
# call with string argument
|
|
|
|
try:
|
|
|
|
sys.exit("exit")
|
|
|
|
except SystemExit, exc:
|
|
|
|
self.assertEquals(exc.code, "exit")
|
|
|
|
except:
|
|
|
|
self.fail("wrong exception")
|
|
|
|
else:
|
|
|
|
self.fail("no exception")
|
|
|
|
|
|
|
|
# call with tuple argument with two entries
|
|
|
|
try:
|
|
|
|
sys.exit((17, 23))
|
|
|
|
except SystemExit, exc:
|
|
|
|
self.assertEquals(exc.code, (17, 23))
|
|
|
|
except:
|
|
|
|
self.fail("wrong exception")
|
|
|
|
else:
|
|
|
|
self.fail("no exception")
|
|
|
|
|
2005-02-15 11:26:11 -04:00
|
|
|
# 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)
|
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
|
|
|
|
|
|
|
# testing sys.settrace() is done in test_trace.py
|
|
|
|
# testing sys.setprofile() is done in test_profile.py
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.assertEquals(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
|
|
|
|
|
|
|
def test_getwindowsversion(self):
|
|
|
|
if hasattr(sys, "getwindowsversion"):
|
|
|
|
v = sys.getwindowsversion()
|
|
|
|
self.assertEqual(len(v), 5)
|
2010-01-24 12:58:36 -04:00
|
|
|
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)
|
2010-01-26 20:28:29 -04:00
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
def test_dlopenflags(self):
|
|
|
|
if hasattr(sys, "setdlopenflags"):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(sys, "getdlopenflags"))
|
2003-02-03 19:03:49 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
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",
|
2008-05-08 17:23:06 -03:00
|
|
|
"unicode", "bytes_warning")
|
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
|
|
|
|
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):
|
|
|
|
import subprocess,os
|
|
|
|
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, '?')
|
|
|
|
|
|
|
|
|
2008-06-01 13:16:17 -03:00
|
|
|
class SizeofTest(unittest.TestCase):
|
|
|
|
|
2008-07-10 14:13:55 -03:00
|
|
|
TPFLAGS_HAVE_GC = 1<<14
|
|
|
|
TPFLAGS_HEAPTYPE = 1L<<9
|
|
|
|
|
2008-06-01 13:16:17 -03:00
|
|
|
def setUp(self):
|
2008-06-25 06:20:03 -03:00
|
|
|
self.c = len(struct.pack('c', ' '))
|
|
|
|
self.H = len(struct.pack('H', 0))
|
2008-06-01 13:16:17 -03:00
|
|
|
self.i = len(struct.pack('i', 0))
|
|
|
|
self.l = len(struct.pack('l', 0))
|
2008-06-25 06:20:03 -03:00
|
|
|
self.P = len(struct.pack('P', 0))
|
|
|
|
# due to missing size_t information from struct, it is assumed that
|
|
|
|
# sizeof(Py_ssize_t) = sizeof(void*)
|
2008-06-26 12:20:35 -03:00
|
|
|
self.header = 'PP'
|
2008-07-10 10:43:26 -03:00
|
|
|
self.vheader = self.header + 'P'
|
2008-06-01 13:16:17 -03:00
|
|
|
if hasattr(sys, "gettotalrefcount"):
|
2008-06-25 06:20:03 -03:00
|
|
|
self.header += '2P'
|
2008-07-10 10:43:26 -03:00
|
|
|
self.vheader += '2P'
|
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
|
|
|
|
2008-07-10 12:24:04 -03:00
|
|
|
def check_sizeof(self, o, size):
|
2008-06-01 13:16:17 -03:00
|
|
|
result = sys.getsizeof(o)
|
2008-07-10 14:13:55 -03:00
|
|
|
if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
|
|
|
|
((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
|
|
|
|
size += self.gc_headsize
|
2008-07-10 12:24:04 -03:00
|
|
|
msg = 'wrong size for %s: got %d, expected %d' \
|
|
|
|
% (type(o), result, size)
|
|
|
|
self.assertEqual(result, size, msg)
|
2008-06-01 13:16:17 -03:00
|
|
|
|
2008-06-25 06:20:03 -03:00
|
|
|
def calcsize(self, fmt):
|
|
|
|
"""Wrapper around struct.calcsize which enforces the alignment of the
|
|
|
|
end of a structure to the alignment requirement of pointer.
|
2008-06-01 13:16:17 -03:00
|
|
|
|
2008-06-25 06:20:03 -03:00
|
|
|
Note: This wrapper should only be used if a pointer member is included
|
|
|
|
and no member with a size larger than a pointer exists.
|
|
|
|
"""
|
|
|
|
return struct.calcsize(fmt + '0P')
|
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.
|
|
|
|
h = self.header
|
|
|
|
size = self.calcsize
|
|
|
|
gc_header_size = self.gc_headsize
|
|
|
|
# bool objects are not gc tracked
|
|
|
|
self.assertEqual(sys.getsizeof(True), size(h + 'l'))
|
|
|
|
# but lists are
|
|
|
|
self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)
|
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
h = self.header
|
|
|
|
size = self.calcsize
|
|
|
|
self.assertEqual(sys.getsizeof(True, -1), size(h + 'l'))
|
|
|
|
|
2008-07-10 10:43:26 -03:00
|
|
|
def test_objecttypes(self):
|
|
|
|
# check all types defined in Objects/
|
2008-06-25 06:20:03 -03:00
|
|
|
h = self.header
|
2008-07-10 10:43:26 -03:00
|
|
|
vh = self.vheader
|
2008-06-25 06:20:03 -03:00
|
|
|
size = self.calcsize
|
2008-07-10 10:43:26 -03:00
|
|
|
check = self.check_sizeof
|
2008-06-01 13:16:17 -03:00
|
|
|
# bool
|
2008-07-10 10:43:26 -03:00
|
|
|
check(True, size(h + 'l'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# buffer
|
2010-01-08 15:04:16 -04:00
|
|
|
check(buffer(''), size(h + '2P2Pil'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# builtin_function_or_method
|
|
|
|
check(len, size(h + '3P'))
|
2008-07-10 12:24:04 -03:00
|
|
|
# bytearray
|
|
|
|
samples = ['', 'u'*100000]
|
|
|
|
for sample in samples:
|
|
|
|
x = bytearray(sample)
|
|
|
|
check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
|
|
|
|
# bytearray_iterator
|
|
|
|
check(iter(bytearray()), size(h + 'PP'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# cell
|
|
|
|
def get_cell():
|
|
|
|
x = 42
|
|
|
|
def inner():
|
|
|
|
return x
|
|
|
|
return inner
|
2008-07-10 10:43:26 -03:00
|
|
|
check(get_cell().func_closure[0], size(h + 'P'))
|
|
|
|
# classobj (old-style class)
|
2008-06-01 13:16:17 -03:00
|
|
|
class class_oldstyle():
|
|
|
|
def method():
|
|
|
|
pass
|
2008-07-10 10:43:26 -03:00
|
|
|
check(class_oldstyle, size(h + '6P'))
|
|
|
|
# instance (old-style class)
|
|
|
|
check(class_oldstyle(), size(h + '3P'))
|
|
|
|
# instancemethod (old-style class)
|
|
|
|
check(class_oldstyle().method, size(h + '4P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# complex
|
2008-07-10 10:43:26 -03:00
|
|
|
check(complex(0,1), size(h + '2d'))
|
|
|
|
# code
|
|
|
|
check(get_cell().func_code, size(h + '4i8Pi2P'))
|
|
|
|
# BaseException
|
|
|
|
check(BaseException(), size(h + '3P'))
|
|
|
|
# UnicodeEncodeError
|
|
|
|
check(UnicodeEncodeError("", u"", 0, 0, ""), size(h + '5P2PP'))
|
|
|
|
# UnicodeDecodeError
|
|
|
|
check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
|
|
|
|
# UnicodeTranslateError
|
|
|
|
check(UnicodeTranslateError(u"", 0, 1, ""), size(h + '5P2PP'))
|
|
|
|
# method_descriptor (descriptor object)
|
|
|
|
check(str.lower, size(h + '2PP'))
|
|
|
|
# classmethod_descriptor (descriptor object)
|
|
|
|
# XXX
|
|
|
|
# member_descriptor (descriptor object)
|
|
|
|
import datetime
|
|
|
|
check(datetime.timedelta.days, size(h + '2PP'))
|
|
|
|
# getset_descriptor (descriptor object)
|
|
|
|
import __builtin__
|
|
|
|
check(__builtin__.file.closed, size(h + '2PP'))
|
|
|
|
# wrapper_descriptor (descriptor object)
|
|
|
|
check(int.__add__, size(h + '2P2P'))
|
|
|
|
# dictproxy
|
|
|
|
class C(object): pass
|
|
|
|
check(C.__dict__, size(h + 'P'))
|
|
|
|
# method-wrapper (descriptor object)
|
|
|
|
check({}.__iter__, size(h + '2P'))
|
|
|
|
# dict
|
2008-07-14 05:42:18 -03:00
|
|
|
check({}, size(h + '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}
|
2008-07-14 05:42:18 -03:00
|
|
|
check(x, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
|
2008-07-10 10:43:26 -03:00
|
|
|
# dictionary-keyiterator
|
|
|
|
check({}.iterkeys(), size(h + 'P2PPP'))
|
|
|
|
# dictionary-valueiterator
|
|
|
|
check({}.itervalues(), size(h + 'P2PPP'))
|
|
|
|
# dictionary-itemiterator
|
|
|
|
check({}.iteritems(), size(h + 'P2PPP'))
|
|
|
|
# ellipses
|
|
|
|
check(Ellipsis, size(h + ''))
|
|
|
|
# EncodingMap
|
|
|
|
import codecs, encodings.iso8859_3
|
|
|
|
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
|
|
|
|
check(x, size(h + '32B2iB'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# enumerate
|
2008-07-10 10:43:26 -03:00
|
|
|
check(enumerate([]), size(h + 'l3P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# file
|
2008-07-10 10:43:26 -03:00
|
|
|
check(self.file, size(h + '4P2i4P3i3Pi'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# float
|
2008-07-10 10:43:26 -03:00
|
|
|
check(float(0), size(h + 'd'))
|
|
|
|
# sys.floatinfo
|
|
|
|
check(sys.float_info, size(vh) + 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
|
2008-07-14 05:42:18 -03:00
|
|
|
check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# function
|
|
|
|
def func(): pass
|
2008-07-10 10:43:26 -03:00
|
|
|
check(func, size(h + '9P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
class c():
|
|
|
|
@staticmethod
|
|
|
|
def foo():
|
|
|
|
pass
|
|
|
|
@classmethod
|
|
|
|
def bar(cls):
|
|
|
|
pass
|
|
|
|
# staticmethod
|
2008-07-10 10:43:26 -03:00
|
|
|
check(foo, size(h + 'P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# classmethod
|
2008-07-10 10:43:26 -03:00
|
|
|
check(bar, size(h + 'P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# generator
|
|
|
|
def get_gen(): yield 1
|
2008-07-10 10:43:26 -03:00
|
|
|
check(get_gen(), size(h + 'Pi2P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# integer
|
2008-07-10 10:43:26 -03:00
|
|
|
check(1, size(h + 'l'))
|
|
|
|
check(100, size(h + 'l'))
|
|
|
|
# iterator
|
|
|
|
check(iter('abc'), size(h + 'lP'))
|
|
|
|
# callable-iterator
|
|
|
|
import re
|
|
|
|
check(re.finditer('',''), size(h + '2P'))
|
|
|
|
# list
|
|
|
|
samples = [[], [1,2,3], ['1', '2', '3']]
|
|
|
|
for sample in samples:
|
|
|
|
check(sample, size(vh + 'PP') + len(sample)*self.P)
|
|
|
|
# sortwrapper (list)
|
|
|
|
# XXX
|
|
|
|
# cmpwrapper (list)
|
|
|
|
# XXX
|
|
|
|
# listiterator (list)
|
|
|
|
check(iter([]), size(h + 'lP'))
|
|
|
|
# listreverseiterator (list)
|
|
|
|
check(reversed([]), size(h + 'lP'))
|
|
|
|
# long
|
2009-02-15 06:13:41 -04:00
|
|
|
check(0L, size(vh))
|
2009-03-20 12:51:55 -03:00
|
|
|
check(1L, size(vh) + self.longdigit)
|
|
|
|
check(-1L, size(vh) + self.longdigit)
|
|
|
|
PyLong_BASE = 2**sys.long_info.bits_per_digit
|
2009-04-02 15:39:37 -03:00
|
|
|
check(long(PyLong_BASE), size(vh) + 2*self.longdigit)
|
|
|
|
check(long(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
|
|
|
|
check(long(PyLong_BASE**2), size(vh) + 3*self.longdigit)
|
2008-06-01 13:16:17 -03:00
|
|
|
# module
|
2008-07-10 10:43:26 -03:00
|
|
|
check(unittest, size(h + 'P'))
|
|
|
|
# None
|
|
|
|
check(None, size(h + ''))
|
|
|
|
# object
|
|
|
|
check(object(), size(h + ''))
|
|
|
|
# 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, "")
|
|
|
|
check(x, size(h + '4Pi'))
|
|
|
|
# PyCObject
|
|
|
|
# XXX
|
|
|
|
# rangeiterator
|
|
|
|
check(iter(xrange(1)), size(h + '4l'))
|
|
|
|
# reverse
|
|
|
|
check(reversed(''), size(h + 'PP'))
|
2008-07-10 12:24:04 -03:00
|
|
|
# set
|
|
|
|
# frozenset
|
|
|
|
PySet_MINSIZE = 8
|
|
|
|
samples = [[], range(10), range(50)]
|
2008-07-14 05:42:18 -03:00
|
|
|
s = size(h + '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
|
|
|
|
check(iter(set()), size(h + 'P3P'))
|
2008-06-01 13:16:17 -03:00
|
|
|
# slice
|
2008-07-10 10:43:26 -03:00
|
|
|
check(slice(1), size(h + '3P'))
|
|
|
|
# str
|
2008-12-05 17:55:28 -04:00
|
|
|
check('', struct.calcsize(vh + 'li') + 1)
|
|
|
|
check('abc', struct.calcsize(vh + 'li') + 1 + 3*self.c)
|
2008-07-10 10:43:26 -03:00
|
|
|
# super
|
|
|
|
check(super(int), size(h + '3P'))
|
|
|
|
# tuple
|
|
|
|
check((), size(vh))
|
|
|
|
check((1,2,3), size(vh) + 3*self.P)
|
|
|
|
# tupleiterator
|
|
|
|
check(iter(()), size(h + 'lP'))
|
|
|
|
# type
|
|
|
|
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
|
|
|
|
# PySequenceMethods + PyBufferProcs)
|
2008-07-14 05:42:18 -03:00
|
|
|
s = size(vh + 'P2P15Pl4PP9PP11PI') + size('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:
|
2008-07-10 12:24:04 -03:00
|
|
|
check(s, size(h + 'PPlP') + usize * (len(s) + 1))
|
2008-07-10 10:43:26 -03:00
|
|
|
# weakref
|
|
|
|
import weakref
|
|
|
|
check(weakref.ref(int), size(h + '2Pl2P'))
|
|
|
|
# weakproxy
|
|
|
|
# XXX
|
|
|
|
# weakcallableproxy
|
|
|
|
check(weakref.proxy(int), size(h + '2Pl2P'))
|
|
|
|
# xrange
|
|
|
|
check(xrange(1), size(h + '3l'))
|
|
|
|
check(xrange(66000), size(h + '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/
|
|
|
|
h = self.header
|
|
|
|
vh = self.vheader
|
|
|
|
size = self.calcsize
|
|
|
|
check = self.check_sizeof
|
|
|
|
# _ast.AST
|
|
|
|
import _ast
|
|
|
|
check(_ast.AST(), size(h + ''))
|
|
|
|
# imp.NullImporter
|
|
|
|
import imp
|
|
|
|
check(imp.NullImporter(self.file.name), size(h + ''))
|
|
|
|
try:
|
|
|
|
raise TypeError
|
|
|
|
except TypeError:
|
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
# traceback
|
|
|
|
if tb != None:
|
|
|
|
check(tb, size(h + '2P2i'))
|
|
|
|
# symtable entry
|
|
|
|
# XXX
|
|
|
|
# sys.flags
|
|
|
|
check(sys.flags, size(vh) + 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()
|