Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weakref not working properly #45758

Closed
MHOOO mannequin opened this issue Nov 10, 2007 · 7 comments
Closed

Weakref not working properly #45758

MHOOO mannequin opened this issue Nov 10, 2007 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@MHOOO
Copy link
Mannequin

MHOOO mannequin commented Nov 10, 2007

BPO 1417
Nosy @birkenfeld, @rhettinger
Files
  • test2.py
  • myhacks.py
  • methodref.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2007-11-10.17:44:34.360>
    created_at = <Date 2007-11-10.12:20:51.920>
    labels = ['invalid', 'type-bug', 'library']
    title = 'Weakref not working properly'
    updated_at = <Date 2007-11-15.21:21:12.027>
    user = 'https://bugs.python.org/MHOOO'

    bugs.python.org fields:

    activity = <Date 2007-11-15.21:21:12.027>
    actor = 'MHOOO'
    assignee = 'none'
    closed = True
    closed_date = <Date 2007-11-10.17:44:34.360>
    closer = 'georg.brandl'
    components = ['Library (Lib)']
    creation = <Date 2007-11-10.12:20:51.920>
    creator = 'MHOOO'
    dependencies = []
    files = ['8725', '8733', '8744']
    hgrepos = []
    issue_num = 1417
    keywords = []
    message_count = 7.0
    messages = ['57348', '57353', '57355', '57362', '57375', '57481', '57562']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'rhettinger', 'ggenellina', '_doublep', 'MHOOO']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1417'
    versions = ['Python 2.5']

    @MHOOO
    Copy link
    Mannequin Author

    MHOOO mannequin commented Nov 10, 2007

    The following code is not working as expected:
    import weakref
    class cls1:
    def giveTo( self, to ):
    to.take( self.bla )
    def bla(self ):
    pass

    class cls2:
    def take( self, what ):
    self.ref = weakref.ref(what)

    c1 = cls1()
    c2 = cls2()
    c1.giveTo( c2 )
    print c1.bla
    print c2.ref

    It prints out:
    <bound method cls1.bla of <main.cls1 instance at 0x00CA9E18>>
    <weakref at 00CAF180; dead>

    Why is the weakref pointing to a dead object, when it's still alive?

    @MHOOO MHOOO mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 10, 2007
    @doublep
    Copy link
    Mannequin

    doublep mannequin commented Nov 10, 2007

    Because self.bla is a bound-method object, which is created and then
    instantly deleted as unreferenced. This is not a bug, it is expected.

    >>> class X:
    ...   def foo (self): pass
    ...
    >>> x = X ()
    >>> x.foo is x.foo
    False

    Note how the objects are different.

    @birkenfeld
    Copy link
    Member

    Closing as invalid.

    @rhettinger
    Copy link
    Contributor

    It's easier to see what is going on if you print the object ids. The
    id of self.bla is different than the subsequent c1.bla. The first is
    freed before the second gets created.

    @MHOOO
    Copy link
    Mannequin Author

    MHOOO mannequin commented Nov 11, 2007

    Well, too bad.
    My workaround (to make weakrefs work) is attached as a file.

    @ggenellina
    Copy link
    Mannequin

    ggenellina mannequin commented Nov 14, 2007

    I think this methodref function is simpler and much less intrusive

    @MHOOO
    Copy link
    Mannequin Author

    MHOOO mannequin commented Nov 15, 2007

    Yeah, cool :)
    Thanks =)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants