[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher - BaseRequest.py:1.1.2.10 mapply.py:1.1.2.4

Tres Seaver tseaver@zope.com
Tue, 20 Nov 2001 10:06:01 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher
In directory cvs.zope.org:/tmp/cvs-serv29829/lib/python/Zope/Publisher

Modified Files:
      Tag: Zope-3x-branch
	BaseRequest.py mapply.py 
Log Message:
 - Yeehaw!

=== Zope3/lib/python/Zope/Publisher/BaseRequest.py 1.1.2.9 => 1.1.2.10 ===
                 add_steps = self._request_default
                 if add_steps is None:
-                    object, add_steps = publication.getDefaultTraversal(
-                        self, object)
+                    r = publication.getDefaultTraversal( self, object)
+                    if type(r) is type( '' ):
+                        add_steps = ( r, )
+                    else:
+                        object, add_steps = r
                 if add_steps:
                     to_traverse.extend(add_steps)
 


=== Zope3/lib/python/Zope/Publisher/mapply.py 1.1.2.3 => 1.1.2.4 ===
 _marker = []  # Create a new marker object.
 
-def mapply(object, positional=(), keyword={}, call=apply):
 
-    if hasattr(object,'__bases__'):
-        # Calling class constructors might be dangerous.
-        raise TypeError, "mapply() can not call class constructors"
-    else:
-        f=object
-        im=0
-        if hasattr(f, 'im_func'):
-            # It's a method.
-            im=1
-        elif not hasattr(f,'func_defaults'):
-            # It's not a function.  Maybe it's an instance.
-            if hasattr(f, '__call__'):
-                f=f.__call__
-                if hasattr(f, 'im_func'):
-                    # It is an instance with a __call__() method.
-                    im=1
-                elif not hasattr(f, 'func_defaults'):
-                    # The __call__ attribute is not a method.
-                    raise TypeError, "mapply() can not call %s" % `object`
-            else:
-                # We don't know what it is.
-                raise TypeError, "mapply() can not call %s" % `object`
-        # else it's a function.
-
-        if im:
-            f = f.im_func
-            c = f.func_code
-            defaults = f.func_defaults
-            names = c.co_varnames[1:c.co_argcount]
+def unwrapMethod( object ):
+    """ object -> ( unwrapped, wrapperCount )
+
+        Unwrap 'object' until we get to a real function, counting the
+        number of unwrappings.
+
+        Bail if we find a class or something we can't
+        idendify as callable.
+    """
+    wrapperCount = 0
+    unwrapped = object
+
+    while 1:
+        if hasattr(unwrapped,'__bases__'):
+            # Calling class constructors might be dangerous.
+            raise TypeError, "mapply() can not call class constructors"
+        if hasattr( unwrapped, 'func_code' ):
+            break
+        if hasattr( unwrapped, 'im_func' ):
+            unwrapped = unwrapped.im_func
+            wrapperCount += 1
+        elif hasattr( unwrapped, '__call__' ):
+            unwrapped = unwrapped.__call__
+            wrapperCount += 1
         else:
-            defaults = f.func_defaults
-            c = f.func_code
-            names = c.co_varnames[:c.co_argcount]
+            raise TypeError, "mapply() can not call %s" % `object`
+
+    return unwrapped, wrapperCount
+
+def mapply(object, positional=(), keyword={}, call=apply):
+
+    unwrapped, wrapperCount = unwrapMethod( object )
+    code = unwrapped.func_code
+    defaults = unwrapped.func_defaults
+    names = code.co_varnames[wrapperCount:code.co_argcount]
 
     nargs = len(names)
     if positional:
         args = list(positional)
         if len(args) > nargs:
             given = len(args)
-            if im:
-                given = given + 1
+            if wrapperCount:
+                given = given + wrapperCount
             raise TypeError, (
                 '%s() takes at most %d argument%s(%d given)' % (
-                getattr(f, '__name__', repr(object)), c.co_argcount,
-                (c.co_argcount > 1 and 's ' or ' '), given))
+                getattr(unwrapped, '__name__', repr(object)), code.co_argcount,
+                (code.co_argcount > 1 and 's ' or ' '), given))
     else:
         args = []
 
@@ -73,7 +74,7 @@
         if v is _marker:
             if index < nrequired:
                 raise TypeError, 'Missing argument to %s(): %s' % (
-                    getattr(f, '__name__', repr(object)), name)
+                    getattr(unwrapped, '__name__', repr(object)), name)
             else: v = defaults[index-nrequired]
         args.append(v)