[issue7366] weakref module example relies on behaviour not guaranteed by id()

2010-02-22 Thread Jack Diederich

Jack Diederich jackd...@gmail.com added the comment:

This is true but /any/ key in the WeakValueDictionary could be reused and 
result in similar behavior, not just the id() of the inserted value.  I'm 
closing at won't fix

--
nosy: +jackdied
resolution:  - wont fix
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7366
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue7366] weakref module example relies on behaviour not guaranteed by id()

2009-11-19 Thread James Henstridge

New submission from James Henstridge ja...@jamesh.id.au:

The documentation for the weakref module contains an example that uses
WeakValueDictionary to implement a id2obj() lookup function that doesn't
store strong references to those objects.

This example implicitly assumes that the id() of an object will be
unique for the lifetime of the interpreter, when it is only unique for
the lifetime of the object.  The problem can be demonstrated like so:

1. create an object foo
2. function 1 remembers the id of this object with oid1 = remember(foo)
3. foo gets garbage collected
4. an object bar is created and happens to get the same memory location
5. function 2 remembers the id of this object with oid2 = remember(bar)
6. function 1 looks up its stored object ID with id2obj(oid1)

In step 6, the object bar is returned rather than an exception being
raised.

As well as the example being broken, the weakref module contains the
functionality a programmer would need to do this kind of thing safely:
use the weakref.ref type directly.

It'd be good to replace the example with a better one.

--
assignee: georg.brandl
components: Documentation
messages: 95530
nosy: georg.brandl, jamesh
severity: normal
status: open
title: weakref module example relies on behaviour not guaranteed by id()
versions: Python 2.7, Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7366
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue7366] weakref module example relies on behaviour not guaranteed by id()

2009-11-19 Thread James Henstridge

James Henstridge ja...@jamesh.id.au added the comment:

Forgot to include a link to the documentation I was talking about:

http://docs.python.org/library/weakref#example

This example also appears in the 2.7a0 and 3.2a0 documentation.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7366
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com