#NEWLINE# def safeRef( target , onDelete = None ) : #NEWLINE# #INDENT# "Return a *safe* weak reference to a callable target\n\n    target -- the object to be weakly referenced, if it's a\n        bound method reference, will create a BoundMethodWeakref,\n        otherwise creates a simple weakref.\n    onDelete -- if provided, will have a hard reference stored\n        to the callable to be called after the safe reference\n        goes out of scope with the reference object, (either a\n        weakref or a BoundMethodWeakref) as argument.\n    " #NEWLINE# if hasattr ( onDelete , '__self__' ) : #NEWLINE# #INDENT# if ( target . __self__ is not None ) : #NEWLINE# #INDENT# assert hasattr ( target , '__func__' ) , ( "safeRef target %r has __self__, but no __func__, don't know how to create reference" % ( target , ) ) #NEWLINE# reference = get_bound_method_weakref ( target = target , onDelete = onDelete ) #NEWLINE# return reference #NEWLINE# #UNINDENT# #UNINDENT# if callable ( onDelete ) : #NEWLINE# #INDENT# return weakref . ref ( target , onDelete ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return weakref . ref ( target )