Bug in ExternalMethods?
I think I've found a bug in ExternalMethod, but I don't know if it's already been fixed or not. Basically, the call signature of ExternalMethods seems to get mixed up sometimes. I'm running Zope 2.5.1 on Linux. 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)) ---- --Script (Python)-- return container.test('tim', 10, a_named_arg=(1,1)) return container.test('tim', 10, (1,1)) ---- You can see the dodgy behaviour by switching between the two test() calls in the Script (Python). tim
Tim Hicks writes:
I think I've found a bug in ExternalMethod, but I don't know if it's already been fixed or not.
Basically, the call signature of ExternalMethods seems to get mixed up sometimes. I'm running Zope 2.5.1 on Linux.
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)) ----
--Script (Python)-- return container.test('tim', 10, a_named_arg=(1,1)) return container.test('tim', 10, (1,1)) ----
You can see the dodgy behaviour by switching between the two test() calls in the Script (Python). This is not a bug but a documented "feature".
The automatic "self" passing magic only happens when you call the method with one positional argument less than required. (This does not apply to ZPublisher which uses a different way.) Your External Method has 3 required and one optional argument. In your first case, you pass 2 required positional arguments and give the optional a different value. As one required positional argument is missing, "self" is passed in automatically. In your second case, you pass 3 required positional arguments. "self" is not passed in automatically passed in automatically. Some say magic is bad .... Pass your self explicitly! Dieter
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
participants (3)
-
Clemens Robbenhaar -
Dieter Maurer -
Tim Hicks