Skip to content

Commit 9275b36

Browse files
authored
Fix e.g. "re.PatternError: bad escape \s at position" error (#40)
## Summary <!--AI:61d54311--> Fixing the regular expression pattern error in the Python script by adjusting the handling of escape sequences. This change resolves the issue where the script would throw a `re.PatternError` due to incorrect escape sequences like `\s` in regex patterns. The modification involves using a lambda function to ensure that the replacement string in `re.sub` calls is treated correctly, preventing the error from occurring. ### Essential Code Lines ```python new_body = re.sub(BODY_SUFFIX_RE, lambda _: suffix, body, flags=re.M | re.I | re.X) ``` ![CleanShot 2025-06-23 at 21 18 40@2x](https://github.com/user-attachments/assets/de52a321-89aa-46e9-9fad-f9755b11acbd) ## How was this tested? ``` python3 tests/test_ai.py ``` ## PRs in the Stack - ➡ #40 (The stack is managed by [git-grok](https://github.com/dimikot/git-grok).)
1 parent 701a0f1 commit 9275b36

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

git-grok

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,8 +2052,9 @@ def body_suffix_upsert(
20522052
BODY_SUFFIX_TITLE + "\n" + "\n".join(items) + "\n\n" + BODY_SUFFIX_FOOTER + "\n"
20532053
)
20542054
if re.search(BODY_SUFFIX_RE, body, flags=re.M | re.I | re.X):
2055-
return re.sub(BODY_SUFFIX_RE, suffix, body, flags=re.M | re.I | re.X)
2056-
return body.rstrip() + "\n\n" + suffix
2055+
return re.sub(BODY_SUFFIX_RE, lambda _: suffix, body, flags=re.M | re.I | re.X)
2056+
else:
2057+
return body.rstrip() + "\n\n" + suffix
20572058

20582059

20592060
#
@@ -2065,7 +2066,7 @@ def body_build_from_injected_text(
20652066
) -> str:
20662067
return re.sub(
20672068
r"\s*" + re.escape(AI_PLACEHOLDER) + r"\s*",
2068-
f"\n\n{injected_text.text}\n\n",
2069+
lambda _: f"\n\n{injected_text.text}\n\n",
20692070
injected_text.template_with_placeholder,
20702071
)
20712072

@@ -2095,7 +2096,7 @@ def body_convert_to_injected_text(
20952096
# Returns an AI hash comment.
20962097
#
20972098
def ai_hash_build(hash: str) -> str:
2098-
return re.sub(r"(?=-->)", ":" + hash, AI_PLACEHOLDER)
2099+
return re.sub(r"(?=-->)", lambda _: f":{hash}", AI_PLACEHOLDER)
20992100

21002101

21012102
#

tests/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ def git_init_and_cd_to_test_dir(
197197
# branch has already been deleted by a parallel test run or so).
198198

199199

200-
def git_touch(file: str):
201-
check_output_x("touch", file)
200+
def git_touch(file: str, content: str | None = None):
201+
with open(file, "w") as f:
202+
f.write(content or "")
202203

203204

204205
def git_add_commit(msg: str):

tests/test_ai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Test(TestCaseWithEmptyTestRepo):
1212
def test_ai(self):
1313
self.git_init_and_cd_to_test_dir(method="push.default")
14-
git_touch("commit01")
14+
git_touch("commit01", r"some \s text")
1515
git_add_commit("commit01")
1616
run_git_grok()
1717

0 commit comments

Comments
 (0)