mdickinson / refcycle

Support for displaying and analyzing reference graphs of Python objects.
Apache License 2.0
15 stars 1 forks source link

Expired weak references can cause annotation to fail. #78

Open mdickinson opened 7 years ago

mdickinson commented 7 years ago

Broken out from the issue 60 discussion: see https://github.com/mdickinson/refcycle/issues/60#issuecomment-317764540

File "C:\Python36\lib\site-packages\refcycle\object_graph.py", line 339, in export_json
self.annotated().export_json(filename=filename)
File "C:\Python36\lib\site-packages\refcycle\object_graph.py", line 279, in annotated
for vertex in self.vertices
File "C:\Python36\lib\site-packages\refcycle\object_graph.py", line 279, in 
for vertex in self.vertices
File "C:\Python36\lib\site-packages\refcycle\annotations.py", line 172, in object_annotation
if isinstance(obj, BASE_TYPES):
ReferenceError: weakly-referenced object no longer exists
VojtechFried commented 7 years ago

This should trigger the bug:

import gc
import refcycle
import weakref

class Item:
  def __init__( self ):
    self.text = "item"

class Cycle:
  def __init__( self, item ):
    self.self = self
    self.item = weakref.proxy( item )

def create_cycle():
  item = Item()
  return Cycle( item )

def main():
  gc.disable()
  gc.collect()
  create_cycle()
  garbage = refcycle.garbage()
  garbage.export_image()

if __name__ == '__main__':
    main()
mdickinson commented 7 years ago

@VojtechFried: Confirmed. Much appreciated - thank you.