Modify ↓
#13582 closed defect (fixed)
TypeError is raised from Chrome.get_navigation_items() with Babel
| Reported by: | Jun Omae | Owned by: | Jun Omae |
|---|---|---|---|
| Priority: | normal | Milestone: | 1.4.4 |
| Component: | i18n | Version: | 1.4.3 |
| Severity: | normal | Keywords: | |
| Cc: | Branch: | ||
| Release Notes: |
Fixed handling of util.html functions and navigation items for |
||
| API Changes: | |||
| Internal Changes: | |||
Description
I encountered the following error while developing th:AccountManagerPlugin with Trac 1.5.x and Python 3.
2023-03-25 06:14:25,244 Trac[main] ERROR: [127.0.0.1] Internal Server Error: <RequestWithSession "POST '/prefs/account'">, referrer 'http://127.0.0.1:46741/trac/prefs/account'
Traceback (most recent call last):
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/main.py", line 610, in dispatch_request
dispatcher.dispatch(req)
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/main.py", line 302, in dispatch
raise e
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/main.py", line 248, in dispatch
resp = chosen_handler.process_request(req)
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/prefs/web_ui.py", line 108, in process_request
resp = chosen_provider.render_preference_panel(req, panel_id)
File "/home/jun66j5/src/accountmanagerplugin.git/acct_mgr/web_ui.py", line 136, in render_preference_panel
data.update(self._do_account(req))
File "/home/jun66j5/src/accountmanagerplugin.git/acct_mgr/web_ui.py", line 248, in _do_account
if self._do_change_password(req) and force_change_password:
File "/home/jun66j5/src/accountmanagerplugin.git/acct_mgr/web_ui.py", line 286, in _do_change_password
add_notice(req, _("Password updated successfully."))
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/chrome.py", line 213, in add_notice
_add_message(req, 'notices', msg, args)
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/chrome.py", line 221, in _add_message
if msg not in req.chrome[name]:
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/api.py", line 569, in __getattr__
value = self.callbacks[name](self)
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/chrome.py", line 818, in prepare_request
chrome['nav'] = self.get_navigation_items(req, handler)
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/trac/web/chrome.py", line 938, in get_navigation_items
if attributes['enabled'] and attributes['link'] and \
File "/home/jun66j5/src/accountmanagerplugin.git/.tox/py39-trac15/lib/python3.9/site-packages/babel/support.py", line 207, in __len__
return len(self.value)
TypeError: object of type 'Fragment' has no len()
The root cause is that babal.support.LazyProxy doesn't have __bool__ method. However, we should check whether the instance is a LazyProxy before checking whether the instance is a Fragment or Element.
-
trac/util/html.py
diff --git a/trac/util/html.py b/trac/util/html.py index 3e19c5268..3430257b2 100644
a b def escape(text, quotes=True): 95 95 """ 96 96 if isinstance(text, Markup): 97 97 return text 98 if LazyProxy and isinstance(text, LazyProxy): 99 text = text.value 98 100 if isinstance(text, Fragment): 99 101 return Markup(text) 100 102 e = escape_quotes(text) … … def plaintext(text, keeplinebreaks=True): 968 970 :param keeplinebreaks: optionally keep linebreaks 969 971 970 972 """ 973 if LazyProxy and isinstance(text, LazyProxy): 974 text = text.value 971 975 if isinstance(text, Fragment): 972 976 text = text.as_text() 973 977 else: … … def find_element(frag, attr=None, cls=None, tag=None): 982 986 attribute, class or tag, using a preorder depth-first search. 983 987 984 988 """ 989 if LazyProxy and isinstance(frag, LazyProxy): 990 frag = frag.value 985 991 if isinstance(frag, Element): 986 992 if attr is not None and attr in frag.attrib: 987 993 return frag -
trac/web/chrome.py
diff --git a/trac/web/chrome.py b/trac/web/chrome.py index 4a559a142..95bd81818 100644
a b from functools import partial 34 34 35 35 from jinja2 import FileSystemLoader 36 36 37 try: 38 import babel 39 except ImportError: 40 babel = LazyProxy = None 41 else: 42 from babel.support import LazyProxy 43 37 44 from trac.api import IEnvironmentSetupParticipant, ISystemInfoProvider 38 45 from trac.config import * 39 46 from trac.core import * … … class Chrome(Component): 640 647 info = get_pkginfo(jinja2).get('version') 641 648 yield 'Jinja2', info 642 649 # Optional Babel 643 try:644 import babel645 except ImportError:646 babel = None647 650 if babel is not None: 648 651 info = get_pkginfo(babel).get('version') 649 652 if not get_available_locales(): … … class Chrome(Component): 877 880 label = section.get(name + '.label') 878 881 if href and href.startswith('/'): 879 882 href = req.href + href 883 if LazyProxy and isinstance(text, LazyProxy): 884 text = text.value 880 885 if isinstance(text, Element) and text.tag == 'a': 881 886 link = text 882 887 if label:
Attachments (0)
Change History (2)
comment:1 by , 3 years ago
| Milestone: | 1.6 → 1.4.4 |
|---|---|
| Owner: | set to |
| Status: | new → assigned |
| Version: | 1.5.4 → 1.4.3 |
comment:2 by , 3 years ago
| Release Notes: | modified (diff) |
|---|---|
| Resolution: | → fixed |
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.



The same issue exists in Trac 1.4.