Skip to content

Weakref not working properly #45758

Closed
Closed

Description

@MHOOO
mannequin
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']

    Activity

    1. MHOOO commented on Nov 10, 2007

      MHOOOmannequin
      MannequinAuthor

      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?

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Nov 10, 2007
    3. doublep commented on Nov 10, 2007

      doublepmannequin
      Mannequin

      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.

    4. birkenfeld commented on Nov 10, 2007

      @birkenfeld
      Member

      Closing as invalid.

    5. rhettinger commented on Nov 10, 2007

      @rhettinger
      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.

    6. MHOOO commented on Nov 11, 2007

      MHOOOmannequin
      MannequinAuthor

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

    7. ggenellina commented on Nov 14, 2007

      ggenellinamannequin
      Mannequin

      I think this methodref function is simpler and much less intrusive

    8. MHOOO commented on Nov 15, 2007

      MHOOOmannequin
      MannequinAuthor

      Yeah, cool :)
      Thanks =)

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions