problem in using metaclasses to inspect Python code

Michele Simionato mis6 at pitt.edu
Wed Dec 11 12:08:31 EST 2002


With the aim of using metaclasses to inspect Python code,
I have found a rather strange inconsistency in the case
of static method versus normal methods.

Let me consider first the case without metaclasses, where everything
works fine.

---- begin inspecting.py ---

#Consider the following class:

class C(object):
      def meth(self):
	  print "I am a plain method"
      def staticmeth():
	  print "I am a static method"
      staticmeth=staticmethod(staticmeth)

#I want to inspect the source code for the normal method "meth"
#and for the static method "staticmeth":

from inspect import getsource

print 'source of meth:\n', getsource(C.meth)
print 'source of staticmeth:\n', getsource(C.staticmeth)

---- end inspecting.py ---

The output of this program is the following:

$ python inspecting.py
source of meth:
      def meth(self):
          print "I am a plain method"

source of staticmeth:
      def staticmeth():
          print "I am a static method"

Therefore everything works as expected. There is no difference
between plain methods and static methods, the source can be
correctly retrived.

Consider now this second script, where a metaclass enters:

---- begin metainspecting.py ---

class MetaInspecting(type):    
    def __init__(cls,name,bases,dict):
        from inspect import getsource
        print 'source of meth:\n', getsource(dict['meth'])
        print 'source of staticmeth:\n', getsource(dict['staticmeth'])

class C(object):
      __metaclass__=MetaInspecting
      def meth(self):
	  print "I am a plain method"
      def staticmeth():
	  print "I am a static method"
      staticmeth=staticmethod(staticmeth)

---- end metainspecting.py ---

The output of this program is the following:
$ python metainspecting.py
source of meth:
      def meth(self):
          print "I am a plain method"

source of staticmeth:
Traceback (most recent call last):
  File "metainspecting.py", line 7, in ?
    class C(object):
  File "metainspecting.py", line 5, in __init__
    print 'source of staticmeth:\n', getsource(dict['staticmeth'])
  File "/usr/lib/python2.2/inspect.py", line 509, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python2.2/inspect.py", line 498, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python2.2/inspect.py", line 378, in findsource
    raise IOError, 'could not get source code'
IOError: could not get source code

As you see, there is an error in retriving the source code for the static
method, whereas the plain method works fine. Since the version without
metaclasses works fine, the inconsistency seems to me more similar to
a bug. What's the origin of this strange behavior ? Can it be fixed ?

           metaclasses-inspecting-classes-don't-work-ly yours,


--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list