[Python-checkins] gh-104683: Modernise `clinic.py` using `str.removeprefix` and `str.removesuffix` (#104685)

AlexWaygood webhook-mailer at python.org
Sat May 20 07:08:41 EDT 2023


https://github.com/python/cpython/commit/663c049ff78a299bdf7c1a0444b9900e6d37372d
commit: 663c049ff78a299bdf7c1a0444b9900e6d37372d
branch: main
author: Alex Waygood <Alex.Waygood at Gmail.com>
committer: AlexWaygood <Alex.Waygood at Gmail.com>
date: 2023-05-20T11:08:28Z
summary:

gh-104683: Modernise `clinic.py` using `str.removeprefix` and `str.removesuffix` (#104685)

Both methods were new in Python 3.9.

files:
M Tools/clinic/clinic.py

diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 792e8e4dc03a..b00b480a684d 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -1741,7 +1741,7 @@ def is_stop_line(line):
             # make sure to recognize stop line even if it
             # doesn't end with EOL (it could be the very end of the file)
             if line.startswith(stop_line):
-                remainder = line[len(stop_line):]
+                remainder = line.removeprefix(stop_line)
                 if remainder and not remainder.isspace():
                     fail(f"Garbage after stop line: {remainder!r}")
                 return True
@@ -1759,7 +1759,7 @@ def is_stop_line(line):
             if body_prefix:
                 line = line.lstrip()
                 assert line.startswith(body_prefix)
-                line = line[len(body_prefix):]
+                line = line.removeprefix(body_prefix)
             input_add(line)
 
         # consume output and checksum line, if present.
@@ -2562,7 +2562,7 @@ def add_c_converter(f, name=None):
         name = f.__name__
         if not name.endswith('_converter'):
             return f
-        name = name[:-len('_converter')]
+        name = name.removesuffix('_converter')
     converters[name] = f
     return f
 
@@ -3969,7 +3969,7 @@ def add_c_return_converter(f, name=None):
         name = f.__name__
         if not name.endswith('_return_converter'):
             return f
-        name = name[:-len('_return_converter')]
+        name = name.removesuffix('_return_converter')
     return_converters[name] = f
     return f
 
@@ -5360,7 +5360,7 @@ def main(argv):
                 if name in ignored:
                     continue
                 if name.endswith(suffix):
-                    ids.append((name, name[:-len(suffix)]))
+                    ids.append((name, name.removesuffix(suffix)))
                     break
         print()
 



More information about the Python-checkins mailing list