Edgewall Software

Opened 10 years ago

Last modified 8 years ago

#591 new defect

Changing outer-defined variables within py:for scope are lost for string but work for dict

Reported by: Ronan Jouchet <ronan@…> Owned by: cmlenz
Priority: major Milestone: 0.9
Component: Template processing Version: 0.6
Keywords: scoping, scope, typing, stack Cc:

Description

Problem

When defining variables in outermost scope, changes from within py:for scope work as expected for dictionaries (changes persist when leaving py:for scope), but are lost for strings when leaving the py:for scope.

Example

  1. Define variable in an initial template <?python ?> block:
<?python
  users = ['Bob', 'Alice']
  currentUser = users[0]
  dict = {'currentUser': users[0]}
?>
  1. Later in the template and in the context of a <py:for> block, update it:
<ul>
  <li py:for "user in users">
    <?python
    currentUser = user
    dict['currentUser'] = user
    # here, both the string and dict are properly updated
    ?>
  </li>
</ul>
  1. Later in the template and out of the previous <py:for> scope where the update happened, use the variable:
<span>
  ${currentUser}<br />
  ${dict['currentUser']}
</span>

Expected: both variables contain Alice

Actual: the dict properly contains 'Alice', but the string contains 'Bob'

Thoughts

A.1. This mailing-list post is not exactly my case. Unlike in the mailing list, my case does declare variables before using them in a <py:for> scope, but changes within the <py:for> are scrapped nevertheless.

A.2. The GenshiVsKid page also mentions "Proper scoping of variables. Names do not leak into outer scopes", but here the initial scope was the py:for outer one. Changes in inner scopes should be persisted in the initial scope.

  1. Plus, the fact that Genshi's behavior differs between Python types (dict vs. string) reinforces the feeling that this is a bug.

Environment

Problem is always reproducible, under both Genshi 0.6.1 and 0.7, in a fresh Python 2.7.6 virtualenv with the bare minimum required by the tutorial (pip install CherryPy FormEncode? Genshi).

I derived the official 'Geddit' tutorial into an example, see attachment, which renders into:

https://dl.dropboxusercontent.com/u/207077637/genshi_scope_bug_dictVsString.png

Attachments (1)

genshi_scope_bug_dictVsString.zip (1.7 KB) - added by Ronan Jouchet <ronan@…> 10 years ago.

Download all attachments as: .zip

Change History (2)

Changed 10 years ago by Ronan Jouchet <ronan@…>

comment:1 Changed 8 years ago by hodgestar

  • Milestone changed from 0.8 to 0.9

Moved to milestone 0.9.

Note: See TracTickets for help on using tickets.