[Python-checkins] gh-104683: Argument clinic: modernise `cpp.Monitor` (#106698)

AlexWaygood webhook-mailer at python.org
Wed Jul 12 18:48:39 EDT 2023


https://github.com/python/cpython/commit/8aa4beaad0d95917b1bb12d146bc15c1aa815e08
commit: 8aa4beaad0d95917b1bb12d146bc15c1aa815e08
branch: main
author: Alex Waygood <Alex.Waygood at Gmail.com>
committer: AlexWaygood <Alex.Waygood at Gmail.com>
date: 2023-07-12T22:48:36Z
summary:

gh-104683: Argument clinic: modernise `cpp.Monitor` (#106698)

files:
M Tools/clinic/cpp.py

diff --git a/Tools/clinic/cpp.py b/Tools/clinic/cpp.py
index c1a2eeef22dec..fbac81336b545 100644
--- a/Tools/clinic/cpp.py
+++ b/Tools/clinic/cpp.py
@@ -1,3 +1,4 @@
+import dataclasses as dc
 import re
 import sys
 from collections.abc import Callable
@@ -15,6 +16,11 @@ def negate(condition: str) -> str:
         return condition[1:]
     return "!" + condition
 
+
+is_a_simple_defined = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match
+
+
+ at dc.dataclass(repr=False)
 class Monitor:
     """
     A simple C preprocessor that scans C source and computes, line by line,
@@ -27,25 +33,20 @@ class Monitor:
 
     Anyway this implementation seems to work well enough for the CPython sources.
     """
+    filename: str | None = None
+    _: dc.KW_ONLY
+    verbose: bool = False
 
-    is_a_simple_defined: Callable[[str], re.Match[str] | None]
-    is_a_simple_defined = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match
-
-    def __init__(self, filename: str | None = None, *, verbose: bool = False) -> None:
+    def __post_init__(self) -> None:
         self.stack: TokenStack = []
         self.in_comment = False
         self.continuation: str | None = None
         self.line_number = 0
-        self.filename = filename
-        self.verbose = verbose
 
     def __repr__(self) -> str:
-        return ''.join((
-            '<Monitor ',
-            str(id(self)),
-            " line=", str(self.line_number),
-            " condition=", repr(self.condition()),
-            ">"))
+        return (
+            f"<Monitor {id(self)} line={self.line_number} condition={self.condition()!r}>"
+        )
 
     def status(self) -> str:
         return str(self.line_number).rjust(4) + ": " + self.condition()
@@ -152,7 +153,7 @@ def pop_stack() -> TokenAndCondition:
             if not condition:
                 self.fail("Invalid format for #" + token + " line: no argument!")
             if token in {'if', 'elif'}:
-                if not self.is_a_simple_defined(condition):
+                if not is_a_simple_defined(condition):
                     condition = "(" + condition + ")"
                 if token == 'elif':
                     previous_token, previous_condition = pop_stack()



More information about the Python-checkins mailing list