Skip to content

Commit 0be3743

Browse files
gh-104683: Add --exclude option to Argument Clinic CLI (#107770)
Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent ea72c6f commit 0be3743

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

Doc/howto/clinic.rst

+5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ The CLI supports the following options:
188188

189189
The directory tree to walk in :option:`--make` mode.
190190

191+
.. option:: --exclude EXCLUDE
192+
193+
A file to exclude in :option:`--make` mode.
194+
This option can be given multiple times.
195+
191196
.. option:: FILE ...
192197

193198
The list of files to process.

Lib/test/test_clinic.py

+29
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,35 @@ def create_files(files, srcdir, code):
22162216
path = os.path.join(ext_path, filename)
22172217
self.assertNotIn(path, out)
22182218

2219+
def test_cli_make_exclude(self):
2220+
code = dedent("""
2221+
/*[clinic input]
2222+
[clinic start generated code]*/
2223+
""")
2224+
with os_helper.temp_dir(quiet=False) as tmp_dir:
2225+
# add some folders, some C files and a Python file
2226+
for fn in "file1.c", "file2.c", "file3.c", "file4.c":
2227+
path = os.path.join(tmp_dir, fn)
2228+
with open(path, "w", encoding="utf-8") as f:
2229+
f.write(code)
2230+
2231+
# Run clinic in verbose mode with --make on tmpdir.
2232+
# Exclude file2.c and file3.c.
2233+
out = self.expect_success(
2234+
"-v", "--make", "--srcdir", tmp_dir,
2235+
"--exclude", os.path.join(tmp_dir, "file2.c"),
2236+
# The added ./ should be normalised away.
2237+
"--exclude", os.path.join(tmp_dir, "./file3.c"),
2238+
# Relative paths should also work.
2239+
"--exclude", "file4.c"
2240+
)
2241+
2242+
# expect verbose mode to only mention the C files in tmp_dir
2243+
self.assertIn("file1.c", out)
2244+
self.assertNotIn("file2.c", out)
2245+
self.assertNotIn("file3.c", out)
2246+
self.assertNotIn("file4.c", out)
2247+
22192248
def test_cli_verbose(self):
22202249
with os_helper.temp_dir() as tmp_dir:
22212250
fn = os.path.join(tmp_dir, "test.c")

Makefile.pre.in

+5-1
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,13 @@ coverage-report: regen-token regen-frozen
775775
# Run "Argument Clinic" over all source files
776776
.PHONY: clinic
777777
clinic: check-clean-src $(srcdir)/Modules/_blake2/blake2s_impl.c
778-
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/clinic/clinic.py --make --srcdir $(srcdir)
778+
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/clinic/clinic.py --make --exclude Lib/test/clinic.test.c --srcdir $(srcdir)
779779
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/generate_global_objects.py
780780

781+
.PHONY: clinic-tests
782+
clinic-tests: check-clean-src $(srcdir)/Lib/test/clinic.test.c
783+
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/clinic/clinic.py -f $(srcdir)/Lib/test/clinic.test.c
784+
781785
# Build the interpreter
782786
$(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS)
783787
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``--exclude`` option to Argument Clinic CLI.

Tools/clinic/clinic.py

+11
Original file line numberDiff line numberDiff line change
@@ -5834,6 +5834,9 @@ def create_cli() -> argparse.ArgumentParser:
58345834
help="walk --srcdir to run over all relevant files")
58355835
cmdline.add_argument("--srcdir", type=str, default=os.curdir,
58365836
help="the directory tree to walk in --make mode")
5837+
cmdline.add_argument("--exclude", type=str, action="append",
5838+
help=("a file to exclude in --make mode; "
5839+
"can be given multiple times"))
58375840
cmdline.add_argument("filename", metavar="FILE", type=str, nargs="*",
58385841
help="the list of files to process")
58395842
return cmdline
@@ -5905,6 +5908,11 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
59055908
parser.error("can't use -o or filenames with --make")
59065909
if not ns.srcdir:
59075910
parser.error("--srcdir must not be empty with --make")
5911+
if ns.exclude:
5912+
excludes = [os.path.join(ns.srcdir, f) for f in ns.exclude]
5913+
excludes = [os.path.normpath(f) for f in excludes]
5914+
else:
5915+
excludes = []
59085916
for root, dirs, files in os.walk(ns.srcdir):
59095917
for rcs_dir in ('.svn', '.git', '.hg', 'build', 'externals'):
59105918
if rcs_dir in dirs:
@@ -5914,6 +5922,9 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
59145922
if not filename.endswith(('.c', '.cpp', '.h')):
59155923
continue
59165924
path = os.path.join(root, filename)
5925+
path = os.path.normpath(path)
5926+
if path in excludes:
5927+
continue
59175928
if ns.verbose:
59185929
print(path)
59195930
parse_file(path, verify=not ns.force)

0 commit comments

Comments
 (0)