classification
Title: _threading_local.py logic error in _localbase __new__
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 2.6
process
Status: open Resolution: accepted
Dependencies: Superseder:
Assigned To: amaury.forgeotdarc Nosy List: ajaksu2, amaury.forgeotdarc, gvanrossum, tony_nelson (4)
Priority: normal Keywords: needs review, patch

Created on 2006-07-14 00:52 by tony_nelson, last changed 2009-03-30 16:04 by gvanrossum.

Files
File name Uploaded Description Edit Remove
localbase.patch amaury.forgeotdarc, 2009-03-30 13:20
Messages (5)
msg60941 - (view) Author: Tony Nelson (tony_nelson) Date: 2006-07-14 00:52
I don't understand _threading_local.py, but I think
that parens are needed below in class _localbase __new__().

    if args or kw and (cls.__init__ is object.__init__):
        raise TypeError...

should be "(args or kw)", I think, looking at the
related test in _patch().

I'm not quite sure if this is python 2.5, but it is in
trunk.
msg84507 - (view) Author: Daniel Diniz (ajaksu2) Date: 2009-03-30 05:07
It isn't obvious to me that this issue is valid.

Some more context:

class _localbase(object):
    __slots__ = '_local__key', '_local__args', '_local__lock'

    def __new__(cls, *args, **kw):
        self = object.__new__(cls)
        key = '_local__key', 'thread.local.' + str(id(self))
        object.__setattr__(self, '_local__key', key)
        object.__setattr__(self, '_local__args', (args, kw))
        object.__setattr__(self, '_local__lock', RLock())

        if args or kw and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")
msg84543 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) Date: 2009-03-30 13:20
It is still valid:

>>> class MyLocal(local):
...    def __init__(self, *args, **kwargs):
...        self.args = args
...        self.kwargs = kwargs
...
>>> MyLocal(1)

This works when local is thread._local (from thread import _local as
local), but fails when imported from _threading_local (from
_threading_local import local)

See attached patch for the test case (and the fix of course)
msg84566 - (view) Author: Tony Nelson (tony_nelson) Date: 2009-03-30 15:40
Thanks, Amaury.  The new test works here on Python2.6.1, failing without
the fix and passing with it.  (Passing MyLocal(a=1) and failing
MyLocal(1), as expected.)  With the fix, _threading_local.py supports
positional arguments to subclass __init__, as well as keyword arguments.
msg84571 - (view) Author: Guido van Rossum (gvanrossum) Date: 2009-03-30 16:04
Looks good to me. Amaury, can you make sure this lands in 2.6, 2.7, 3.0
and 3.1?
History
Date User Action Args
2009-03-30 16:04:08gvanrossumsetnosy: + gvanrossum
messages: + msg84571

assignee: amaury.forgeotdarc
resolution: accepted
2009-03-30 15:40:36tony_nelsonsetmessages: + msg84566
2009-03-30 13:20:10amaury.forgeotdarcsetfiles: + localbase.patch

nosy: + amaury.forgeotdarc
messages: + msg84543

keywords: + needs review, patch
2009-03-30 05:07:44ajaksu2setversions: + Python 2.6, - Python 2.5
nosy: + ajaksu2

messages: + msg84507

type: behavior
stage: test needed
2006-07-14 00:52:48tony_nelsoncreate