[Sugar-devel] [PATCH sugar-base 04/17] sugar.dispatch.saferef: don't use assert for critical code paths

Sascha Silbe sascha-pgp at silbe.org
Sat Oct 16 07:30:17 EDT 2010


assert a debugging tool, not meant to guard critical code paths. See e.g.
"Language Reference" section 6.3, first paragraph:

> Assert statements are a convenient way to insert debugging assertions into a
> program:

The same section also explains that enabling optimization will cause assert
statements not to be evaluated.

Signed-off-by: Sascha Silbe <sascha-pgp at silbe.org>

diff --git a/src/sugar/dispatch/saferef.py b/src/sugar/dispatch/saferef.py
index b0ceef9..655f9a1 100644
--- a/src/sugar/dispatch/saferef.py
+++ b/src/sugar/dispatch/saferef.py
@@ -23,11 +23,12 @@ def safeRef(target, onDelete = None):
         if target.im_self is not None:
             # Turn a bound method into a BoundMethodWeakref instance.
             # Keep track of these instances for lookup by disconnect().
-            assert hasattr(target, 'im_func'), """safeRef target %r has im_self, but no im_func, don't know how to create reference"""%( target,)
-            reference = get_bound_method_weakref(
-                target=target,
-                onDelete=onDelete
-            )
+            if not hasattr(target, 'im_func'):
+                raise TypeError("safeRef target %r has im_self, but no"
+                    " im_func, don't know how to create reference" %
+                    (target, ))
+            reference = get_bound_method_weakref(target=target,
+                onDelete=onDelete)
             return reference
     if callable(onDelete):
         return weakref.ref(target, onDelete)
-- 
1.7.1



More information about the Sugar-devel mailing list