Skip to content
Myr

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

3 files+40 added−21 removed

modified:jinja2/_compat.py+19 added-5 removed
22 unmodified lines
23 text_type = str24 string_types = (str,)25 integer_types = (int,)2627 import pathlib28 path_types = (str, pathlib.PurePath)2927 iterkeys = lambda d: iter(d.keys())28 itervalues = lambda d: iter(d.values())29 iteritems = lambda d: iter(d.items())30
21 unmodified lines
52 range_type = xrange53 string_types = (str, unicode)54 integer_types = (int, long)5559 path_types = string_types6056 iterkeys = lambda d: d.iterkeys()57 itervalues = lambda d: d.itervalues()58 iteritems = lambda d: d.iteritems()59
42 unmodified lines
102try:103 from collections import abc104except ImportError:105 import collections as abc106107108try:109 from os import fspath110except ImportError:111 try:112 from pathlib import PurePath113 except ImportError:114 PurePath = None115116 def fspath(path):117 if hasattr(path, "__fspath__"):118 return path.__fspath__()119120 # Python 3.5 doesn't have __fspath__ yet, use str.121 if PurePath is not None and isinstance(path, PurePath):122 return str(path)123124 return path
modified:jinja2/loaders.py+18 added-13 removed
15 unmodified lines
16from os import path17from hashlib import sha118from jinja2.exceptions import TemplateNotFound19from jinja2.utils import open_if_exists, internalcode20from jinja2._compat import string_types, path_types, iteritems20from jinja2._compat import string_types, iteritems, fspath, abc212223def split_template_path(template):24 """Split a path into segments and perform a sanity check. If it detects
128 unmodified lines
153 To follow symbolic links, set the *followlinks* parameter to ``True``::154155 >>> loader = FileSystemLoader('/path/to/templates', followlinks=True)156157 .. versionchanged:: 2.8+158 The *followlinks* parameter was added.157 .. versionchanged:: 2.8158 The ``followlinks`` parameter was added.159 """160161 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]167165 # 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]171172 self.encoding = encoding173 self.followlinks = followlinks174
315 unmodified lines
490491 # 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__ = path494495 if (496 not isinstance(path, abc.Iterable)497 or isinstance(path, string_types)498 ):499 path = [path]500501 mod.__path__ = [fspath(p) for p in path]502503 sys.modules[package_name] = weakref.proxy(mod,504 lambda x: sys.modules.pop(package_name, None))505506 # the only strong reference, the sys.modules entry is weak
30 unmodified lines
modified:CHANGES.rst+3 added-3 removed
47 unmodified lines
48 ``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`555657Version 2.10.358--------------
629 unmodified lines