Skip to content

Keep parser functions containing < or > as templates so enclosing templates are not lost - #375

Open
chinh13eboi2luae-ui wants to merge 1 commit into
earwig:mainfrom
chinh13eboi2luae-ui:fix/earwig/mwparserfromhell/370
Open

chinh13eboi2luae-ui wants to merge 1 commit into
earwig:mainfrom
chinh13eboi2luae-ui:fix/earwig/mwparserfromhell/370

Conversation

@chinh13eboi2luae-ui

Copy link
Copy Markdown

Fixes #370.

Problem
A parser function whose first argument contains < or > (for example {{#ifexpr: {{X}} <= 2 | ... }}) was not tokenized as a template. The tokenizer treats </> in a template name as invalid. When such a parser function was nested inside another template, the outer templates were dropped as well. For example, {{a|b={{s|{{#ifexpr: {{X}} <= 2 | {{Y}} | {{Z}} }}|02}}}} returned top-level templates ['X', 'Y', 'Z'] instead of ['a']. The same happened with the infobox begins = {{start date|df=yes| {{#ifexpr: {{CURRENTMONTH}} <= 2 | ... }} |02|15}} line from the issue.

Change

  • _verify_safe (Python) and Tokenizer_verify_safe (C) now allow < and > while reading a template name if a top-level colon already appears in it. Only the text from the start of the name up to the first { is checked for the colon. In parser functions, the text after the colon is an argument, not a page title.
  • This also affects other names with a colon before < or >, such as {{DISPLAYTITLE:<i>X</i>}} or {{Foo:a<b}}. These now stay templates. Names without a colon, such as {{a<b|c}}, are still rejected as before. If the colon only appears after a nested template in the name, the old behavior is kept.
  • I added three tokenizer cases to tests/tokenizer/templates.mwtest: <= in a parser function, >= in a parser function, and the nested case from the issue.
  • I added a CHANGELOG entry.

Validation (local Docker, Python 3.12)

  • Only the pure-Python tokenizer was tested. The image has no C compiler, so the tok_parse.c change was not compiled or run, and the CTokenizer tests were skipped. The new .mwtest cases also run against CTokenizer when the extension is built, so CI should cover the C path.
  • Before the patch, the three new .mwtest cases failed with PyTokenizer. A standalone reproduction also failed for all four issue inputs: <=, >=, 1 <= 2, and the infobox line.
  • With the patch, the reproduction passed in two repeated runs. Each run showed exactly one top-level template, the expected parameter containing the middle template, and an exact round-trip of str(parse(text)).
  • Behavior checks passed in both runs:
    • the issue's control inputs (= 2, no middle template, {{s|{{X}} < 2 | ...}})
    • a real <b> tag inside nested templates still parses as a tag
    • a trailing |c=3 parameter keeps its value
    • >= with no surrounding spaces works
    • {{a<b|c}} still is not a template
  • The project command pytest tests passed before and after the patch (1437 passed, 3 skipped after the patch), as did ruff check and ruff format --check.

Refs #370.

Validated with local Docker tests.

Docker validation

Base commit 3cb4dd29f264db331e9459c3302c313e3fe1d67f.

Tests ran locally in an isolated Linux container with networking disabled.

Command

PYTHONPATH=src python -m pytest tests -rxs -q -p no:cacheprovider && ruff check --no-cache src tests && ruff format --check --no-cache src tests

Base test output

========================== warnings summary ===============================
../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_tokenizer, argvalues type: generator
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_tokenizer, argvalues type: filter
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_roundtrip, argvalues type: generator
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
SKIPPED [1] tests/test_docs.py:120: cannot continue because of unsuccessful web call
SKIPPED [1] tests/test_tokenizer.py:139: CTokenizer not available
SKIPPED [1] tests/test_tokenizer.py:153: CTokenizer not available
1431 passed, 3 skipped, 3 warnings in 1.26s
All checks passed!
51 files already formatted

Patched test output

========================== warnings summary ===============================
../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_tokenizer, argvalues type: generator
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_tokenizer, argvalues type: filter
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

../../opt/venv/lib/python3.12/site-packages/_pytest/python.py:144
  /opt/venv/lib/python3.12/site-packages/_pytest/python.py:144: PytestRemovedIn10Warning: Passing a non-Collection iterable to parametrize is deprecated.
  Test: tests/test_tokenizer.py::test_roundtrip, argvalues type: generator
  Please convert to a list or tuple.
  See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
SKIPPED [1] tests/test_docs.py:120: cannot continue because of unsuccessful web call
SKIPPED [1] tests/test_tokenizer.py:139: CTokenizer not available
SKIPPED [1] tests/test_tokenizer.py:153: CTokenizer not available
1437 passed, 3 skipped, 3 warnings in 1.37s
All checks passed!
51 files already formatted

The added regression tests also failed against the original source and passed with the complete patch.

Independent behavioral verification

Input: mwparserfromhell.parse('{{a|b={{s|{{#ifexpr: {{X}} <= 2 | {{Y}} | {{Z}} }}|02}}}}'), plus the >= variant, the '1 <= 2' variant and the real infobox 'begins' line. All of them are parsed with the pure-Python Tokenizer (use_c=False). Observed: the outer template is missing from the top-level templates. Expected: the top-level templates are exactly [outer], the parameter contains the middle template, and str(parse(text)) == text. CTokenizer is not covered because this image cannot compile it.

Exercised the library interface. The original scenario reproduced the defect; the fixed scenario and 2 additional behavior checks passed in 2 consecutive runs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template lost when a parameter has <= or >= in a parser function nested in another template

1 participant