Skip to content

Commit 9040af3

Browse files
committed
implement: Compile the set as the PDF generator does (t30)
1 parent dc8590b commit 9040af3

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎in2lambda/validation/pdf/__init__.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ def _compiled(fields: list[tuple[str, str]], images: list[str]) -> list[Problem]
122122
input=_marked_document(fields, available),
123123
capture_output=True,
124124
text=True,
125+
# Not the locale's encoding: a set holding any non-ASCII character would
126+
# then fail to even be handed over under, say, LC_ALL=C.
127+
encoding="utf-8",
125128
cwd=work,
126129
timeout=_TIMEOUT,
127130
)
128131
if run.returncode:
129132
return [Problem(_SET, f"pandoc cannot read the set: {run.stderr.strip()}")]
130133

131-
latex = (work / "set.tex").read_text()
134+
latex = (work / "set.tex").read_text(encoding="utf-8")
132135
run = subprocess.run(
133136
[
134137
"xelatex",
@@ -140,6 +143,7 @@ def _compiled(fields: list[tuple[str, str]], images: list[str]) -> list[Problem]
140143
stdin=subprocess.DEVNULL,
141144
capture_output=True,
142145
text=True,
146+
encoding="utf-8",
143147
cwd=work,
144148
timeout=_TIMEOUT,
145149
)

‎tests/test_validation.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
on the ``Summer2025`` branch.
1010
"""
1111

12+
import os
1213
import shutil
14+
import subprocess
15+
import sys
1316
from pathlib import Path
1417

1518
import pytest
@@ -133,6 +136,37 @@ def test_image_that_is_not_on_disk_is_reported(tmp_path: Path) -> None:
133136
]
134137

135138

139+
@needs_compiler
140+
def test_non_ascii_is_compiled_whatever_the_locale() -> None:
141+
"""Exports are full of curly quotes, and containers are often not UTF-8 locales.
142+
143+
Run in a process of its own because the locale is read when Python starts.
144+
"""
145+
script = (
146+
"from in2lambda.api.set import Set\n"
147+
"from in2lambda.validation import validate\n"
148+
"question_set = Set()\n"
149+
"question_set.add_question('Quotes', 'The rocket\\u2019s mass.')\n"
150+
"print(len(validate(question_set)))\n"
151+
)
152+
run = subprocess.run(
153+
[sys.executable, "-c", script],
154+
env=os.environ
155+
| {
156+
"LC_ALL": "C",
157+
"LANG": "C",
158+
"PYTHONUTF8": "0",
159+
"PYTHONCOERCECLOCALE": "0",
160+
"PYTHONIOENCODING": "utf-8",
161+
},
162+
capture_output=True,
163+
text=True,
164+
)
165+
166+
assert run.returncode == 0, run.stderr
167+
assert run.stdout.strip() == "0"
168+
169+
136170
def test_missing_compiler_says_what_to_install(
137171
monkeypatch: pytest.MonkeyPatch,
138172
) -> None:

0 commit comments

Comments
 (0)