use fspath instead of str
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 1230796 · Browse files · 3 files·+40 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−21 removed
- other:+40 added−21 removed
Provenance
- Commit
b43531f- Tree
ded34f7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1230796
3 files changed+40 added−21 removed
modified:jinja2/_compat.py+19 added-5 removed
22 unmodified lines23 text_type = str24 string_types = (str,)25 integer_types = (int,)26 27- import pathlib28- path_types = (str, pathlib.PurePath)29-27 iterkeys = lambda d: iter(d.keys())28 itervalues = lambda d: iter(d.values())29 iteritems = lambda d: iter(d.items())30 21 unmodified lines52 range_type = xrange53 string_types = (str, unicode)54 integer_types = (int, long)55 59- path_types = string_types60-56 iterkeys = lambda d: d.iterkeys()57 itervalues = lambda d: d.itervalues()58 iteritems = lambda d: d.iteritems()59 42 unmodified lines102 try:103 from collections import abc104 except ImportError:105 import collections as abc106+107+108+try:109+ from os import fspath110+except ImportError:111+ try:112+ from pathlib import PurePath113+ except ImportError:114+ PurePath = None115+116+ def fspath(path):117+ if hasattr(path, "__fspath__"):118+ return path.__fspath__()119+120+ # Python 3.5 doesn't have __fspath__ yet, use str.121+ if PurePath is not None and isinstance(path, PurePath):122+ return str(path)123+124+ return pathmodified:jinja2/loaders.py+18 added-13 removed
15 unmodified lines16 from os import path17 from hashlib import sha118 from jinja2.exceptions import TemplateNotFound19 from jinja2.utils import open_if_exists, internalcode20-from jinja2._compat import string_types, path_types, iteritems20+from jinja2._compat import string_types, iteritems, fspath, abc21 22 23 def split_template_path(template):24 """Split a path into segments and perform a sanity check. If it detects128 unmodified lines153 To follow symbolic links, set the *followlinks* parameter to ``True``::154 155 >>> loader = FileSystemLoader('/path/to/templates', followlinks=True)156 157- .. versionchanged:: 2.8+158- The *followlinks* parameter was added.157+ .. versionchanged:: 2.8158+ The ``followlinks`` parameter was added.159 """160 161 def __init__(self, searchpath, encoding='utf-8', followlinks=False):162- if isinstance(searchpath, path_types):162+ if (163+ not isinstance(searchpath, abc.Iterable)164+ or isinstance(searchpath, string_types)165+ ):166 searchpath = [searchpath]167 165- # In Python 3.5, os.path.join only supports strings, not instances166- # of pathlib.Path. This line can be simplified to list(searchpath)167- # when support for Python 3.5 is dropped.168- self.searchpath = [str(path) for path in searchpath]168+ # In Python 3.5, os.path.join doesn't support Path. This can be169+ # simplified to list(searchpath) when Python 3.5 is dropped.170+ self.searchpath = [fspath(p) for p in searchpath]171 172 self.encoding = encoding173 self.followlinks = followlinks174 315 unmodified lines490 491 # create a fake module that looks for the templates in the492 # path given.493 mod = _TemplateModule(package_name)492- if isinstance(path, path_types):493- path = [str(path)]494- else:495- path = [str(p) for p in path]496- mod.__path__ = path494 495+ if (496+ not isinstance(path, abc.Iterable)497+ or isinstance(path, string_types)498+ ):499+ path = [path]500+501+ mod.__path__ = [fspath(p) for p in path]502+503 sys.modules[package_name] = weakref.proxy(mod,504 lambda x: sys.modules.pop(package_name, None))505 506 # the only strong reference, the sys.modules entry is weak30 unmodified linesmodified:CHANGES.rst+3 added-3 removed
47 unmodified lines48 ``revindex0`` when looping over a generator. :issue:`459, 751, 794`,49 :pr:`993`50 - ``PackageLoader`` doesn't depend on setuptools or pkg_resources.51 :issue:`970`52-- Allow passing ``pathlib.Path`` as the search path in53- :class:`~loader.FileSystemLoader` and54- :class:`~loader.ModuleLoader`. :issue:`870`52+- Support :class:`os.PathLike` objects in53+ :class:`~loader.FileSystemLoader` and :class:`~loader.ModuleLoader`.54+ :issue:`870`55 56 57 Version 2.10.358 --------------629 unmodified lines