adventure: return tuples from python scripts
Hello, We tried to return a tuple from a function and had strange results The following advanced python-scripts (the scripts are inside the quotes) "return (1,2)" , "return 1,2", "return tuple((1,2))" all return only 2 while the next scripts "return (1,2)," and "return ((1,2),)" both return ((1,2),) But now this one: "return tuple((1,2),)" it gives 2 Is this correct? To us it is rather funny, because Python is doing this 'correct'. Our Zope version is 2.5.0 Hans de Wit Stichting Farmaceutische Kengetallen Postbus 30460 2500 GL DEN HAAG Tel. 070-3737448 Fax 070-3737445
[<H.de.Wit@SFK.NL>]
We tried to return a tuple from a function and had strange results The following advanced python-scripts (the scripts are inside the quotes) "return (1,2)" , "return 1,2", "return tuple((1,2))"
all return only 2
while the next scripts "return (1,2)," and "return ((1,2),)"
both return ((1,2),)
But now this one: "return tuple((1,2),)" it gives 2
Is this correct? To us it is rather funny, because Python is doing this 'correct'.
Our Zope version is 2.5.0
I tried this out with Zope 2.3.3 on Windows 2000. When I "tested" the script using the "Test" tab, I got a variety of strange and incorrect results, none quite the same as what you describe. But when I called the script from a dtml page, the results displayed on the screen were (1,2) for all the variations I tried. If you haven't done it already, I'd suggest calling it from a dtml document and seeing what you get that way. Cheers, Tom P
H.de.Wit@SFK.NL wrote:
The following advanced python-scripts (the scripts are inside the quotes) "return (1,2)" , "return 1,2", "return tuple((1,2))"
all return only 2
This should only happen if you attempt to invoke the script directly with a URL, or otherwise cause the tuple to be returned as an HTTP response. Zope tries to render non-string results in a reasonable way, but 2-tuples are a special case. Zope interprets a 2-tuple response as (title, body). Cheers, Evan @ 4-am
H.de.Wit@SFK.NL writes:
We tried to return a tuple from a function and had strange results The following advanced python-scripts (the scripts are inside the quotes) "return (1,2)" , "return 1,2", "return tuple((1,2))"
all return only 2 No, they return the correct tuple.
However, "ZPublisher.HTTPResponse.setBody" does special things with some special return values. Most prominently: * an Python false value is translated into an empty response. This leads many browsers to wait forever... * a pair is interpreted as "title,body". That's your case. When you look at the HTML source, you will see your first element wrapped in a <title> and your second element in a <body> tag. Dieter
participants (4)
-
Dieter Maurer -
Evan Simpson -
H.de.Wit@SFK.NL -
Thomas B. Passin