Commit Graph

73 Commits

Author SHA1 Message Date
Fred Drake 0a4dd390bf Make weak references subclassable:
- weakref.ref and weakref.ReferenceType will become aliases for each
  other

- weakref.ref will be a modern, new-style class with proper __new__
  and __init__ methods

- weakref.WeakValueDictionary will have a lighter memory footprint,
  using a new weakref.ref subclass to associate the key with the
  value, allowing us to have only a single object of overhead for each
  dictionary entry (currently, there are 3 objects of overhead per
  entry: a weakref to the value, a weakref to the dictionary, and a
  function object used as a weakref callback; the weakref to the
  dictionary could be avoided without this change)

- a new macro, PyWeakref_CheckRefExact(), will be added

- PyWeakref_CheckRef() will check for subclasses of weakref.ref

This closes SF patch #983019.
2004-07-02 18:57:45 +00:00
Walter Dörwald 80ce6dd564 The default argument in dict.setdefault() defaults to None.
Add this default to weakref.WeakValueDictionary.setdefault()
and weakref.WeakKeyDictionary.setdefault() too.
2004-05-27 18:16:25 +00:00
Raymond Hettinger 31017aed36 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
(Championed by Bob Ippolito.)

The update() method for mappings now accepts all the same argument forms
as the dict() constructor.  This includes item lists and/or keyword
arguments.
2004-03-04 08:25:44 +00:00
Tim Peters 886128f4f8 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Someone review this, please!  Final releases are getting close, Fred
(the weakref guy) won't be around until Tuesday, and the pre-patch
code can indeed raise spurious RuntimeErrors in the presence of
threads or mutating comparison functions.

See the bug report for my confusions:  I can't see any reason for why
__delitem__ iterated over the keys.  The new one-liner implementation
is much faster, can't raise RuntimeError, and should be better-behaved
in all respects wrt threads.

New tests test_weak_keyed_bad_delitem and
test_weak_keyed_cascading_deletes fail before this patch.

Bugfix candidate for 2.2.3 too, if someone else agrees with this patch.
2003-05-25 01:45:11 +00:00
Raymond Hettinger 2c2d322884 SF patch #667730: More DictMixin
* Adds missing pop() methods to weakref.py
* Expands test suite to broaden coverage of objects with
  a mapping interface.

Contributed by Sebastien Keim.
2003-03-09 07:05:43 +00:00
Tim Peters c411dbaeee Whitespace normalization. 2002-07-16 21:35:23 +00:00
Guido van Rossum 009afb7c90 SF patch 564549 (Erik Andersén).
The WeakKeyDictionary constructor didn't work when a dict arg was
given.  Fixed by moving a line.  Also adding a unit test.

Bugfix candidate.
2002-06-10 20:00:52 +00:00
Raymond Hettinger 54f0222547 SF 563203. Replaced 'has_key()' with 'in'. 2002-06-01 14:18:47 +00:00
Fred Drake 3bae7ddf8e WeakKeyDictionary.has_key(): If the key being tested is not weakly
referencable (weakref.ref() raises TypeError), return 0 instead of
propogating the TypeError.
This closes SF bug #478536; bugfix candidate.
2001-11-06 16:36:53 +00:00
Fred Drake e029242d5c ReferenceError is now built-in, so pick it up from the right place.
It still needs to be here to preserve the API.
2001-10-05 21:54:09 +00:00
Fred Drake 746fe0fae5 Clean up circular references in the Weak*Dictionary classes; this avoids
depending on the cycle detector code in the library implementation.
This is a *slightly* different patch than SF patch #417795, but takes
the same approach.  (This version avoids calling the __len__() method of
the dict in the remove() functions.)
This closes SF patch #417795.
2001-09-28 19:01:26 +00:00
Fred Drake b663a2ccbd Add __delitem__() support for WeakKeyDictionary.
This closes SF bug #458860.
2001-09-06 14:51:01 +00:00
Fred Drake 4fd06e0170 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
for keys that are not in the dictionary.
2001-08-03 04:11:27 +00:00
Fred Drake 101209d44c Added iterator support to the Weak*Dictionary classes. 2001-05-02 05:43:09 +00:00
Fred Drake bd7f818c50 Weak*Dictionary: Added docstrings to the classes.
Weak*Dictionary.update():  No longer create a temporary list to hold the
    things that will be stuffed into the underlying dictionary.  This had
    been done so that if any of the objects used as the weakly-held value
    was not weakly-referencable, no updates would take place (TypeError
    would be raised).  With this change, TypeError will still be raised
    but a partial update could occur.  This is more like other .update()
    implementations.

Thoughout, use of the name "ref" as a local variable has been removed.  The
original use of the name occurred when the function to create a weak
reference was called "new"; the overloaded use of the name could be
confusing for someone reading the code.  "ref" used as a variable name
has been replaced with "wr" (for 'weak reference').
2001-04-19 16:26:06 +00:00
Fred Drake 1d9e4b7de3 Weak*Dictionary.update(): Fix calls to [].append() to only have one
parameter.

Weak*Dictionary.get():  Make the second parameter optional.

WeakKeyDictionary.has_key(), .keys():  Make these actually work!
2001-04-16 17:34:48 +00:00
Fred Drake 9a9d219f07 mapping(): Remove this function since it does not add anything to the API. 2001-04-10 19:11:23 +00:00
Skip Montanaro 40fc16059f final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
giving it a slight facelift
2001-03-01 04:27:19 +00:00
Fred Drake 9d2c85dec7 Change WeakDictionary to WeakValueDictionary in a couple more places.
WeakValueDictionary.copy(),
WeakKeyDictionary.copy():  Actually return the copy!
2001-03-01 03:06:03 +00:00
Martin v. Löwis 5e1633365d Patch #403985: Add support for weak-keyed dictionaries 2001-02-27 18:36:56 +00:00
Fred Drake 0399bd8ce2 Ouch! I need a better test suite for this. ;-( 2001-02-02 19:28:35 +00:00
Fred Drake 312a5dc539 WeakDictionary.items(): Do not allow (key,ref) pairs to leak out for
dead references.
2001-02-02 15:13:24 +00:00
Fred Drake 41deb1efc2 PEP 205, Weak References -- initial checkin. 2001-02-01 05:27:45 +00:00