Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

fix(bench): preserve backticks inside fenced Python replies (#129)

Afloatd62a6bd47c86
diff --git a/bench/evaluate.py b/bench/evaluate.py
index c0fa34a..6c2f8c2 100644
--- a/bench/evaluate.py
+++ b/bench/evaluate.py
@@ -20,11 +20,14 @@ import sys
import tempfile
import tokenize
-FENCE = re.compile(r"```[ \t]*([A-Za-z0-9_+-]*)[ \t]*\n(.*?)(?:```|\Z)", re.S)
+# A closing fence occupies its own line and is at least as long as the opener.
+# Inline backticks and shorter fences are part of the generated Python source.
+FENCE = re.compile(r"^ {0,3}(`{3,})[ \t]*([A-Za-z0-9_+-]*)[ \t]*\r?\n"
+ r"(.*?)(?:^ {0,3}\1`*[ \t]*(?:\r?\n|\Z)|\Z)", re.M | re.S)
def extract(text: str, entry: str) -> str:
- blocks = [b for lang, b in FENCE.findall(text) if lang.lower() in ("", "python", "py", "python3")]
+ blocks = [b for _, lang, b in FENCE.findall(text) if lang.lower() in ("", "python", "py", "python3")]
if not blocks:
return text
for b in blocks:
diff --git a/bench/test_evaluate_fences.py b/bench/test_evaluate_fences.py
new file mode 100644
index 0000000..c6ad007
--- /dev/null
+++ b/bench/test_evaluate_fences.py
@@ -0,0 +1,63 @@
+import unittest
+
+from evaluate import extract, program, run
+
+
+PROBLEM = {"entry_point": "answer", "src_prompt": "def answer():\n", "test": "assert answer() == 42"}
+
+
+class FencedCodeGradingTest(unittest.TestCase):
+ def assert_passes(self, reply, problem=PROBLEM):
+ for clean in (False, True):
+ with self.subTest(clean=clean):
+ self.assertEqual(run(program(problem, reply, clean)), (True, ""))
+
+ def test_backticks_inside_a_string_do_not_end_the_block(self):
+ self.assert_passes('```python\ndef answer():\n return "```"\n```',
+ {**PROBLEM, "test": 'assert answer() == "```"'})
+
+ def test_backticks_in_a_comment_do_not_end_the_block(self):
+ self.assert_passes('```python\n# Return the answer, not the ``` delimiter.\ndef answer():\n return 42\n```')
+
+ def test_shorter_fence_inside_a_longer_block_is_preserved(self):
+ self.assert_passes('````python\nEXAMPLE = """\n```\n"""\ndef answer():\n'
+ ' return 42 if EXAMPLE.strip() == "```" else 0\n````\n')
+
+ def test_backticks_followed_by_text_are_not_a_closing_fence(self):
+ self.assert_passes('```python\nEXAMPLE = """\n``` this is part of a string\n"""\n'
+ 'def answer():\n return 42\n```\n')
+
+ def test_longer_closing_fence_and_surrounding_prose(self):
+ self.assert_passes("Here is the solution:\n```python\ndef answer():\n return 42\n````\nExplanation follows.")
+
+ def test_closing_fence_allows_indentation_and_trailing_whitespace(self):
+ self.assert_passes("```python\ndef answer():\n return 42\n ``` \t\nExplanation follows.")
+
+ def test_language_aliases_and_crlf(self):
+ for language in ("", "python", "PY", "python3"):
+ with self.subTest(language=language):
+ self.assert_passes(f"```{language}\r\ndef answer():\r\n return 42\r\n```\r\n")
+
+ def test_block_defining_entry_point_is_still_selected(self):
+ self.assert_passes("```python\nx = 0\n```\n```text\nnot Python\n```\n"
+ "```py\ndef answer():\n return 42\n```\n")
+
+ def test_first_python_block_remains_the_fallback(self):
+ self.assertEqual(extract("```text\nignore\n```\n```python\nx = 1\n```\n```py\nx = 2\n```", "answer"),
+ "x = 1\n")
+
+ def test_unclosed_block_keeps_truncated_reply_for_sanitization(self):
+ source = "def answer():\n return 42\ndef unfinished("
+ reply = "```python\n" + source
+ self.assertEqual(extract(reply, "answer"), source)
+ self.assertEqual(run(program(PROBLEM, reply, True)), (True, ""))
+ self.assertFalse(run(program(PROBLEM, reply, False))[0])
+
+ def test_unfenced_code_is_unchanged(self):
+ source = 'def answer():\n return 42 # ``` is just a comment\n'
+ self.assertEqual(extract(source, "answer"), source)
+ self.assert_passes(source)
+
+
+if __name__ == "__main__":
+ unittest.main()