@@ -156,43 +156,38 @@ def safe_markdown(value: Any) -> str:
156
156
_PRECOMPILED_BLOCK_PATTERN = re .compile (r"{% block \w+? %}" )
157
157
_PRECOMPILED_INCLUDE_PATTERN = re .compile (r"{% include '.+?' %}|{% include \".+?\" %}" )
158
158
_PRECOMPILED_HASH_COMMENT_PATTERN = re .compile (r"{# .+? #}" )
159
- # Workaround for bug in re module https://github.com/adafruit/circuitpython/issues/8525
160
159
_PRECOMPILED_BLOCK_COMMENT_PATTERN = re .compile (
161
- # TODO: Use r"{% comment ('.*?' |\".*?\" )?%}[\s\S]*?{% endcomment %}" without flags when fixed
162
- r"{% comment ('.*?' |\".*?\" )?%}.*?{% endcomment %}" ,
163
- 16 , # re.DOTALL flag
160
+ r"{% comment ('.*?' |\".*?\" )?%}[\s\S]*?{% endcomment %}"
164
161
)
165
162
_PRECOMPILED_TOKEN_PATTERN = re .compile (r"{{ .+? }}|{% .+? %}" )
166
163
167
164
168
- def _find_next_extends (template : bytes ) -> "re.Match[bytes]" :
165
+ def _find_next_extends (template : str ) :
169
166
return _PRECOMPILED_EXTENDS_PATTERN .search (template )
170
167
171
168
172
- def _find_next_block (template : bytes ) -> "re.Match[bytes]" :
169
+ def _find_next_block (template : str ) :
173
170
return _PRECOMPILED_BLOCK_PATTERN .search (template )
174
171
175
172
176
- def _find_next_include (template : bytes ) -> "re.Match[bytes]" :
173
+ def _find_next_include (template : str ) :
177
174
return _PRECOMPILED_INCLUDE_PATTERN .search (template )
178
175
179
176
180
- def _find_named_endblock (template : bytes , name : bytes ) -> "re.Match[bytes]" :
181
- return re .search (
182
- r"{% endblock " .encode ("utf-8" ) + name + r" %}" .encode ("utf-8" ), template
183
- )
177
+ def _find_named_endblock (template : str , name : str ):
178
+ return re .search (r"{% endblock " + name + r" %}" , template )
184
179
185
180
186
- def _exists_and_is_file (path : str ) -> bool :
181
+ def _exists_and_is_file (path : str ):
187
182
try :
188
183
return (os .stat (path )[0 ] & 0b_11110000_00000000 ) == 0b_10000000_00000000
189
184
except OSError :
190
185
return False
191
186
192
187
193
- def _resolve_includes (template : bytes ) -> bytes :
188
+ def _resolve_includes (template : str ) :
194
189
while (include_match := _find_next_include (template )) is not None :
195
- template_path = include_match .group (0 )[12 :- 4 ]. decode ( "utf-8" )
190
+ template_path = include_match .group (0 )[12 :- 4 ]
196
191
197
192
# TODO: Restrict include to specific directory
198
193
@@ -203,19 +198,19 @@ def _resolve_includes(template: bytes) -> bytes:
203
198
with open (template_path , "rt" , encoding = "utf-8" ) as template_file :
204
199
template = (
205
200
template [: include_match .start ()]
206
- + template_file .read (). encode ( "utf-8" )
201
+ + template_file .read ()
207
202
+ template [include_match .end () :]
208
203
)
209
204
return template
210
205
211
206
212
- def _check_for_unsupported_nested_blocks (template : bytes ):
207
+ def _check_for_unsupported_nested_blocks (template : str ):
213
208
if _find_next_block (template ) is not None :
214
209
raise ValueError ("Nested blocks are not supported" )
215
210
216
211
217
- def _resolve_includes_blocks_and_extends (template : bytes ) -> bytes :
218
- block_replacements : "dict[bytes, bytes ]" = {}
212
+ def _resolve_includes_blocks_and_extends (template : str ) :
213
+ block_replacements : "dict[str, str ]" = {}
219
214
220
215
# Processing nested child templates
221
216
while (extends_match := _find_next_extends (template )) is not None :
@@ -225,7 +220,7 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
225
220
with open (
226
221
extended_template_name , "rt" , encoding = "utf-8"
227
222
) as extended_template_file :
228
- extended_template = extended_template_file .read (). encode ( "utf-8" )
223
+ extended_template = extended_template_file .read ()
229
224
230
225
# Removed the extend tag
231
226
template = template [extends_match .end () :]
@@ -248,7 +243,7 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
248
243
249
244
if block_name in block_replacements :
250
245
block_replacements [block_name ] = block_replacements [block_name ].replace (
251
- r"{{ block.super }}" . encode ( "utf-8" ) , block_content
246
+ r"{{ block.super }}" , block_content
252
247
)
253
248
else :
254
249
block_replacements .setdefault (block_name , block_content )
@@ -265,16 +260,14 @@ def _resolve_includes_blocks_and_extends(template: bytes) -> bytes:
265
260
return _replace_blocks_with_replacements (template , block_replacements )
266
261
267
262
268
- def _replace_blocks_with_replacements (
269
- template : bytes , replacements : "dict[bytes, bytes]"
270
- ) -> bytes :
263
+ def _replace_blocks_with_replacements (template : str , replacements : "dict[str, str]" ):
271
264
# Replace blocks in top-level template
272
265
while (block_match := _find_next_block (template )) is not None :
273
266
block_name = block_match .group (0 )[9 :- 3 ]
274
267
275
268
# Self-closing block tag without default content
276
269
if (endblock_match := _find_named_endblock (template , block_name )) is None :
277
- replacement = replacements .get (block_name , "" . encode ( "utf-8" ) )
270
+ replacement = replacements .get (block_name , "" )
278
271
279
272
template = (
280
273
template [: block_match .start ()]
@@ -299,7 +292,7 @@ def _replace_blocks_with_replacements(
299
292
# Replace default content with replacement
300
293
else :
301
294
replacement = replacements [block_name ].replace (
302
- r"{{ block.super }}" . encode ( "utf-8" ) , block_content
295
+ r"{{ block.super }}" , block_content
303
296
)
304
297
305
298
template = (
@@ -311,15 +304,15 @@ def _replace_blocks_with_replacements(
311
304
return template
312
305
313
306
314
- def _find_next_hash_comment (template : bytes ) -> "re.Match[bytes]" :
307
+ def _find_next_hash_comment (template : str ) :
315
308
return _PRECOMPILED_HASH_COMMENT_PATTERN .search (template )
316
309
317
310
318
- def _find_next_block_comment (template : bytes ) -> "re.Match[bytes]" :
311
+ def _find_next_block_comment (template : str ) :
319
312
return _PRECOMPILED_BLOCK_COMMENT_PATTERN .search (template )
320
313
321
314
322
- def _remove_comments (template : bytes ) -> bytes :
315
+ def _remove_comments (template : str ) :
323
316
# Remove hash comments: {# ... #}
324
317
while (comment_match := _find_next_hash_comment (template )) is not None :
325
318
template = template [: comment_match .start ()] + template [comment_match .end () :]
@@ -331,7 +324,7 @@ def _remove_comments(template: bytes) -> bytes:
331
324
return template
332
325
333
326
334
- def _find_next_token (template : bytes ) -> "re.Match[bytes]" :
327
+ def _find_next_token (template : str ) :
335
328
return _PRECOMPILED_TOKEN_PATTERN .search (template )
336
329
337
330
@@ -343,10 +336,6 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
343
336
context_name : str = "context" ,
344
337
dry_run : bool = False ,
345
338
) -> "Generator[str] | str" :
346
- # Workaround for bug in re module https://github.com/adafruit/circuitpython/issues/6860
347
- # TODO: Remove .encode() and .decode() when bug is fixed
348
- template : bytes = template .encode ("utf-8" )
349
-
350
339
# Resolve includes, blocks and extends
351
340
template = _resolve_includes_blocks_and_extends (template )
352
341
@@ -363,10 +352,10 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
363
352
364
353
# Resolve tokens
365
354
while (token_match := _find_next_token (template )) is not None :
366
- token : str = token_match .group (0 ). decode ( "utf-8" )
355
+ token = token_match .group (0 )
367
356
368
357
# Add the text before the token
369
- if text_before_token := template [: token_match .start ()]. decode ( "utf-8" ) :
358
+ if text_before_token := template [: token_match .start ()]:
370
359
function_string += (
371
360
indent * indentation_level + f"yield { repr (text_before_token )} \n "
372
361
)
@@ -457,9 +446,7 @@ def _create_template_function( # pylint: disable=,too-many-locals,too-many-bran
457
446
458
447
# Add the text after the last token (if any)
459
448
if template :
460
- function_string += (
461
- indent * indentation_level + f"yield { repr (template .decode ('utf-8' ))} \n " #
462
- )
449
+ function_string += indent * indentation_level + f"yield { repr (template )} \n "
463
450
464
451
# If dry run, return the template function string
465
452
if dry_run :
0 commit comments