Task.__repr__: handle python-trace
[portage.git] / pym / _emerge / Task.py
1 # Copyright 1999-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 from portage.util.SlotObject import SlotObject
5
6 class Task(SlotObject):
7         __slots__ = ("_hash_key", "_hash_value")
8
9         def __eq__(self, other):
10                 try:
11                         return self._hash_key == other._hash_key
12                 except AttributeError:
13                         # depgraph._pkg() generates _hash_key
14                         # for lookups here, so handle that
15                         return self._hash_key == other
16
17         def __ne__(self, other):
18                 try:
19                         return self._hash_key != other._hash_key
20                 except AttributeError:
21                         return True
22
23         def __hash__(self):
24                 return self._hash_value
25
26         def __len__(self):
27                 return len(self._hash_key)
28
29         def __getitem__(self, key):
30                 return self._hash_key[key]
31
32         def __iter__(self):
33                 return iter(self._hash_key)
34
35         def __contains__(self, key):
36                 return key in self._hash_key
37
38         def __str__(self):
39                 """
40                 Emulate tuple.__repr__, but don't show 'foo' as u'foo' for unicode
41                 strings.
42                 """
43                 return "(%s)" % ", ".join(("'%s'" % x for x in self._hash_key))
44
45         def __repr__(self):
46                 if self._hash_key is None:
47                         # triggered by python-trace
48                         return SlotObject.__repr__(self)
49                 return "<%s (%s)>" % (self.__class__.__name__,
50                         ", ".join(("'%s'" % x for x in self._hash_key)))