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: weakref.proxy incorrect behaviour
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, arigo, jafo, kozlovsky, mwh, pitrou, rhettinger
Priority: low Keywords:

Created on 2005-03-25 21:54 by kozlovsky, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (10)
msg24796 - (view) Author: Alexander Kozlovsky (kozlovsky) Date: 2005-03-25 21:54
According documentation, proxy in most contexts must 
have the same behaviour as object itself.

PROBLEM:

bool(proxy_object) != bool(object_itself)

EXAMPLE:

>>> class X(list): pass # collection class
... 
>>> x = X() # empty collection
>>> import weakref
>>> proxy = weakref.proxy(x)
>>> bool(x)
False
>>> bool(proxy)
True

PYTHON VERSION:

2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit 
(Intel)]

also tested on:

2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit 
(Intel)]
msg24797 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-03-26 14:50
Logged In: YES 
user_id=4771

The bug is in weakrefobject:proxy_nonzero(), which calls the underlying object's nb_nonzero slot instead of going through the general PyObject_IsTrue() mecanism.  Built-in types like list and dict don't have a nb_nonzero slot.

As a related bug, (Callable)ProxyType should implement tp_richcompare in addition to tp_compare.  In fact, we should review the code to check if ProxyType knows about the most recent type slots...
msg24798 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-03-27 11:17
Logged In: YES 
user_id=80475

Fixed the __nonzero__ problem for Py2.5 and will backport to
Py2.4.  See Objects/weakrefobject.c 1.21.

Leaviing this report open until the rest of the module can
be checked and fixed.

msg24799 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-03-29 13:23
Logged In: YES 
user_id=4771

The following type slots appear to be missing in the proxy types:

* nb_(inplace_){floor,true}_divide
* sq_repeat, sq_concat, sq_item, sq_ass_item, sq_inplace_concat, sq_inplace_repeat (which are needed for operator.repeat(), operator.concat(), etc. to work)
* tp_as_buffer
* tp_richcompare (tp_compare alone is not enough)
* tp_descr_get, tp_descr_set (for proxies to descriptors...)
* nb_coerce (though it seems that only an explicit call to coerce() can show that this is missing; implicit coercion is done correctly in other operators)
* nb_oct, nb_hex

As far as I can tell, there is no existing operator that is broken in the same way that nb_nonzero was.
msg24800 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-03-29 13:39
Logged In: YES 
user_id=6656

Also see bug #1075356 (which is very minor, but might as
well get fixed at the same time).
msg56062 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2007-09-20 17:46
Raymond: Is this still a problem, or should we close it because the
reported issue is resolved and 2.4 is no longer maintained?
msg114490 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-21 12:42
I'll close this in a couple of weeks unless anyone objects.
msg114500 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2010-08-21 15:19
All the missing type slots I reported can cause incorrect behavior very similar to the one reported originally.  For example (in Python 2.7):

    class I(int): pass
    i = I(123)
    hex(i)                   => '0x7b'
    hex(weakref.proxy(i))    => TypeError

I think that the present bug report is fine to report these as bugs, as I don't see the point of opening another bug report with the same title.  I will just re-mark this issue as "open - not resolved".
msg125272 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-04 01:23
Works in 2.7 and 3.2.
msg125347 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2011-01-04 18:35
Not for me (the last example I posted, on 2.7 head).  But I will not fight for this.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41761
2011-01-04 18:35:31arigosetnosy: mwh, arigo, rhettinger, jafo, pitrou, kozlovsky, BreamoreBoy
messages: + msg125347
2011-01-04 01:23:34pitrousetstatus: open -> closed

nosy: + pitrou
messages: + msg125272

resolution: out of date
2010-08-21 21:35:59georg.brandllinkissue9658 superseder
2010-08-21 16:33:17BreamoreBoysetversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.4
2010-08-21 15:19:58arigosetstatus: pending -> open
resolution: accepted -> (no value)
messages: + msg114500
2010-08-21 12:42:29BreamoreBoysetstatus: open -> pending
nosy: + BreamoreBoy
messages: + msg114490

2008-03-17 21:37:37rhettingersetassignee: rhettinger ->
2007-09-20 17:46:25jafosetresolution: accepted
messages: + msg56062
priority: high -> low
assignee: rhettinger
nosy: + jafo
type: behavior
2005-03-25 21:54:29kozlovskycreate