Skip to content
Myr

more relative imports

Before adoption: this commit came in with the history the project adopted, so no landing records it.

_identifier exports a compiled regex instead of a string to avoid some tricky cleanup

authored by David Lord · committed by David Lord · parent 963b5d3 · Browse files · 8 files+32 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+32 added−33 removed

  • other:+32 added−33 removed

8 files+32 added−33 removed

modified:scripts/generate_identifier_pattern.py+7 added-4 removed
56 unmodified lines
57 return "".join(out)585960def main():61 """Build the regex pattern and write it to the file62 :file:`jinja2/_identifier.py`."""61 """Build the regex pattern and write it to62 ``jinja2/_identifier.py``.63 """64 pattern = build_pattern(collapse_ranges(get_characters()))65 filename = os.path.abspath(66 os.path.join(os.path.dirname(__file__), "..", "src", "jinja2", "_identifier.py")67 )6869 with open(filename, "w", encoding="utf8") as f:69 f.write("")70 f.write("import re\n\n")71 f.write("# generated by scripts/generate_identifier_pattern.py\n")72 f.write("pattern = re.compile(\n")71 f.write('pattern = "{}" # noqa: B950\n'.format(pattern))73 f.write(' r"[\\w{}]+" # noqa: B950\n'.format(pattern))74 f.write(")\n")757677if __name__ == "__main__":78 main()
modified:src/jinja2/_identifier.py+5 added-1 removed
1import re23# generated by scripts/generate_identifier_pattern.py2pattern = "·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛ࣔ-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣଁ-ଃ଼ା-ୄେୈୋ-୍ୖୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఃా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഁ-ഃാ-ൄെ-ൈൊ-്ൗൢൣංඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ູົຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭ᳲ-᳴᳸᳹᷀-᷵᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑅳𑄴𑆀-𑆂𑆳-𑇊𑇀-𑇌𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𖫰-𖫴𖬰-𖬶𖽑-𖽾𖾏-𖾒𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞣐𞀪-𞣖𞥄-𞥊󠄀-󠇯" # noqa: B9504pattern = re.compile(5 r"[\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛ࣔ-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣଁ-ଃ଼ା-ୄେୈୋ-୍ୖୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఃా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഁ-ഃാ-ൄെ-ൈൊ-്ൗൢൣංඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ູົຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭ᳲ-᳴᳸᳹᷀-᷵᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑅳𑄴𑆀-𑆂𑆳-𑇊𑇀-𑇌𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𖫰-𖫴𖬰-𖬶𖽑-𖽾𖾏-𖾒𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞣐𞀪-𞣖𞥄-𞥊󠄀-󠇯]+" # noqa: B9506)
modified:src/jinja2/asyncsupport.py+4 added-4 removed
140 unmodified lines
141 return TemplateModule(self, context, body_stream)142143144def patch_template():145 from jinja2 import Template145 from . import Template146147 Template.generate = wrap_generate_func(Template.generate)148 Template.generate_async = update_wrapper(generate_async, Template.generate_async)149 Template.render_async = update_wrapper(render_async, Template.render_async)
5 unmodified lines
155 )156157158def patch_runtime():159 from jinja2.runtime import BlockReference, Macro159 from .runtime import BlockReference, Macro160161 BlockReference.__call__ = wrap_block_reference_call(BlockReference.__call__)162 Macro._invoke = wrap_macro_invoke(Macro._invoke)163164165def patch_filters():166 from jinja2.filters import FILTERS167 from jinja2.asyncfilters import ASYNC_FILTERS166 from .filters import FILTERS167 from .asyncfilters import ASYNC_FILTERS168169 FILTERS.update(ASYNC_FILTERS)170171
99 unmodified lines
modified:src/jinja2/compiler.py+1 added-1 removed
718 unmodified lines
719 def visit_Template(self, node, frame=None):720 assert frame is None, "no root frame allowed"721 eval_ctx = EvalContext(self.environment, self.name)722723 from jinja2.runtime import __all__ as exported723 from .runtime import __all__ as exported724725 self.writeline("from __future__ import %s" % ", ".join(code_features))726 self.writeline("from jinja2.runtime import " + ", ".join(exported))727
1123 unmodified lines
modified:src/jinja2/environment.py+3 added-3 removed
365 unmodified lines
366 self.enable_async = enable_async367 self.is_async = self.enable_async and have_async_gen368 if self.is_async:369 # runs patch_all() to enable async support370 import jinja2.asyncsupport # noqa: F401370 from . import asyncsupport # noqa: F401371372 _environment_sanity_check(self)373374 def add_extension(self, extension):
342 unmodified lines
717 don't give much benefit.718719 .. versionadded:: 2.4720 """721 from jinja2.loaders import ModuleLoader721 from .loaders import ModuleLoader722723 if log_function is None:724725 def log_function(x):
105 unmodified lines
831 def handle_exception(self, source=None):832 """Exception handling helper. This is used internally to either raise833 rewritten exceptions or return a rendered traceback for the template.834 """835 from jinja2.debug import rewrite_traceback_stack835 from .debug import rewrite_traceback_stack836837 reraise(*rewrite_traceback_stack(source=source))838839 def join_path(self, template, parent):
528 unmodified lines
modified:src/jinja2/exceptions.py+2 added-2 removed
62 unmodified lines
63 def __init__(self, name, message=None):64 IOError.__init__(self, name)6566 if message is None:67 from jinja2.runtime import Undefined67 from .runtime import Undefined6869 if isinstance(name, Undefined):70 name._fail_with_undefined_error()71
20 unmodified lines
92 """9394 def __init__(self, names=(), message=None):95 if message is None:96 from jinja2.runtime import Undefined96 from .runtime import Undefined9798 parts = []99100 for name in names:
86 unmodified lines
modified:src/jinja2/lexer.py+4 added-13 removed
53 unmodified lines
54try:55 # check if this Python supports Unicode identifiers56 compile("föö", "<unknown>", "eval")57except SyntaxError:58 # no Unicode support, use ASCII identifiers58 # Python 2, no Unicode support, use ASCII identifiers59 name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*")60 check_ident = False61else:62 # Unicode support, build a pattern to match valid characters, and set flag63 # to use str.isidentifier to validate during lexing64 from jinja2 import _identifier62 # Unicode support, import generated re pattern and set flag to use63 # str.isidentifier to validate during lexing.64 from ._identifier import pattern as name_re6566 name_re = re.compile(r"[\w{0}]+".format(_identifier.pattern))66 check_ident = True68 # remove the pattern from memory after building the regex69 import sys6771 del sys.modules["jinja2._identifier"]72 import jinja27374 del jinja2._identifier75 del _identifier7668# internal the tokens and keep references to them69TOKEN_ADD = intern("add")70TOKEN_ASSIGN = intern("assign")71TOKEN_COLON = intern("colon")
780 unmodified lines
modified:src/jinja2/utils.py+6 added-5 removed
11 unmodified lines
12import os13import re14import warnings15from collections import deque16from random import choice17from random import randrange18from threading import Lock1920from markupsafe import escape21from markupsafe import Markup
84 unmodified lines
106 if is_undefined(var):107 return default108 return var109 """108 from jinja2.runtime import Undefined110 from .runtime import Undefined111112 return isinstance(obj, Undefined)113114
8 unmodified lines
123 used so that Jinja doesn't have to recreate environments and lexers all124 the time. Normally you don't have to care about that but if you are125 measuring memory consumption you may want to clean the caches.126 """125 from jinja2.environment import _spontaneous_environments126 from jinja2.lexer import _lexer_cache127 from .environment import _spontaneous_environments128 from .lexer import _lexer_cache129130 _spontaneous_environments.clear()131 _lexer_cache.clear()132
128 unmodified lines
261262263def generate_lorem_ipsum(n=5, html=True, min=20, max=100):264 """Generate some lorem ipsum for the template."""263 from jinja2.constants import LOREM_IPSUM_WORDS264 from random import choice, randrange265 from .constants import LOREM_IPSUM_WORDS266267 words = LOREM_IPSUM_WORDS.split()268 result = []269
467 unmodified lines