dtml-var weirdness
Hi everyone, Stupid question of the day: Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0. <dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if> Sometimes the next line results in a KeyError, value 'xyz': <dtml-var name="xyz"> In such cases, this works fine: <dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with> Huh? TIA, -- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
you mean <dtml-var xyz> or <dtml-var expr="_['xyz']"> ??? - aj ----- Original Message ----- From: "Joseph A Knapka" <jknapka@earthlink.net> To: <zope@zope.org> Sent: Friday, February 15, 2002 20:44 Subject: [Zope] dtml-var weirdness
Hi everyone,
Stupid question of the day:
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with>
Huh?
TIA,
-- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Andreas Jung wrote:
you mean <dtml-var xyz> or <dtml-var expr="_['xyz']"> ???
My understanding is that <dtml-var xyz> is exactly equivalent to <dtml-var name="xyz">. In any case, the code fails in exactly the same manner no matter which syntax is used. Thanks, -- Joe
- aj
----- Original Message ----- From: "Joseph A Knapka" <jknapka@earthlink.net> To: <zope@zope.org> Sent: Friday, February 15, 2002 20:44 Subject: [Zope] dtml-var weirdness
Hi everyone,
Stupid question of the day:
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with>
Huh?
TIA,
-- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
----- Original Message ----- From: "Joseph A Knapka" <jknapka@earthlink.net> To: "Andreas Jung" <andreas@andreas-jung.com> Cc: <zope@zope.org> Sent: Saturday, February 16, 2002 06:28 Subject: Re: [Zope] dtml-var weirdness
Andreas Jung wrote:
you mean <dtml-var xyz> or <dtml-var expr="_['xyz']"> ???
My understanding is that <dtml-var xyz> is exactly equivalent to <dtml-var name="xyz">. In any case, the code fails in exactly the same manner no matter which syntax is used.
- dtml-var has no 'name' attribute but an 'expr' attribute - both notations are different. all stuff between "..." is treated as a Python expression. - try to output the REQUEST object before and after setting the variable. maybe this helps. - aj
dtml-var does indeed have a "name" attribute... jens On Saturday, February 16, 2002, at 12:52 , Andreas Jung wrote:
----- Original Message ----- From: "Joseph A Knapka" <jknapka@earthlink.net> To: "Andreas Jung" <andreas@andreas-jung.com> Cc: <zope@zope.org> Sent: Saturday, February 16, 2002 06:28 Subject: Re: [Zope] dtml-var weirdness
Andreas Jung wrote:
you mean <dtml-var xyz> or <dtml-var expr="_['xyz']"> ???
My understanding is that <dtml-var xyz> is exactly equivalent to <dtml-var name="xyz">. In any case, the code fails in exactly the same manner no matter which syntax is used.
- dtml-var has no 'name' attribute but an 'expr' attribute - both notations are different. all stuff between "..." is treated as a Python expression. - try to output the REQUEST object before and after setting the variable. maybe this helps.
- aj
On Sat, Feb 16, 2002 at 12:52:30PM -0500, Andreas Jung wrote:
- dtml-var has no 'name' attribute but an 'expr' attribute
WHAT? When and where dtml-var lost its name attribute? Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
Has there ever been one ? The docs don't mention a 'name' attribute. - aj ----- Original Message ----- From: "Oleg Broytmann" <phd@phd.pp.ru> To: "Andreas Jung" <andreas@andreas-jung.com> Cc: <zope@zope.org> Sent: Saturday, February 16, 2002 15:15 Subject: Re: [Zope] dtml-var weirdness
On Sat, Feb 16, 2002 at 12:52:30PM -0500, Andreas Jung wrote:
- dtml-var has no 'name' attribute but an 'expr' attribute
WHAT? When and where dtml-var lost its name attribute?
Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
On 16 Feb 2002, at 16:38, Andreas Jung wrote: (in response to a question about the name attribute of the var tag.
Has there ever been one ? The docs don't mention a 'name' attribute.
It's still in heavy use on zope.org, for example. And it is still documented on zope.org, too. The idiom <dtml-var name="..."> is scattered all over zope.org. Just for fun, try http://www.google.de/search?q=%22dtml- var+name%22+view_source+site%3Awww.zope.org This results in 96 hits, at this moment. Specifically, have a look at a) zope.org, usage http://www.zope.org/Products/all_products/view_source, line 27 "<dtml-if name="sequence-start"> <dtml-var name="document_title"> Products <dtml-var name="sequence-number"> ... b) zope.org, documentationnn http://www.zope.org/Documentation/Guides/DTML-HTML/DTML.4.1.html DTML Tag Syntax ... <dtml-var name="standard_html_header"> ... c) zopelabs http://www.zopelabs.com/cookbook/990721623 "knowing the difference between <dtml-var name="foo">; and <dtml-var expr="foo">; will surely make learning Zope a bit more easy." ...
- aj ----- Original Message ----- From: "Oleg Broytmann" <phd@phd.pp.ru> To: "Andreas Jung" <andreas@andreas-jung.com> Cc: <zope@zope.org> Sent: Saturday, February 16, 2002 15:15 Subject: Re: [Zope] dtml-var weirdness
On Sat, Feb 16, 2002 at 12:52:30PM -0500, Andreas Jung wrote:
- dtml-var has no 'name' attribute but an 'expr' attribute
WHAT? When and where dtml-var lost its name attribute?
Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
-- Wolfgang Strobl
On Sat, Feb 16, 2002 at 04:38:07PM -0500, Andreas Jung wrote:
Has there ever been one ? The docs don't mention a 'name' attribute.
It has it from the very beginning :) Don't know about documentation, but The Only Authorative Source of Informtion - the source code :) - says: args = parse_params(args, name=''... (DT_Var.py).
On Sat, Feb 16, 2002 at 12:52:30PM -0500, Andreas Jung wrote:
- dtml-var has no 'name' attribute but an 'expr' attribute
WHAT? When and where dtml-var lost its name attribute?
Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
[Joseph A Knapka]>
Stupid question of the day:
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with>
You set the property named "xyz" in the REQUEST, and you can be sure of referring to it if you say "REQUEST['xyz'] or "REQUEST.xyz", as in <dtml-var "REQUEST['xyz']"> Note that using "expr=" is redundant - you can always omit it. So your example could be written: <dtml-if "REQUEST.has_key('xyz')"> <dtml-else> <dtml-call "REQUEST.set('xyz','Hello world')"> </dtml-if> or, a bit simpler and more readable, <dtml-if "not REQUEST.has_key('xyz')"> <dtml-call "REQUEST.set('xyz','Hello world')"> </dtml-if> Referring to "REQUEST['xyz']" always works. But as long as no other namespace has been inserted into the mix, you can also get the value in any of these ways: 1) <dtml-var "xyz"> 2) <dtml-var xyz> 3) <dtml-var "_['xyz']> That's because REQUEST properties are specially made available to its owner's namespace for convenience. These last three versions all really ask for the same thing, just in slightly different ways: 1) asks for a Python variable named "xyz". Python looks for this variable in the namespace stack, which normally starts with the namepace of the dtml document containing the code (if it is in a dtml document, otherwise different rules apply). 2) asks Zope for a variable named "xyz". Zope looks in its namespace stack to find it. It's normally the same namespace stack that Python would be using, so this is equivalent to 1) in most cases. 3) is an alternative way to ask for the same thing as 2). It's advantage is that the name of the variable does not have to be known when you write the code - any string will do. If the code is in a dtml method, the method does not have its own namespace. What namespace it uses, and therefore whether it can find the right variable, depends on how it is called, thus the results can vary. There have been many threads in the list about this. Cheers, Tom P
"Thomas B. Passin" wrote:
[Joseph A Knapka]>
Stupid question of the day:
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with>
You set the property named "xyz" in the REQUEST, and you can be sure of referring to it if you say "REQUEST['xyz'] or "REQUEST.xyz", as in
<dtml-var "REQUEST['xyz']">
Yes, but why can I *not* be sure of referring to it by <dtml-var xyz>? (I forgot to mention, this code appears with a DTML document, not a method.)
Note that using "expr=" is redundant - you can always omit it.
Though the Zope docs recommend against doing so, and I can see why: <dtml-var "xyz"> vs <dtml-var xyz> have different semantics, and are visually not easy to differentiate.
So your example could be written:
<dtml-if "REQUEST.has_key('xyz')"> <dtml-else> <dtml-call "REQUEST.set('xyz','Hello world')"> </dtml-if>
or, a bit simpler and more readable,
<dtml-if "not REQUEST.has_key('xyz')"> <dtml-call "REQUEST.set('xyz','Hello world')"> </dtml-if>
Referring to "REQUEST['xyz']" always works. But as long as no other namespace has been inserted into the mix, you can also get the value in any of these ways:
1) <dtml-var "xyz"> 2) <dtml-var xyz> 3) <dtml-var "_['xyz']>
That's because REQUEST properties are specially made available to its owner's namespace for convenience.
These last three versions all really ask for the same thing, just in slightly different ways:
1) asks for a Python variable named "xyz". Python looks for this variable in the namespace stack, which normally starts with the namepace of the dtml document containing the code (if it is in a dtml document, otherwise different rules apply).
2) asks Zope for a variable named "xyz". Zope looks in its namespace stack to find it. It's normally the same namespace stack that Python would be using, so this is equivalent to 1) in most cases.
3) is an alternative way to ask for the same thing as 2). It's advantage is that the name of the variable does not have to be known when you write the code - any string will do.
If the code is in a dtml method, the method does not have its own namespace. What namespace it uses, and therefore whether it can find the right variable, depends on how it is called, thus the results can vary. There have been many threads in the list about this.
Yes, I understand all that. My question, to put it very explicitly, is this: Since I'm checking to see whether REQUEST has a key 'xyz', and setting it if not, then by the time we get to the <dtml-var xyz> line, REQUEST definitely *does* have an 'xyz' key; and since REQUEST is undoubtedly in the namespace (right?), how can <dtml-var xyz> possibly result in a KeyError? Thanks, -- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
[Joseph A Knapka]
"Thomas B. Passin" wrote:
Yes, I understand all that. My question, to put it very explicitly, is this:
Since I'm checking to see whether REQUEST has a key 'xyz', and setting it if not, then by the time we get to the <dtml-var xyz> line, REQUEST definitely *does* have an 'xyz' key; and since REQUEST is undoubtedly in the namespace (right?), how can <dtml-var xyz> possibly result in a KeyError?
Sounds mysterious, all right. Let's get clear on a few things, then maybe Dieter or someone else will know the answer. 1) Is it the case that nothing else every happens on your page between the setting of the xyz property in the REQUEST and the <dtml-var xyz> rendering? 2) When you say that you "sometimes" get an error, can you tell what has preceded it when you do and do not get an error? 3) Add a line after you assign the variable, like this: <dtml-if expr="REQUEST.has_key('xyz')"> The property already exists, and its value is ==<dtml-var "REQUEST.xyz">==<br> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> We just set the property<br> </dtml-if> This will let you know if there is a property with that name whose value is empty, or whether it is in fact getting set. The "== ==" characters are ther to let you see if there is an empty string. It may seem obvious that it has to get set, but best to cover all possiblitites when strange things happen. You could also add <dtml-var REQUEST> before and after to see whether the xyz property did get into the REQUEST after all. Let us know the results. Cheers, Tom P
"Thomas B. Passin" wrote:
[Joseph A Knapka]
Since I'm checking to see whether REQUEST has a key 'xyz', and setting it if not, then by the time we get to the <dtml-var xyz> line, REQUEST definitely *does* have an 'xyz' key; and since REQUEST is undoubtedly in the namespace (right?), how can <dtml-var xyz> possibly result in a KeyError?
Sounds mysterious, all right. Let's get clear on a few things, then maybe Dieter or someone else will know the answer.
1) Is it the case that nothing else every happens on your page between the setting of the xyz property in the REQUEST and the <dtml-var xyz> rendering?
Nothing else happens within the document - I've put the <dtml-var xyz> immediately after the if/then/else block to be sure. However, the document containing the problematic code is being invoked by another. I suspect that may be an important fact, which I apologize for omitting from my original post.
2) When you say that you "sometimes" get an error, can you tell what has preceded it when you do and do not get an error?
That was a bit disingenuous of me :-) By "sometimes" I mean that in a particular bit of production code the problem occurs, but I haven't been able to reproduce the problem in a simple, postable-to-the-list case. I constructed a test case that is identical in form AFAICT: a document D1 is invoked as a request handler, and that document invokes a second document D2 that contains the code in question. That test case works fine.
3) Add a line after you assign the variable, like this:
<dtml-if expr="REQUEST.has_key('xyz')"> The property already exists, and its value is ==<dtml-var "REQUEST.xyz">==<br> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> We just set the property<br> </dtml-if>
This will let you know if there is a property with that name whose value is empty, or whether it is in fact getting set. The "== ==" characters are ther to let you see if there is an empty string. It may seem obvious that it has to get set, but best to cover all possiblitites when strange things happen.
You could also add <dtml-var REQUEST> before and after to see whether the xyz property did get into the REQUEST after all.
Let us know the results.
Will do. Of course, as you've pointed out, it's not hard to work around the problem, and I've already done so, but I just want to understand what's really going on here. Software behaving in apparently mysterious ways does not inspire confidence :-) Thanks very much, -- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
Joseph A Knapka writes:
Stupid question of the day: This does not seem stupid at all!
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with> This appears to be a bug.
When you happen to reproduce it, we may be able to analyse its cause. Dieter
Dieter Maurer wrote:
Joseph A Knapka writes:
Stupid question of the day: This does not seem stupid at all!
Why might the following code result in a KeyError? It does in some contexts under Zope 2.5.0.
<dtml-if expr="REQUEST.has_key('xyz')"> <dtml-else> <dtml-call expr="REQUEST.set('xyz','Hello world')"> </dtml-if>
Sometimes the next line results in a KeyError, value 'xyz':
<dtml-var name="xyz">
In such cases, this works fine:
<dtml-with REQUEST only> <dtml-var name="xyz"> </dtml-with> This appears to be a bug.
When you happen to reproduce it, we may be able to analyse its cause.
OK. I have production code that demonstrates the problem, but I haven't got a simple test case to break yet. I'll attempt to have something analyzable within the next couple of days. Thanks, -- Joe "I should like to close this book by sticking out any part of my neck which is not yet exposed, and making a few predictions about how the problem of quantum gravity will in the end be solved." --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"
participants (7)
-
Andreas Jung -
Dieter Maurer -
Jens Vagelpohl -
Joseph A Knapka -
Oleg Broytmann -
Thomas B. Passin -
Wolfgang Strobl