This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: WeakValueDictionary.setdefault() bug
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fdrake Nosy List: barry, fdrake
Priority: high Keywords:

Created on 2002-03-13 05:29 by barry, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
wr-patch.txt barry, 2002-03-13 05:29
Messages (2)
msg9680 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2002-03-13 05:29
In Python 2.1.2, WeakValueDictionary.setdefault() is
broken:

Python 2.1.2 (#1, Jan 27 2002, 18:54:41) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)]
on linux2
Type "copyright", "credits" or "license" for more
information.
>>> import weakref
>>> d = weakref.WeakValueDictionary()
>>> class C: pass
... 
>>> d.setdefault('yes', C())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.1/weakref.py", line 91,
in setdefault
    ref = ref(default, remove)
UnboundLocalError: local variable 'ref' referenced
before assignment

Attached is a simple patch, along with a test case. 
This is a bug fix candidate for Python 2.1.3, should
there be one.  It is not necessary for Python 2.2,
although you may want to port the test forward.
msg9681 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2002-03-13 05:50
Logged In: YES 
user_id=3066

Fixed using a very similar patch in Lib/weakref.py revision
1.8.2.1.  Backported a test that was added for Python 2.2.*
in Lib/test/test_weakref.py revision 1.7.2.1.
History
Date User Action Args
2022-04-10 16:05:05adminsetgithub: 36252
2002-03-13 05:29:20barrycreate