[Python-Dev] Re: unexpected consequence of function attributes

Barry A. Warsaw barry@wooz.org
Wed, 17 Jan 2001 14:46:36 -0500


>>>>> "JH" == Jeremy Hylton <jeremy@alum.mit.edu> writes:

    JH> I have found one place in the library that depended on
    JH> hasattr(func, '__dict__') to return false -- dis.dis.  You
    JH> might want to check and see if there is anything other code
    JH> that doesn't expect function's to have extra attributes.  I
    JH> expect that only introspective code would be affected.

I guess we need a test_dis.py in the regression test suite, eh? :)

Here's an extremely quick and dirty fix to dis.py.
-Barry

-------------------- snip snip --------------------
Index: dis.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/dis.py,v
retrieving revision 1.28
diff -u -r1.28 dis.py
--- dis.py	2001/01/14 23:36:05	1.28
+++ dis.py	2001/01/17 19:45:40
@@ -15,6 +15,10 @@
         return
     if type(x) is types.InstanceType:
         x = x.__class__
+    if hasattr(x, 'func_code'):
+        x = x.func_code
+    if hasattr(x, 'im_func'):
+        x = x.im_func
     if hasattr(x, '__dict__'):
         items = x.__dict__.items()
         items.sort()
@@ -28,17 +32,12 @@
                 except TypeError, msg:
                     print "Sorry:", msg
                 print
+    elif hasattr(x, 'co_code'):
+        disassemble(x)
     else:
-        if hasattr(x, 'im_func'):
-            x = x.im_func
-        if hasattr(x, 'func_code'):
-            x = x.func_code
-        if hasattr(x, 'co_code'):
-            disassemble(x)
-        else:
-            raise TypeError, \
-                  "don't know how to disassemble %s objects" % \
-                  type(x).__name__
+        raise TypeError, \
+              "don't know how to disassemble %s objects" % \
+              type(x).__name__
 
 def distb(tb=None):
     """Disassemble a traceback (default: last traceback)."""