[Python-bugs-list] [ python-Bugs-620190 ] inspect and object instances

SourceForge.net noreply@sourceforge.net
Sat, 28 Jun 2003 10:25:18 -0700


Bugs item #620190, was opened at 2002-10-08 12:26
Message generated for change (Comment added) made by jhylton
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=620190&group_id=5470

Category: Python Library
Group: Python 2.2.2
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Alexander Schmolck (aschmolck)
Assigned to: Jeremy Hylton (jhylton)
Summary: inspect and object instances

Initial Comment:
inspect.getargspec(NewKlass.aMethod)  fails (which
might technically be OK because the docs only mention
functions, not methods) but I also vaguely seem to
remember that some other operations in the inspect
module only worked for oldstyle classes under 2.2.1.

----------------------------------------------------------------------

>Comment By: Jeremy Hylton (jhylton)
Date: 2003-06-28 17:25

Message:
Logged In: YES 
user_id=31392

The simple fix for getargspec() was checked in.  I'm not
going to do anything about the methodness suggestion.  I
don't think Python has a clear notion of which attributes
are methods and which are not.


----------------------------------------------------------------------

Comment By: Alexander Schmolck (aschmolck)
Date: 2003-06-22 17:05

Message:
Logged In: YES 
user_id=559641

a late reply RE: other problems (the notification I received
from SF for a (mysterious) 'settings change' prompted me to
look at this report again).

There doesn't seem to be a straightforwad way to test for
(instance) "methodness".
For example, I think it ought to be easy to get the
instance, class and/or staticmethods of something,
independent of whether it is old-style class, new-style
class or builtin type etc., which to most extents and
purposes is an insignificant implementation detail. For
example, I doubt the following will do what the casual
programmer expects:

>>> inspect.getmembers(foo, inspect.ismethod)

because foo might e.g. be a Numeric.array. The only function
in `inspect` that returns something along those lines that
is useful and understandable to someone who hasn't got the
most exquisite knowledge about python's
old-style/new-style/builin-type, method-wrapper, __slots__
etc. chaos is `classify_class_attrs', only that this doesn't
work for types or classes with __slots__ (at least the
latter ought to be fixed) and also packages together
functionality that I think should exist seperately.


----------------------------------------------------------------------

Comment By: Michael Stone (mbrierst)
Date: 2003-02-11 22:46

Message:
Logged In: YES 
user_id=670441

Well I think this (one line)
patch will make getargspec work with
methods, if anyone wants to apply it.
Did you find any other operations that
don't work?

patch:
Index: Lib/inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/inspect.py,v
retrieving revision 1.41
diff -c -r1.41 inspect.py
*** Lib/inspect.py	19 Jan 2003 13:21:20 -0000	1.41
--- Lib/inspect.py	11 Feb 2003 22:35:41 -0000
***************
*** 16,22 ****
      getmodule() - determine the module that an object came from
      getclasstree() - arrange classes so as to represent their hierarchy
  
!     getargspec(), getargvalues() - get info about function arguments
      formatargspec(), formatargvalues() - format an argument spec
      getouterframes(), getinnerframes() - get info about frames
      currentframe() - get the current stack frame
--- 16,22 ----
      getmodule() - determine the module that an object came from
      getclasstree() - arrange classes so as to represent their hierarchy
  
!     getargspec(), getargvalues() - get info about function or method's arguments
      formatargspec(), formatargvalues() - format an argument spec
      getouterframes(), getinnerframes() - get info about frames
      currentframe() - get the current stack frame
***************
*** 625,636 ****
      return args, varargs, varkw
  
  def getargspec(func):
!     """Get the names and default values of a function's arguments.
  
      A tuple of four things is returned: (args, varargs, varkw, defaults).
      'args' is a list of the argument names (it may contain nested lists).
      'varargs' and 'varkw' are the names of the * and ** arguments or None.
      'defaults' is an n-tuple of the default values of the last n arguments."""
      if not isfunction(func): raise TypeError, 'arg is not a Python function'
      args, varargs, varkw = getargs(func.func_code)
      return args, varargs, varkw, func.func_defaults
--- 625,637 ----
      return args, varargs, varkw
  
  def getargspec(func):
!     """Get the names and default values of a function or method's arguments.
  
      A tuple of four things is returned: (args, varargs, varkw, defaults).
      'args' is a list of the argument names (it may contain nested lists).
      'varargs' and 'varkw' are the names of the * and ** arguments or None.
      'defaults' is an n-tuple of the default values of the last n arguments."""
+     if ismethod(func): func = func.im_func
      if not isfunction(func): raise TypeError, 'arg is not a Python function'
      args, varargs, varkw = getargs(func.func_code)
      return args, varargs, varkw, func.func_defaults


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=620190&group_id=5470