template variable reference fails

Tom Ekberg <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.user
Message-ID <DM6PR08MB47789B265AD64806EF2CFAC9CA7B0@DM6PR08MB4778.namprd08.prod.outlook.com>
I finally got back to working on this undefined tal variable problem. Yesterday I instrumented cgi/PageTemplates/PythonExpr.py but didn't get very far. I could see how the expression is evaluated (it defines function f that returns the expression), but couldn't see how local variables are made available to the scope of the expression. In the 1.5 roundup and python 2.7, the exec statement is used. In 1.6+ (tip) roundup and python 3.5 the exec function is used. If you google that you will find out that there are subtle differences between the two. I found these two stackoverflow postings interesting:


https://stackoverflow.com/questions/15086040/behavior-of-exec-function-in-python-2-and-python-3

https://stackoverflow.com/questions/24733831/using-a-function-defined-in-an-execed-string-in-python-3


Since I couldn't see how local variables were specified (only global variables) I gave up trying determine why I was getting the undefined variable reference error. Maybe someone else understands this and can explain it to me. The error page shows the variable defined (same spelling, value OK). I tried 9 different workarounds, fortunately, since I was running out of workarounds, the 9th one worked correctly.  I ended with this <td> element:


    <td
       tal:define="users python:sorted([user for user in db['user'].list() if user.username in sorted('list of usernames separated by commas'.split(','))], key=lambda x:x.realname.plain().lower());
           global user_realnames python:[x.realname for x in users];
           selected_realnames python:[x.realname.plain() for x in context.assignedto_multi];
           selected_ids python:[str(x.id) for x in context.assignedto_multi];
           non_cstaff_users python:[x for x in context.assignedto_multi if x.realname not in user_realnames]">

I had originally put the 'list of usernames separated by commas' with the sorted() part in a local varable tal:defined in the enclosing <table> element. This was the undefined variable error I posted in my first message on this topic. The variable was only referenced in one place so in the <td> element I substituted its value. Doing just that worked up until the last variable (non_cstaff_users) trying to reference user_realnames. The error page said that the variable user_realnames was undefined, the rest of the error page showed it as a variable with a reasonable value. After I made user_realnames a global variable the page came up without error. Note that the variable users is referenced in the define for user_realnames. I did NOT need to make variable users global to get that to work.

There is something strange going on here.


[cid:f26fda5e-415d-4930-b7b8-7bb2f0a45fc5]

_______________________________________________
Roundup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/roundup-users
Outlook-o03cjwz3.jpg (image/jpeg, 14.3 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.