cpython/Lib/test/test_structseq.py
Christian Heimes d32ed6f511 Merged revisions 59933-59951 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r59935 | raymond.hettinger | 2008-01-13 07:15:15 +0100 (Sun, 13 Jan 2008) | 1 line

  Named tuple is a concept, not a specific type.
........
  r59936 | raymond.hettinger | 2008-01-13 07:18:07 +0100 (Sun, 13 Jan 2008) | 1 line

  Fix spelling.
........
  r59937 | georg.brandl | 2008-01-13 10:36:18 +0100 (Sun, 13 Jan 2008) | 2 lines

  Clarify the effect of text mode.
........
  r59938 | thomas.heller | 2008-01-13 12:19:43 +0100 (Sun, 13 Jan 2008) | 1 line

  Make Modules/socketobject.c compile for Windows again.
........
  r59939 | ka-ping.yee | 2008-01-13 12:25:13 +0100 (Sun, 13 Jan 2008) | 9 lines

  Check in the patch proposed by Ben Hayden (benjhayden) for issue
  #1550: help('modules') broken by several 3rd party libraries.

  Tested with Python build: trunk:54235:59936M -- the reported error
  occurs with Django installed (or with any __init__.py present on
  the path that raises an exception), and such errors indeed go away
  when this change is applied.
........
  r59940 | georg.brandl | 2008-01-13 16:04:05 +0100 (Sun, 13 Jan 2008) | 2 lines

  Back out r59931 - test_ctypes fails with it.
........
  r59943 | amaury.forgeotdarc | 2008-01-14 01:22:44 +0100 (Mon, 14 Jan 2008) | 6 lines

  As discussed in issue 1700288:
  ctypes takes some liberties when creating python types: it modifies the types'
  __dict__ directly, bypassing all the machinery of type objects which deal with
  special methods.  And this broke recent optimisations of method lookup.
  Now we try to modify the type with more "official" functions.
........
  r59944 | amaury.forgeotdarc | 2008-01-14 01:29:41 +0100 (Mon, 14 Jan 2008) | 5 lines

  Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
  now that ctypes uses a more supported method to create types:

  Method cache optimization, by Armin Rigo, ported to 2.6 by Kevin Jacobs.
........
  r59946 | amaury.forgeotdarc | 2008-01-14 02:07:27 +0100 (Mon, 14 Jan 2008) | 4 lines

  ?Why did my tests not notice this before?
  Slots inheritance is very different from OO inheritance.
  This code lead to infinite recursion on classes derived from StructType.
........
  r59947 | christian.heimes | 2008-01-14 04:33:52 +0100 (Mon, 14 Jan 2008) | 1 line

  Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags
........
  r59948 | christian.heimes | 2008-01-14 04:35:38 +0100 (Mon, 14 Jan 2008) | 1 line

  I missed the most important file
........
  r59949 | christian.heimes | 2008-01-14 04:42:48 +0100 (Mon, 14 Jan 2008) | 1 line

  Applied patch #1816: sys.flags patch
........
  r59950 | christian.heimes | 2008-01-14 05:13:37 +0100 (Mon, 14 Jan 2008) | 2 lines

  Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's
  readonly and help(sys.float_info) explains the attributes nicely.
........
  r59951 | christian.heimes | 2008-01-14 07:06:19 +0100 (Mon, 14 Jan 2008) | 1 line

  Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
........
2008-01-14 18:49:24 +00:00

121 lines
3.6 KiB
Python

import unittest
from test import test_support
import time
class StructSeqTest(unittest.TestCase):
def test_tuple(self):
t = time.gmtime()
astuple = tuple(t)
self.assertEqual(len(t), len(astuple))
self.assertEqual(t, astuple)
# Check that slicing works the same way; at one point, slicing t[i:j] with
# 0 < i < j could produce NULLs in the result.
for i in range(-len(t), len(t)):
self.assertEqual(t[i:], astuple[i:])
for j in range(-len(t), len(t)):
self.assertEqual(t[i:j], astuple[i:j])
for j in range(-len(t), len(t)):
self.assertEqual(t[:j], astuple[:j])
self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
self.assertRaises(IndexError, t.__getitem__, len(t))
for i in range(-len(t), len(t)-1):
self.assertEqual(t[i], astuple[i])
def test_repr(self):
t = time.gmtime()
self.assert_(repr(t))
t = time.gmtime(0)
self.assertEqual(repr(t),
"time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
"tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
def test_concat(self):
t1 = time.gmtime()
t2 = t1 + tuple(t1)
for i in range(len(t1)):
self.assertEqual(t2[i], t2[i+len(t1)])
def test_repeat(self):
t1 = time.gmtime()
t2 = 3 * t1
for i in range(len(t1)):
self.assertEqual(t2[i], t2[i+len(t1)])
self.assertEqual(t2[i], t2[i+2*len(t1)])
def test_contains(self):
t1 = time.gmtime()
for item in t1:
self.assert_(item in t1)
self.assert_(-42 not in t1)
def test_hash(self):
t1 = time.gmtime()
self.assertEqual(hash(t1), hash(tuple(t1)))
def test_cmp(self):
t1 = time.gmtime()
t2 = type(t1)(t1)
self.assertEqual(t1, t2)
self.assert_(not (t1 < t2))
self.assert_(t1 <= t2)
self.assert_(not (t1 > t2))
self.assert_(t1 >= t2)
self.assert_(not (t1 != t2))
def test_fields(self):
t = time.gmtime()
self.assertEqual(len(t), t.n_fields)
self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
def test_constructor(self):
t = time.struct_time
self.assertRaises(TypeError, t)
self.assertRaises(TypeError, t, None)
self.assertRaises(TypeError, t, "123")
self.assertRaises(TypeError, t, "123", dict={})
self.assertRaises(TypeError, t, "123456789", dict=None)
s = "123456789"
self.assertEqual("".join(t(s)), s)
def test_eviltuple(self):
class Exc(Exception):
pass
# Devious code could crash structseqs' contructors
class C:
def __getitem__(self, i):
raise Exc
def __len__(self):
return 9
self.assertRaises(Exc, time.struct_time, C())
def test_reduce(self):
t = time.gmtime()
x = t.__reduce__()
def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.
t = time.gmtime()
L = list(t)
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
for start in indices:
for stop in indices:
# Skip step 0 (invalid)
for step in indices[1:]:
self.assertEqual(list(t[start:stop:step]),
L[start:stop:step])
def test_main():
test_support.run_unittest(StructSeqTest)
if __name__ == "__main__":
test_main()