https://github.com/python/cpython/commit/1709020e8ebaf9bf1bc9ee14d56173c860613931
commit: 1709020e8ebaf9bf1bc9ee14d56173c860613931
branch: main
author: Erlend E. Aasland <[email protected]>
committer: erlend-aasland <[email protected]>
date: 2024-01-15T00:09:26+01:00
summary:
gh-113317: Move FormatCounterFormatter into libclinic (#114066)
files:
M Tools/clinic/clinic.py
M Tools/clinic/libclinic/__init__.py
M Tools/clinic/libclinic/utils.py
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index d45159f47a0989..c247bd075321cd 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -23,7 +23,6 @@
import pprint
import re
import shlex
-import string
import sys
import textwrap
@@ -270,24 +269,6 @@ def __init__(self) -> None:
self.unlock: list[str] = []
-class FormatCounterFormatter(string.Formatter):
- """
- This counts how many instances of each formatter
- "replacement string" appear in the format string.
-
- e.g. after evaluating "string {a}, {b}, {c}, {a}"
- the counts dict would now look like
- {'a': 2, 'b': 1, 'c': 1}
- """
- def __init__(self) -> None:
- self.counts = collections.Counter[str]()
-
- def get_value(
- self, key: str, args: object, kwargs: object # type: ignore[override]
- ) -> Literal['']:
- self.counts[key] += 1
- return ''
-
class Language(metaclass=abc.ABCMeta):
start_line = ""
@@ -341,7 +322,7 @@ def assert_only_one(
fields = ['dsl_name']
fields.extend(additional_fields)
line: str = getattr(self, attr)
- fcf = FormatCounterFormatter()
+ fcf = libclinic.FormatCounterFormatter()
fcf.format(line)
def local_fail(should_be_there_but_isnt: bool) -> None:
if should_be_there_but_isnt:
diff --git a/Tools/clinic/libclinic/__init__.py
b/Tools/clinic/libclinic/__init__.py
index f26bf3ef6d55fe..1b300b55acc21e 100644
--- a/Tools/clinic/libclinic/__init__.py
+++ b/Tools/clinic/libclinic/__init__.py
@@ -16,8 +16,9 @@
wrapped_c_string_literal,
)
from .utils import (
- create_regex,
+ FormatCounterFormatter,
compute_checksum,
+ create_regex,
write_file,
)
@@ -39,8 +40,9 @@
"wrapped_c_string_literal",
# Utility functions
- "create_regex",
+ "FormatCounterFormatter",
"compute_checksum",
+ "create_regex",
"write_file",
]
diff --git a/Tools/clinic/libclinic/utils.py b/Tools/clinic/libclinic/utils.py
index 1514558cb5665c..d2d09387a73d1e 100644
--- a/Tools/clinic/libclinic/utils.py
+++ b/Tools/clinic/libclinic/utils.py
@@ -1,6 +1,9 @@
+import collections
import hashlib
-import re
import os
+import re
+import string
+from typing import Literal
def write_file(filename: str, new_contents: str) -> None:
@@ -39,7 +42,27 @@ def create_regex(
group_re = r"\w+" if word else ".+"
before = re.escape(before)
after = re.escape(after)
- pattern = fr"{before}({group_re}){after}"
+ pattern = rf"{before}({group_re}){after}"
if whole_line:
- pattern = fr"^{pattern}$"
+ pattern = rf"^{pattern}$"
return re.compile(pattern)
+
+
+class FormatCounterFormatter(string.Formatter):
+ """
+ This counts how many instances of each formatter
+ "replacement string" appear in the format string.
+
+ e.g. after evaluating "string {a}, {b}, {c}, {a}"
+ the counts dict would now look like
+ {'a': 2, 'b': 1, 'c': 1}
+ """
+
+ def __init__(self) -> None:
+ self.counts = collections.Counter[str]()
+
+ def get_value(
+ self, key: str, args: object, kwargs: object # type: ignore[override]
+ ) -> Literal[""]:
+ self.counts[key] += 1
+ return ""
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]