Edgewall Software

Opened 3 years ago

Closed 3 years ago

Last modified 18 months ago

#13404 closed defect (fixed)

ImportError: cannot import name 'soft_unicode' from 'jinja2.utils' with Jinja2 3.0.x — at Version 4

Reported by: Jun Omae Owned by: Jun Omae
Priority: normal Milestone: 1.5.4
Component: rendering Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

Make compatible with Jinja 3 and add workaround for AssertionError when Jinja 3.0.0 is used.

API Changes:
Internal Changes:

Description

======================================================================
ERROR: test (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test
Traceback (most recent call last):
  File "/opt/python/3.9.1/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/travis/build/edgewall/trac/trac/test.py", line 47, in <module>
    from trac.env import Environment
  File "/home/travis/build/edgewall/trac/trac/env.py", line 52, in <module>
    from trac.web.chrome import Chrome
  File "/home/travis/build/edgewall/trac/trac/web/chrome.py", line 40, in <module>
    from trac.mimeview.api import RenderingContext, get_mimetype
  File "/home/travis/build/edgewall/trac/trac/mimeview/__init__.py", line 14, in <module>
    from trac.mimeview.api import *
  File "/home/travis/build/edgewall/trac/trac/mimeview/api.py", line 70, in <module>
    from trac.resource import Resource
  File "/home/travis/build/edgewall/trac/trac/resource.py", line 21, in <module>
    from trac.util.presentation import classes
  File "/home/travis/build/edgewall/trac/trac/util/presentation.py", line 26, in <module>
    from jinja2.utils import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'jinja2.utils' (/home/travis/virtualenv/python3.9.1/lib/python3.9/site-packages/jinja2/utils.py)

Jinja2 2.11.3 says the following warning when calling soft_unicode() but no warnings when importing it.

$ ~/venv/py39/bin/python
Python 3.9.4 (default, Apr  9 2021, 01:15:05)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>> jinja2.__version__
'2.11.3'
>>> from jinja2.utils import soft_unicode
>>> soft_unicode('***')
<stdin>:1: DeprecationWarning: 'jinja2.utils.soft_unicode' will be removed in version 3.0. Use 'markupsafe.soft_unicode' instead.
'***'

Change History (4)

comment:1 by Jun Omae, 3 years ago

2 deprecated warnings are raised while running unit-test with Jinja2 3.0.0:

trac/util/presentation.py:133: DeprecationWarning: 'jinja2.Markup' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.Markup' instead.
  rv = Markup(rv)
/venv/py39/lib/python3.9/site-packages/jinja2/environment.py:119: DeprecationWarning: The 'with' extension is deprecated and will be removed in Jinja 3.1. This is built in now.
  result[extension.identifier] = extension(environment)

Also, 2 failures.

======================================================================
FAIL: test_timestamp_columns (trac.ticket.tests.report.ReportModuleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jun66j5/src/tracdev/git/trac/ticket/tests/report.py", line 410, in test_timestamp_columns
    rendered = Chrome(self.env).render_template(req, rv[0], rv[1],
  File "/home/jun66j5/src/tracdev/git/trac/web/chrome.py", line 1377, in render_template
    template, data = self.prepare_template(req, filename, data, text,
...
  File "/venv/py39/lib/python3.9/site-packages/jinja2/compiler.py", line 812, in pop_assign_tracking
    ref = frame.symbols.ref(name)
  File "/venv/py39/lib/python3.9/site-packages/jinja2/idtracking.py", line 80, in ref
    raise AssertionError(
AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('header_group')

======================================================================
FAIL: test_valid_html_for_report (trac.ticket.tests.report.ReportModuleTestCase)
----------------------------------------------------------------------
...
  File "/venv/py39/lib/python3.9/site-packages/jinja2/idtracking.py", line 80, in ref
    raise AssertionError(
AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('header_group')

FAILED (failures=2, skipped=5)

comment:2 by Jun Omae, 3 years ago

The AssertionError: Tried to resolve a name ... has been filed at https://github.com/pallets/jinja/issues/1427 and a workaround has been posted in the page.

Proposed changes in jomae.git@t13404, tested with Jinja 3.0.0 and 2.11.3.

comment:3 by Ryan J Ollos, 3 years ago

Changes look good. Do you think we should create another release soon since this upgrading Jinja2 will cause breakage in production?

comment:4 by Jun Omae, 3 years ago

Component: generalrendering
Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Thanks for the reviewing. Committed in [17543].

Hmm, I don't think new release is not necessary for this issue. We don't recommend to use 1.5.x releasees in production because it is for development. It is easy to downgrade to Jinja2<3 for 1.5.x users.

Last edited 3 years ago by Jun Omae (previous) (diff)
Note: See TracTickets for help on using tickets.