Mercurial > cpython-lru_cache-weakref
annotate Lib/UserDict.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 6ee19c19b2c3 |
| children | 247183270095 |
| rev | line source |
|---|---|
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10477
diff
changeset
|
1 """A more or less complete user-defined wrapper around dictionary objects.""" |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 class UserDict: |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
4 def __init__(self, dict=None): |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
5 self.data = {} |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
6 if dict is not None: self.update(dict) |
|
5708
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
7 def __repr__(self): return repr(self.data) |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
8 def __cmp__(self, dict): |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
9 if isinstance(dict, UserDict): |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
10 return cmp(self.data, dict.data) |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
11 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6396
diff
changeset
|
12 return cmp(self.data, dict) |
|
5708
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
13 def __len__(self): return len(self.data) |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
14 def __getitem__(self, key): return self.data[key] |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
15 def __setitem__(self, key, item): self.data[key] = item |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
16 def __delitem__(self, key): del self.data[key] |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
17 def clear(self): self.data.clear() |
|
5708
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
18 def copy(self): |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
19 if self.__class__ is UserDict: |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
20 return UserDict(self.data) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6396
diff
changeset
|
21 import copy |
|
20883
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
22 data = self.data |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
23 try: |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
24 self.data = {} |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
25 c = copy.copy(self) |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
26 finally: |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
27 self.data = data |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
28 c.update(self) |
|
6ee19c19b2c3
copy(): Make sure the copy of a derived class cannot share the data of the
Fred Drake <fdrake@acm.org>
parents:
19041
diff
changeset
|
29 return c |
|
5708
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
30 def keys(self): return self.data.keys() |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
31 def items(self): return self.data.items() |
|
17664
b9076d8718ab
Added support for .iteritems(), .iterkeys(), .itervalues().
Fred Drake <fdrake@acm.org>
parents:
17608
diff
changeset
|
32 def iteritems(self): return self.data.iteritems() |
|
b9076d8718ab
Added support for .iteritems(), .iterkeys(), .itervalues().
Fred Drake <fdrake@acm.org>
parents:
17608
diff
changeset
|
33 def iterkeys(self): return self.data.iterkeys() |
|
b9076d8718ab
Added support for .iteritems(), .iterkeys(), .itervalues().
Fred Drake <fdrake@acm.org>
parents:
17608
diff
changeset
|
34 def itervalues(self): return self.data.itervalues() |
|
5708
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
35 def values(self): return self.data.values() |
|
99cd39f6a52d
Fix bug in copy() by using copy.copy() instead of making assumptions
Guido van Rossum <guido@python.org>
parents:
5707
diff
changeset
|
36 def has_key(self, key): return self.data.has_key(key) |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
37 def update(self, dict): |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
38 if isinstance(dict, UserDict): |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
39 self.data.update(dict.data) |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
40 elif isinstance(dict, type(self.data)): |
|
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
41 self.data.update(dict) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6396
diff
changeset
|
42 else: |
|
10477
4b19461780b9
Improved a bunch of things.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
43 for k, v in dict.items(): |
|
18141
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
44 self[k] = v |
|
6396
6ea926b46f8a
UserDict.get(): New method to mirror built-in dictionaries' get()
Barry Warsaw <barry@python.org>
parents:
5708
diff
changeset
|
45 def get(self, key, failobj=None): |
|
18141
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
46 if not self.has_key(key): |
|
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
47 return failobj |
|
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
48 return self[key] |
|
13892
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
49 def setdefault(self, key, failobj=None): |
|
18141
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
50 if not self.has_key(key): |
|
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
51 self[key] = failobj |
|
97a208291cd8
Patch #413171: Implement get, setdefault, update in terms of
Martin v. Löwis <martin@v.loewis.de>
parents:
17664
diff
changeset
|
52 return self[key] |
| 15523 | 53 def popitem(self): |
| 54 return self.data.popitem() | |
|
17608
22af15f8410a
Give UserDict new __contains__ and __iter__ methods.
Tim Peters <tim.peters@gmail.com>
parents:
16657
diff
changeset
|
55 def __contains__(self, key): |
|
22af15f8410a
Give UserDict new __contains__ and __iter__ methods.
Tim Peters <tim.peters@gmail.com>
parents:
16657
diff
changeset
|
56 return key in self.data |
|
19041
9a88e50bf114
Remove the __iter__ method from the UserDict class -- it can silently
Guido van Rossum <guido@python.org>
parents:
18141
diff
changeset
|
57 |
|
9a88e50bf114
Remove the __iter__ method from the UserDict class -- it can silently
Guido van Rossum <guido@python.org>
parents:
18141
diff
changeset
|
58 class IterableUserDict(UserDict): |
|
17608
22af15f8410a
Give UserDict new __contains__ and __iter__ methods.
Tim Peters <tim.peters@gmail.com>
parents:
16657
diff
changeset
|
59 def __iter__(self): |
|
22af15f8410a
Give UserDict new __contains__ and __iter__ methods.
Tim Peters <tim.peters@gmail.com>
parents:
16657
diff
changeset
|
60 return iter(self.data) |
