#13449 closed defect (fixed)
Exception: 'NameError: name 'reload' is not defined' when using mod_python with Python 3.x
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | normal | Milestone: | 1.5.4 |
Component: | web frontend/mod_python | Version: | 1.5.3 |
Severity: | blocker | Keywords: | patch |
Cc: | Branch: | ||
Release Notes: |
Fix use of |
||
API Changes: | |||
Internal Changes: |
Description
When Trac 1.5.3 is used in combination with mod_python on an Apache2 server, together in combination with Python 3.9, a 'NameError: name 'reload' is not defined' exception is raised.
The reload
function which was a builtin function in Python 2.x has moved to the imp
module or the importlib
module depending on the actually used Python 3.x version.
Because this problem in present in the modpython_frontend.py
it will only be present when mod_python is used. A 'tracd' installation will not show this problem.
This is also the only reload
call that is present in the complete trac sourcecode.
The relevant python backtrace is:
PythonHandler trac.web.modpython_frontend: NameError: name 'reload' is not defined PythonHandler trac.web.modpython_frontend: Traceback (most recent call last): PythonHandler trac.web.modpython_frontend: File "/usr/lib/python3/dist-packages/mod_python/apache.py", line 398, in HandlerDispatch\n result = obj(req) PythonHandler trac.web.modpython_frontend: File "/usr/local/lib/python3.9/dist-packages/trac/web/modpython_frontend.py", line 146, in handler\n reload(sys.modules['trac.web'])
A diff patch to resolve this problem is also attached.
Attachments (1)
Change History (5)
by , 3 years ago
Attachment: | mod_python_reload.diff added |
---|
comment:1 by , 3 years ago
Branch: | trunk |
---|---|
Milestone: | → 1.5.4 |
Owner: | set to |
Status: | new → assigned |
Thanks for the reporting. I missed that reload() has been removed in Python 3.0.
$ pyflakes trac/web/modpython_frontend.py
trac/web/modpython_frontend.py:146: undefined name 'reload'
We could use simply importlib.reload()
, because Trac 1.5.3 requires Python 3.5+.
-
trac/web/modpython_frontend.py
diff --git a/trac/web/modpython_frontend.py b/trac/web/modpython_frontend.py index afd6cbf21..ebe813126 100644
a b 16 16 # Author: Christopher Lenz <cmlenz@gmx.de> 17 17 # Matthew Good <trac@matt-good.net> 18 18 19 import importlib 19 20 import os 20 21 import pkg_resources 21 22 import sys … … def handler(req): 143 144 egg_cache = req.subprocess_env.get('PYTHON_EGG_CACHE') 144 145 if egg_cache: 145 146 pkg_resources.set_extraction_path(egg_cache) 146 reload(sys.modules['trac.web'])147 importlib.reload(sys.modules['trac.web']) 147 148 pkg_resources.require('Trac==%s' % VERSION) 148 149 gateway = ModPythonGateway(req, req.get_options()) 149 150 from trac.web.main import dispatch_request
comment:2 by , 3 years ago
Ok. I just wanted to provide a generic solution that works for every used Python version.
Because Python ≥ 3.5 is a requirement for Trac 1.5, it is indeed also ok, and simpler to use the changes you suggested.
Can you apply your diff on trunk?
comment:3 by , 3 years ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Committed in [17571].
comment:4 by , 3 years ago
Owner: | changed from | to
---|
Patch: Use reload from the correct module depending on the running Python version