|
| 1 | +from string.templatelib import Template |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from RestrictedPython import compile_restricted_exec |
| 6 | +from RestrictedPython._compat import IS_PY314_OR_GREATER |
| 7 | +from RestrictedPython.Eval import default_guarded_getattr |
| 8 | +from RestrictedPython.Eval import default_guarded_getiter |
| 9 | +from RestrictedPython.PrintCollector import PrintCollector |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.skipif( |
| 13 | + not IS_PY314_OR_GREATER, |
| 14 | + reason="t-strings were added in Python 3.14.", |
| 15 | +) |
| 16 | +def test_transform(): |
| 17 | + """It compiles a function call successfully and returns the used name.""" |
| 18 | + |
| 19 | + result = compile_restricted_exec('a = t"{max([1, 2, 3])}"') |
| 20 | + assert result.errors == () |
| 21 | + assert result.warnings == [ |
| 22 | + 'Line 1: TemplateStr statements are not yet allowed, please use f-strings or a real template engine instead.'] # NOQA: E501 |
| 23 | + assert result.code is not None |
| 24 | + loc = {} |
| 25 | + exec(result.code, {}, loc) |
| 26 | + template = loc['a'] |
| 27 | + assert isinstance(template, Template) |
| 28 | + assert template.values == (3, ) |
| 29 | + assert result.used_names == {'max': True} |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.skipif( |
| 33 | + not IS_PY314_OR_GREATER, |
| 34 | + reason="t-strings were added in Python 3.14.", |
| 35 | +) |
| 36 | +def test_visit_invalid_variable_name(): |
| 37 | + """Accessing private attributes is forbidden. |
| 38 | +
|
| 39 | + This is just a smoke test to validate that restricted exec is used |
| 40 | + in the run-time evaluation of t-strings. |
| 41 | + """ |
| 42 | + result = compile_restricted_exec('t"{__init__}"') |
| 43 | + assert result.errors == ( |
| 44 | + 'Line 1: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501 |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +t_string_self_documenting_expressions_example = """ |
| 49 | +from datetime import date |
| 50 | +from string.templatelib import Template, Interpolation |
| 51 | +
|
| 52 | +user = 'eric_idle' |
| 53 | +member_since = date(1975, 7, 31) |
| 54 | +
|
| 55 | +def render_template(template: Template) -> str: |
| 56 | + result = '' |
| 57 | + for part in template: |
| 58 | + if isinstance(part, Interpolation): |
| 59 | + if isinstance(part.value, str): |
| 60 | + result += part.value.upper() |
| 61 | + else: |
| 62 | + result += str(part.value) |
| 63 | + else: |
| 64 | + result += part.lower() |
| 65 | + return result |
| 66 | +
|
| 67 | +print(render_template(t'The User {user} is a member since {member_since}')) |
| 68 | +""" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.skipif( |
| 72 | + not IS_PY314_OR_GREATER, |
| 73 | + reason="t-strings were added in Python 3.14.", |
| 74 | +) |
| 75 | +def test_t_string_self_documenting_expressions(): |
| 76 | + """Checks if t-string self-documenting expressions is checked.""" |
| 77 | + result = compile_restricted_exec( |
| 78 | + t_string_self_documenting_expressions_example, |
| 79 | + ) |
| 80 | + # assert result.errors == ( |
| 81 | + # 'Line 13: TemplateStr statements are not allowed.', |
| 82 | + # ) |
| 83 | + # assert result.warnings == [ |
| 84 | + # 'Line 13: TemplateStr statements are not yet allowed, please use ' |
| 85 | + # 'f-strings or a real template engine instead.', |
| 86 | + # "Line None: Prints, but never reads 'printed' variable." |
| 87 | + # ] |
| 88 | + # assert result.code is None |
| 89 | + assert result.errors == () |
| 90 | + assert result.warnings == [ |
| 91 | + 'Line 20: TemplateStr statements are not yet allowed, ' |
| 92 | + 'please use f-strings or a real template engine instead.', |
| 93 | + "Line None: Prints, but never reads 'printed' variable."] |
| 94 | + assert result.code is not None |
| 95 | + |
| 96 | + glb = { |
| 97 | + '_print_': PrintCollector, |
| 98 | + '_getattr_': default_guarded_getattr, |
| 99 | + '_getiter_': default_guarded_getiter, |
| 100 | + '_inplacevar_': lambda x: x, |
| 101 | + } |
| 102 | + exec(result.code, glb) |
| 103 | + assert glb['_print']() == "user='eric_idle' member_since=datetime.date(1975, 7, 31)\n" # NOQA: E501 |
0 commit comments