diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/collections.py | 32 | ||||
-rw-r--r-- | Lib/ctypes/test/test_delattr.py | 21 | ||||
-rw-r--r-- | Lib/lib-tk/FixTk.py | 4 | ||||
-rw-r--r-- | Lib/opcode.py | 3 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 34 | ||||
-rw-r--r-- | Lib/test/test_dict.py | 11 | ||||
-rw-r--r-- | Lib/test/test_float.py | 73 |
7 files changed, 159 insertions, 19 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 2a7bb62..d539683 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -1,17 +1,15 @@ __all__ = ['deque', 'defaultdict', 'namedtuple'] - -from _collections import deque, defaultdict -from operator import itemgetter as _itemgetter -from itertools import izip as _izip -from keyword import iskeyword as _iskeyword -import sys as _sys - # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. # They should however be considered an integral part of collections.py. from _abcoll import * import _abcoll __all__ += _abcoll.__all__ +from _collections import deque, defaultdict +from operator import itemgetter as _itemgetter +from keyword import iskeyword as _iskeyword +import sys as _sys + def namedtuple(typename, field_names, verbose=False): """Returns a new subclass of tuple with named fields. @@ -19,9 +17,9 @@ def namedtuple(typename, field_names, verbose=False): >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords - >>> p[0] + p[1] # works just like the tuple (11, 22) + >>> p[0] + p[1] # indexable like a plain tuple 33 - >>> x, y = p # unpacks just like a tuple + >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) >>> p.x + p.y # fields also accessable by name @@ -58,31 +56,35 @@ def namedtuple(typename, field_names, verbose=False): # Create and fill-in the class template argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes reprtxt = ', '.join('%s=%%r' % name for name in field_names) + dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) template = '''class %(typename)s(tuple): '%(typename)s(%(argtxt)s)' \n __slots__ = () \n - _fields = property(lambda self: %(field_names)r) \n def __new__(cls, %(argtxt)s): return tuple.__new__(cls, (%(argtxt)s)) \n + _cast = classmethod(tuple.__new__) \n def __repr__(self): return '%(typename)s(%(reprtxt)s)' %% self \n - def _asdict(self, dict=dict, zip=zip): + def _asdict(t): 'Return a new dict which maps field names to their values' - return dict(zip(%(field_names)r, self)) \n + return {%(dicttxt)s} \n def _replace(self, **kwds): 'Return a new %(typename)s object replacing specified fields with new values' - return %(typename)s(*map(kwds.get, %(field_names)r, self)) \n\n''' % locals() + return %(typename)s._cast(map(kwds.get, %(field_names)r, self)) \n + @property + def _fields(self): + return %(field_names)r \n\n''' % locals() for i, name in enumerate(field_names): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: print(template) # Execute the template string in a temporary namespace - namespace = dict(itemgetter=_itemgetter, zip=_izip) + namespace = dict(itemgetter=_itemgetter) try: exec(template, namespace) except SyntaxError as e: - raise SyntaxError(e.message + ':\n' + template) + raise SyntaxError(e.msg + ':\n' + template) from e result = namespace[typename] # For pickling to work, the __module__ variable needs to be set to the frame diff --git a/Lib/ctypes/test/test_delattr.py b/Lib/ctypes/test/test_delattr.py new file mode 100644 index 0000000..0f4d586 --- /dev/null +++ b/Lib/ctypes/test/test_delattr.py @@ -0,0 +1,21 @@ +import unittest +from ctypes import * + +class X(Structure): + _fields_ = [("foo", c_int)] + +class TestCase(unittest.TestCase): + def test_simple(self): + self.assertRaises(TypeError, + delattr, c_int(42), "value") + + def test_chararray(self): + self.assertRaises(TypeError, + delattr, (c_char * 5)(), "value") + + def test_struct(self): + self.assertRaises(TypeError, + delattr, X(), "foo") + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/lib-tk/FixTk.py b/Lib/lib-tk/FixTk.py index 0726a51..8820cba 100644 --- a/Lib/lib-tk/FixTk.py +++ b/Lib/lib-tk/FixTk.py @@ -11,6 +11,10 @@ import sys, os # the real Tcl library will do. prefix = os.path.join(sys.prefix,"tcl") +if not os.path.exists(prefix): + # devdir/../tcltk/lib + prefix = os.path.join(sys.prefix, os.path.pardir, "tcltk", "lib") + prefix = os.path.abspath(prefix) # if this does not exist, no further search is needed if os.path.exists(prefix): if "TCL_LIBRARY" not in os.environ: diff --git a/Lib/opcode.py b/Lib/opcode.py index ac26583..6fe380f 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -71,6 +71,7 @@ def_op('BINARY_TRUE_DIVIDE', 27) def_op('INPLACE_FLOOR_DIVIDE', 28) def_op('INPLACE_TRUE_DIVIDE', 29) +def_op('STORE_MAP', 54) def_op('INPLACE_ADD', 55) def_op('INPLACE_SUBTRACT', 56) def_op('INPLACE_MULTIPLY', 57) @@ -123,7 +124,7 @@ name_op('LOAD_NAME', 101) # Index in name list def_op('BUILD_TUPLE', 102) # Number of tuple items def_op('BUILD_LIST', 103) # Number of list items def_op('BUILD_SET', 104) # Number of set items -def_op('BUILD_MAP', 105) # Always zero for now +def_op('BUILD_MAP', 105) # Number of dict entries (upto 255) name_op('LOAD_ATTR', 106) # Index in name list def_op('COMPARE_OP', 107) # Comparison operator hascompare.append(107) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 5a4029d..fdc82fc 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -49,6 +49,7 @@ class TestNamedTuple(unittest.TestCase): self.assertEqual(repr(p), 'Point(x=11, y=22)') self.assert_('__dict__' not in dir(p)) # verify instance has no dict self.assert_('__weakref__' not in dir(p)) + self.assertEqual(p, Point._cast([11, 22])) # test _cast classmethod self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method @@ -93,9 +94,40 @@ class TestNamedTuple(unittest.TestCase): def test_odd_sizes(self): Zero = namedtuple('Zero', '') self.assertEqual(Zero(), ()) + self.assertEqual(Zero._cast([]), ()) + self.assertEqual(repr(Zero()), 'Zero()') + self.assertEqual(Zero()._asdict(), {}) + self.assertEqual(Zero()._fields, ()) + Dot = namedtuple('Dot', 'd') self.assertEqual(Dot(1), (1,)) - + self.assertEqual(Dot._cast([1]), (1,)) + self.assertEqual(Dot(1).d, 1) + self.assertEqual(repr(Dot(1)), 'Dot(d=1)') + self.assertEqual(Dot(1)._asdict(), {'d':1}) + self.assertEqual(Dot(1)._replace(d=999), (999,)) + self.assertEqual(Dot(1)._fields, ('d',)) + + # n = 10000 + n = 254 # SyntaxError: more than 255 arguments: + import string, random + names = [''.join([random.choice(string.ascii_letters) for j in range(10)]) for i in range(n)] + Big = namedtuple('Big', names) + b = Big(*range(n)) + self.assertEqual(b, tuple(range(n))) + self.assertEqual(Big._cast(range(n)), tuple(range(n))) + for pos, name in enumerate(names): + self.assertEqual(getattr(b, name), pos) + repr(b) # make sure repr() doesn't blow-up + d = b._asdict() + d_expected = dict(zip(names, range(n))) + self.assertEqual(d, d_expected) + b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)])) + b2_expected = list(range(n)) + b2_expected[1] = 999 + b2_expected[-5] = 42 + self.assertEqual(b2, tuple(b2_expected)) + self.assertEqual(b._fields, tuple(names)) class TestOneTrickPonyABCs(unittest.TestCase): diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 56ce722..53e6446 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -1,7 +1,7 @@ import unittest from test import test_support -import sys, UserDict +import sys, UserDict, random, string class DictTest(unittest.TestCase): @@ -11,6 +11,15 @@ class DictTest(unittest.TestCase): self.assertEqual(dict(), {}) self.assert_(dict() is not {}) + def test_literal_constructor(self): + # check literal constructor for different sized dicts (to exercise the BUILD_MAP oparg + items = [] + for n in range(400): + dictliteral = '{' + ', '.join('%r: %d' % item for item in items) + '}' + self.assertEqual(eval(dictliteral), dict(items)) + items.append((''.join([random.choice(string.ascii_letters) for j in range(8)]), n)) + random.shuffle(items) + def test_bool(self): self.assert_(not {}) self.assert_({1: 2}) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 922de7a..0de65cb 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -3,6 +3,12 @@ import unittest, struct import os from test import test_support +def isinf(x): + return x * 0.5 == x + +def isnan(x): + return x != x + class FormatFunctionsTestCase(unittest.TestCase): def setUp(self): @@ -159,6 +165,70 @@ class ReprTestCase(unittest.TestCase): self.assertEqual(v, eval(repr(v))) floats_file.close() +# Beginning with Python 2.6 float has cross platform compatible +# ways to create and representate inf and nan +class InfNanTest(unittest.TestCase): + def test_inf_from_str(self): + self.assert_(isinf(float("inf"))) + self.assert_(isinf(float("+inf"))) + self.assert_(isinf(float("-inf"))) + + self.assertEqual(repr(float("inf")), "inf") + self.assertEqual(repr(float("+inf")), "inf") + self.assertEqual(repr(float("-inf")), "-inf") + + self.assertEqual(repr(float("INF")), "inf") + self.assertEqual(repr(float("+Inf")), "inf") + self.assertEqual(repr(float("-iNF")), "-inf") + + self.assertEqual(str(float("inf")), "inf") + self.assertEqual(str(float("+inf")), "inf") + self.assertEqual(str(float("-inf")), "-inf") + + self.assertRaises(ValueError, float, "info") + self.assertRaises(ValueError, float, "+info") + self.assertRaises(ValueError, float, "-info") + self.assertRaises(ValueError, float, "in") + self.assertRaises(ValueError, float, "+in") + self.assertRaises(ValueError, float, "-in") + + def test_inf_as_str(self): + self.assertEqual(repr(1e300 * 1e300), "inf") + self.assertEqual(repr(-1e300 * 1e300), "-inf") + + self.assertEqual(str(1e300 * 1e300), "inf") + self.assertEqual(str(-1e300 * 1e300), "-inf") + + def test_nan_from_str(self): + self.assert_(isnan(float("nan"))) + self.assert_(isnan(float("+nan"))) + self.assert_(isnan(float("-nan"))) + + self.assertEqual(repr(float("nan")), "nan") + self.assertEqual(repr(float("+nan")), "nan") + self.assertEqual(repr(float("-nan")), "nan") + + self.assertEqual(repr(float("NAN")), "nan") + self.assertEqual(repr(float("+NAn")), "nan") + self.assertEqual(repr(float("-NaN")), "nan") + + self.assertEqual(str(float("nan")), "nan") + self.assertEqual(str(float("+nan")), "nan") + self.assertEqual(str(float("-nan")), "nan") + + self.assertRaises(ValueError, float, "nana") + self.assertRaises(ValueError, float, "+nana") + self.assertRaises(ValueError, float, "-nana") + self.assertRaises(ValueError, float, "na") + self.assertRaises(ValueError, float, "+na") + self.assertRaises(ValueError, float, "-na") + + def test_nan_as_str(self): + self.assertEqual(repr(1e300 * 1e300 * 0), "nan") + self.assertEqual(repr(-1e300 * 1e300 * 0), "nan") + + self.assertEqual(str(1e300 * 1e300 * 0), "nan") + self.assertEqual(str(-1e300 * 1e300 * 0), "nan") def test_main(): test_support.run_unittest( @@ -166,7 +236,8 @@ def test_main(): UnknownFormatTestCase, IEEEFormatTestCase, FormatTestCase, - #ReprTestCase + ReprTestCase, + InfNanTest, ) if __name__ == '__main__': |