[Zope-dev] Bug in ExternalMethods?

Clemens Robbenhaar zope-dev@zope.org
Thu, 19 Dec 2002 20:27:50 +0100


Hi Tim,

 > Here's a small demonstration of the bug:
 > 
 > --test.py--
 > def test(self, a_string, an_int, a_named_arg=1):
 >     """bug provoking?"""
 >     template =  """a_string = %s; type(a_string) -> %s\n
 > an_int = %s; type(an_int) -> %s\n
 > a_named_arg = %s; type(a_named_arg) -> %s"""
 >     return template % (a_string, type(a_string), an_int, type(an_int),
 > a_named_arg, type(a_named_arg))
 > ----

 External methods need no "self" (and normally should have no "self") as
first parameter, as they are not methods of some objects, I thought.
But Zope seems to do some magic here.

 > 
 > --Script (Python)--
 > return container.test('tim', 10, a_named_arg=(1,1))
 > return container.test('tim', 10, (1,1))
 > ----
 > 

 Indeed the first call should be illegal, as one passes only two
non-keyword arguments, but the function has three arguments without
default values. If I try to call the function from the python
command line in an equivalent way as done here by the script, I get an
TypeError: test() takes at least 3 non-keyword arguments (2 given).

 But Zope seems to mangle in the container/context into the "self" argument,
if the following two conditions hold:
 
 1. the first argument is named "self".
    (If the first argument is named "foo", it does not work.)
 2. there are to less arguments to make a "normal" function call


 Condition 2. is true for the first call, but not for the second one.
Here enough arguments are provided to make a plaing function call
with the arguments:

  self='tim', a_string=10, an_int=(1,1), a_named_arg=(1,1),

as defined by the caller or the default values.


 Thus I guess it is not a bug, but a feature. There is actually an
example for this usage in the Zope Book (with the image thumbnails), but
at least I failed to recognise the magic "self" mumble.


Cheers,
Clemens