Edgewall Software
Modify

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#13477 closed defect (fixed)

ImportError: cannot import name 'contextfilter' from 'jinja2'

Reported by: Ryan J Ollos Owned by: Jun Omae
Priority: normal Milestone: 1.5.4
Component: web frontend Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

Fixed incompatible with Jinja2 3.1.

API Changes:
Internal Changes:

Description

$ pip list | grep Jinja
Jinja2                        3.1.1
Traceback (most recent call last):
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/contrib/checkwiki.py", line 22, in <module>
    from trac.test import EnvironmentStub, Mock, MockPerm
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/test.py", line 47, in <module>
    from trac.env import Environment
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/env.py", line 52, in <module>
    from trac.web.chrome import Chrome
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/web/chrome.py", line 40, in <module>
    from trac.mimeview.api import RenderingContext, get_mimetype
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/mimeview/__init__.py", line 14, in <module>
    from trac.mimeview.api import *
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/mimeview/api.py", line 70, in <module>
    from trac.resource import Resource
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/resource.py", line 21, in <module>
    from trac.util.presentation import classes
  File "/Users/rjollos/Documents/Workspace/trac-dev/teo-rjollos.git/trac/util/presentation.py", line 24, in <module>
    from jinja2 import Undefined, contextfilter, evalcontextfilter
ImportError: cannot import name 'contextfilter' from 'jinja2' (/Users/rjollos/.pyenv/versions/trac-py310/lib/python3.10/site-packages/jinja2/__init__.py)
make: *** [update-help] Error 1

Attachments (0)

Change History (6)

comment:1 by anonymous, 2 years ago

https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-1-0

  • Drop support for Python 3.6.
  • Remove previously deprecated code.
    • contextfilter and contextfunction are replaced by pass_context.

comment:2 by Jun Omae, 2 years ago

Milestone: 1.4.41.5.4

Changing milestone to 1.5.x because Jinja2 doesn't support Python 2.7 since 3.0.0.

comment:3 by Jun Omae, 2 years ago

Verified unit tests passing with:

  • Jinja2 3.1.1 on Python 3.9
  • Jinja2 3.0.1 on Python 3.6, 3.7 and 3.8
  • Jinja2 2.11.3 on Python 3.5
  • trac/util/presentation.py

    commit 12146a3576d8e0f0629251564d0d863201643c8a (HEAD -> t13477)
    Author: Jun Omae <jun66j5@gmail.com>
    Date:   Mon Apr 11 18:54:23 2022 +0900
    
        (#13477) Use `pass_context` and `pass_eval_context` instead of deprecated `contextfilter` and `evalcontextfilter` since Jinja2 3.1
    
    diff --git a/trac/util/presentation.py b/trac/util/presentation.py
    index 23232d55c..b53a12c2b 100644
    a b from datetime import datetime  
    2121from math import ceil
    2222import re
    2323
    24 from jinja2 import Undefined, contextfilter, evalcontextfilter
     24from jinja2 import Undefined
    2525from jinja2.filters import make_attrgetter
     26try:
     27    from jinja2 import pass_context, pass_eval_context
     28except ImportError:
     29    from jinja2 import (contextfilter as pass_context,
     30                        evalcontextfilter as pass_eval_context)
    2631
    2732from trac.core import TracError
    2833from .datefmt import to_utimestamp, utc
    def jinja2_update(jenv):  
    7277
    7378# -- Jinja2 custom filters
    7479
    75 @evalcontextfilter
     80@pass_eval_context
    7681def htmlattr_filter(_eval_ctx, d, autospace=True):
    7782    """Create an SGML/XML attribute string based on the items in a dict.
    7883
    def group(iterable, num, predicate=None):  
    259264        yield tuple(buf)
    260265
    261266
    262 @contextfilter
     267@pass_context
    263268def groupattr_filter(_eval_ctx, iterable, num, attr, *args, **kwargs):
    264269    """Similar to `group`, but as an attribute filter."""
    265270    attr_getter = make_attrgetter(_eval_ctx.environment, attr)

comment:4 by Ryan J Ollos, 2 years ago

That looks good, thank you!

comment:5 by Jun Omae, 2 years ago

Release Notes: modified (diff)
Resolution: fixed
Status: newclosed

Thanks for the review! Pushed in [17591].

comment:6 by Jun Omae, 2 years ago

Owner: set to Jun Omae

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.