Commits

Walter Dörwald committed 558f4cc

Neue Version von Signature.py benutzt.

  • Participants
  • Parent commits fe2e7d9

Comments (0)

Files changed (2)

examples/doc/Signature.py

-__version__ = 0,1,1
-
+__version__ = 0,1,2
 __doc__ = """\
 This module, Signature, contains a single class Signature. This class
 permits the convenient examination of the call signatures of Python
     ...     return (x+y)**z
     ...
     >>> f = Signature(foo)
-
+    
     >>> print 'ordinary arglist:', f.ordinary_args()
     ordinary arglist: ('x', 'y', 'z')
-
+    
     >>> print 'special_args:', f.special_args()
     special_args: {'keyword': 'kw', 'positional': 'args'}
-
+    
     >>> print 'full_arglist:', f.full_arglist()
     full_arglist: ['x', 'y', 'z', 'args', 'kw']
-
+    
     >>> print 'defaults:', f.defaults()
     defaults: {'z': -1.0}
-
+    
     >>> print 'signature:', str(f)
     signature: foo(x, y, z=-1.0, *args, **kw)
 
 
   If there are no arguments with default values, then an empty dictionary
   is returned. The special arguments specified with the '*' and '**'
-  syntax are not considered.
+  syntax are not considered. 
 
 
 o Signature.__str__()
   Behavior:
 
   While it's impossible to exactly match the actual declaration, in most
-  cases this should look pretty close.
+  cases this should look pretty close. 
 """
 
 import types, string
 
 class Signature:
-    # Magic numbers: These are the bit masks in func_code.co_flags that
-    # reveal whether or not the function has a *arg or **kw argument.
-    #
-    POS_LIST = 4
-    KEY_DICT = 8
     def __init__(self, func):
         self.type = type(func)
         self.name, self.func = _getcode(func)
     def ordinary_args(self):
         n = self.func.func_code.co_argcount
-        return self.func.func_code.co_varnames[0:n]
+        return _varnames(self.func)[0:n]
     def special_args(self):
         n = self.func.func_code.co_argcount
         x = {}
         #
-        #
-        #
-        if self.func.func_code.co_flags & (self.POS_LIST|self.KEY_DICT):
-             x['positional'] = self.func.func_code.co_varnames[n]
-             x['keyword'] = self.func.func_code.co_varnames[n+1]
-        elif self.func.func_code.co_flags & self.POS_LIST:
-            x['positional'] = self.func.func_code.co_varnames[n]
-        elif self.func.func_code.co_flags & self.KEY_DICT:
-            x['keyword'] = self.func.func_code.co_varnames[n]
+        if _is_variadic(self.func):
+            x['positional'] = _varnames(self.func)[n]
+            if _has_keywordargs(self.func):
+                x['keyword'] = _varnames(self.func)[n+1]
+        elif _has_keywordargs(self.func):
+            x['keyword'] = _varnames(self.func)[n]
+        else:
+            pass
         return x
     def full_arglist(self):
         base = list(self.ordinary_args())
             l.append( '**' + specials['keyword'] )
         return "%s(%s)" % (self.name, string.join(l, ', '))
 
+# Magic numbers: These are the bit masks in func_code.co_flags that
+# reveal whether or not the function has a *arg or **kw argument.
+
+_POS_LIST = 4
+_KEY_DICT = 8
+
+def _is_variadic(function):
+    return function.func_code.co_flags & _POS_LIST
+
+def _has_keywordargs(function):
+    return function.func_code.co_flags & _KEY_DICT
+
+def _varnames(function):
+    return function.func_code.co_varnames
+
 def _getcode(f):
     """_getcode(f)
 
-    This function returns the name and """
+    This function returns the name and function object of a callable
+    object."""
     def method_get(f):
         return f.__name__, f.im_func
     def function_get(f):
         if hasattr(f, '__init__'):
             return f.__name__, f.__init__.im_func
         else:
-            return f.__name, lambda: None
+            return f.__name__, lambda: None
     codedict = { types.UnboundMethodType: method_get,
                  types.MethodType       : method_get,
                  types.FunctionType     : function_get,

examples/doc/doc.py

 		for a in sig.ordinary_args():
 			_a = elements.arg(name = a)
 			if defaults.has_key(a):
-				_a["default"] = str(defaults[a])
+				_a["default"] = repr(defaults[a])
 			xmlsig.append(_a)
 		if specials.has_key('positional'):
 			xmlsig.append(elements.arg(name = specials['positional'],type="positional"))