~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_py.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-13 06:08:53 UTC
  • mfrom: (4737.1.1 merge-2.0-into-devel)
  • Revision ID: pqm@pqm.ubuntu.com-20091013060853-erk2aaj80fnkrv25
(andrew) Merge lp:bzr/2.0 into lp:bzr, including fixes for #322807,
        #389413, #402623 and documentation improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
strings.
21
21
"""
22
22
 
23
 
from __future__ import absolute_import
24
 
 
25
23
 
26
24
class StaticTuple(tuple):
27
25
    """A static type, similar to a tuple of strings."""
28
26
 
29
 
    __slots__ = ()
30
 
 
31
27
    def __new__(cls, *args):
32
28
        # Make the empty StaticTuple a singleton
33
29
        if not args and _empty_tuple is not None:
36
32
 
37
33
    def __init__(self, *args):
38
34
        """Create a new 'StaticTuple'"""
 
35
        for bit in args:
 
36
            if type(bit) not in (str, StaticTuple):
 
37
                raise TypeError('key bits must be strings or StaticTuple')
39
38
        num_keys = len(args)
40
39
        if num_keys < 0 or num_keys > 255:
41
 
            raise TypeError('StaticTuple(...) takes from 0 to 255 items')
42
 
        for bit in args:
43
 
            if type(bit) not in (str, StaticTuple, unicode, int, long, float,
44
 
                                 None.__class__, bool):
45
 
                raise TypeError('StaticTuple can only point to'
46
 
                    ' StaticTuple, str, unicode, int, long, float, bool, or'
47
 
                    ' None not %s' % (type(bit),))
 
40
            raise ValueError('must have 1 => 256 key bits')
48
41
        # We don't need to pass args to tuple.__init__, because that was
49
42
        # already handled in __new__.
50
43
        tuple.__init__(self)
52
45
    def __repr__(self):
53
46
        return '%s%s' % (self.__class__.__name__, tuple.__repr__(self))
54
47
 
55
 
    def __reduce__(self):
56
 
        return (StaticTuple, tuple(self))
57
 
 
58
 
    def __add__(self, other):
59
 
        """Concatenate self with other"""
60
 
        return StaticTuple.from_sequence(tuple.__add__(self,other))
61
 
 
62
48
    def as_tuple(self):
63
 
        return tuple(self)
 
49
        return self
64
50
 
65
51
    def intern(self):
66
52
        return _interned_tuples.setdefault(self, self)
67
53
 
68
 
    @staticmethod
69
 
    def from_sequence(seq):
70
 
        """Convert a sequence object into a StaticTuple instance."""
71
 
        if isinstance(seq, StaticTuple):
72
 
            # it already is
73
 
            return seq
74
 
        return StaticTuple(*seq)
75
 
 
76
 
 
77
54
 
78
55
# Have to set it to None first, so that __new__ can determine whether
79
56
# the _empty_tuple singleton has been created yet or not.