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: Python 2.7 Tutorial Conflicting behavior with WeakValueDictionary.
Type: behavior Stage: resolved
Components: Demos and Tools Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: jessembacon, r.david.murray
Priority: normal Keywords:

Created on 2015-05-03 23:15 by jessembacon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
msg242534 - (view) Author: Jesse Bacon (jessembacon) Date: 2015-05-03 23:15
https://docs.python.org/2/tutorial/stdlib2.html
Section 11.6. Weak References

The example code below from the python tutorial suggests that the value of a is no persistent when cast into a WeakValueDictionary as entry 'primary'

The value persisted in the dictionary aver 'a' was deleted and garbage collection had been called.  I did not see a bug report for this already forgive me if there was one already put in.  

import weakref, gc
class A:
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return str(self.value)

a = A(10)                   # create a reference
d = weakref.WeakValueDictionary()
d['primary'] = a            # does not create a reference
d['primary']                # fetch the object if it is still alive

del a                       # remove the one reference
gc.collect()                # run garbage collection right away

d['primary']                # entry was automatically removed
msg242535 - (view) Author: Jesse Bacon (jessembacon) Date: 2015-05-03 23:16
https://docs.python.org/2/tutorial/stdlib2.html
Section 11.6. Weak References

The example code below from the python tutorial suggests that the value of 'a' is not persistent when cast into a WeakValueDictionary as entry 'primary'

The value persisted in the dictionary aver 'a' was deleted and garbage collection had been called.  I did not see a bug report for this already forgive me if there was one already put in.  

import weakref, gc
class A:
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return str(self.value)

a = A(10)                   # create a reference
d = weakref.WeakValueDictionary()
d['primary'] = a            # does not create a reference
d['primary']                # fetch the object if it is still alive

del a                       # remove the one reference
gc.collect()                # run garbage collection right away

d['primary']                # entry was automatically removed
msg242536 - (view) Author: Jesse Bacon (jessembacon) Date: 2015-05-03 23:17
https://docs.python.org/2/tutorial/stdlib2.html
Section 11.6. Weak References

The example code below from the python tutorial suggests that the value of 'a' is not persistent when cast into a WeakValueDictionary as entry 'primary'

The value persisted in the dictionary after 'a' was deleted and garbage collection had been called.  I did not see a bug report for this already forgive me if there was one already put in.  

import weakref, gc
class A:
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return str(self.value)

a = A(10)                   # create a reference
d = weakref.WeakValueDictionary()
d['primary'] = a            # does not create a reference
d['primary']                # fetch the object if it is still alive

del a                       # remove the one reference
gc.collect()                # run garbage collection right away

d['primary']                # entry was automatically removed
msg242539 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-04 00:54
If I run the example code you pasted using 2.7, I get a KeyError.  So the example looks correct to me.
msg242540 - (view) Author: Jesse Bacon (jessembacon) Date: 2015-05-04 01:16
Thank you for the second eyes. 
I just verified that it works using the standard python shell.  It looks like the bug is in IPython.  I’ll post it there.

In [7]: class A:
   ...:     def __init__(self, value):
   ...:         self.value = value
   ...:     def __repr__(self):
   ...:         return str(self.value)
   ...:     

In [8]: a = A(10)

In [9]: d = weakref.WeakValueDictionary()

In [10]: d['primary'] = a

In [11]: d['primary'] 
Out[11]: 10

In [12]: del a

In [13]: gc.collect()
Out[13]: 148

In [14]: d['primary']
Out[14]: 10

In [15]: 

sincerely,

Jesse Bacon

> On May 3, 2015, at 8:54 PM, R. David Murray <report@bugs.python.org> wrote:
> 
> 
> R. David Murray added the comment:
> 
> If I run the example code you pasted using 2.7, I get a KeyError.  So the example looks correct to me.
> 
> ----------
> nosy: +r.david.murray
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24123>
> _______________________________________
msg242542 - (view) Author: Jesse Bacon (jessembacon) Date: 2015-05-04 02:44
After further testing it appears that the bug is in IPython not Python 2.7. 
The bug has been moved to the Python list at https://github.com/ipython/ipython/issues/8403#issuecomment-98565759
msg242549 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-04 10:52
Well, technically it is probably not a bug.  IPython is doubtless holding on to a reference to 'a' because it was defined at the prompt.  Perhaps it could use a weakvaluedict for that, though :)  On the other hand they might consider it a feature that objects don't go out of scope.
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68311
2015-05-04 10:52:15r.david.murraysetmessages: + msg242549
stage: resolved
2015-05-04 02:44:41jessembaconsetstatus: open -> closed
resolution: third party
messages: + msg242542
2015-05-04 01:16:34jessembaconsetmessages: + msg242540
title: Python 2.7 Tutorial Conflicting behavior with WeakValueDictionary. -> Python 2.7 Tutorial Conflicting behavior with WeakValueDictionary.
2015-05-04 00:54:35r.david.murraysetnosy: + r.david.murray
messages: + msg242539
2015-05-03 23:17:56jessembaconsetmessages: + msg242536
2015-05-03 23:16:38jessembaconsetmessages: + msg242535
2015-05-03 23:15:38jessembaconcreate