8000 Emit the message from the right script. · python/cpython@f1e5827 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1e5827

Browse files
Emit the message from the right script.
1 parent d9f45a8 commit f1e5827

File tree

2 files changed

+83
-78
lines changed

2 files changed

+83
-78
lines changed

Tools/c-analyzer/c_analyzer/__main__.py

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os.path
55
import re
66
import sys
7-
import textwrap
87

98
from c_common import fsutil
109
from c_common.logging import VERBOSITY, Printer
@@ -44,39 +43,6 @@
4443
logger = logging.getLogger(__name__)
4544

4645

47-
EXPLANATION = textwrap.dedent('''
48-
-------------------------
49-
50-
Non-constant global variables are generally not supported
51-
in the CPython repo. We use a tool to analyze the C code
52-
and report if any unsupported globals are found. The tool
53-
may be run manually with:
54-
55-
./python Tools/c-analyzer/check-c-globals.py --format summary [FILE]
56-
57-
Occasionally the tool is unable to parse updated code.
58-
If this happens then add the file to the "EXCLUDED" list
59-
in Tools/c-analyzer/cpython/_parser.py and create a new
60-
issue for fixing the tool (and CC ericsnowcurrently
61-
on the issue).
62-
63-
If the tool reports an unsupported global variable and
64-
it is actually const (and thus supported) then first try
65-
fixing the declaration appropriately in the code. If that
66-
doesn't work then add the variable to the "should be const"
67-
section of Tools/c-analyzer/cpython/ignored.tsv.
68-
69-
If the tool otherwise reports an unsupported global variable
70-
then first try to make it non-global, possibly adding to
71-
PyInterpreterState (for core code) or module state (for
72-
extension modules). In an emergency, you can add the
73-
variable to Tools/c-analyzer/cpython/globals-to-fix.tsv
74-
to get CI passing, but doing so should be avoided. If
75-
this course it taken, be sure to create an issue for
76-
eliminating the global (and CC ericsnowcurrently).
77-
''')
78-
79-
8046
#######################################
8147
# table helpers
8248

@@ -357,45 +323,40 @@ def cmd_check(filenames, *,
357323
if track_progress:
358324
filenames = track_progress(filenames)
359325

360-
try:
361-
logger.info('analyzing files...')
362-
analyzed = _analyze(filenames, **kwargs)
363-
analyzed.fix_filenames(relroot, normalize=False)
364-
decls = filter_forward(analyzed, markpublic=True)
365-
366-
logger.info('checking analysis results...')
367-
failed = []
368-
for data, failure in _check_all(decls, checks, failfast=failfast):
369-
if data is None:
370-
printer.info('stopping after one failure')
371-
break
372-
if div is not None and len(failed) > 0:
373-
printer.info(div)
374-
failed.append(data)
375-
handle_failure(failure, data)
376-
handle_after()
377-
378-
printer.info('-------------------------')
379-
logger.info(f'total failures: {len(failed)}')
380-
logger.info('done checking')
381-
382-
if fmt == 'summary':
383-
print('Categorized by storage:')
326+
logger.info('analyzing files...')
327+
analyzed = _analyze(filenames, **kwargs)
328+
analyzed.fix_filenames(relroot, normalize=False)
329+
decls = filter_forward(analyzed, markpublic=True)
330+
331+
logger.info('checking analysis results...')
332+
failed = []
333+
for data, failure in _check_all(decls, checks, failfast=failfast):
334+
if data is None:
335+
printer.info('stopping after one failure')
336+
break
337+
if div is not None and len(failed) > 0:
338+
printer.info(div)
339+
failed.append(data)
340+
handle_failure(failure, data)
341+
handle_after()
342+
343+
printer.info('-------------------------')
344+
logger.info(f'total failures: {len(failed)}')
345+
logger.info('done checking')
346+
347+
if fmt == 'summary':
348+
print('Categorized by storage:')
349+
print()
350+
from .match import group_by_storage
351+
grouped = group_by_storage(failed, ignore_non_match=False)
352+
for group, decls in grouped.items():
384353
print()
385-
from .match import group_by_storage
386-
grouped = group_by_storage(failed, ignore_non_match=False)
387-
for group, decls in grouped.items():
388-
print()
389-
print(group)
390-
for decl in decls:
391-
print(' ', _fmt_one_summary(decl))
392-
print(f'subtotal: {len(decls)}')
393-
except Exception:
394-
print(EXPLANATION)
395-
raise # re-raise
354+
print(group)
355+
for decl in decls:
356+
print(' ', _fmt_one_summary(decl))
357+
print(f'subtotal: {len(decls)}')
396358

397359
if len(failed) > 0:
398-
print(EXPLANATION)
399360
sys.exit(len(failed))
400361

401362

Tools/c-analyzer/cpython/__main__.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import sys
3+
import textwrap
34

45
from c_common.fsutil import expand_filenames, iter_files_by_suffix
56
from c_common.scriptutil import (
@@ -26,6 +27,39 @@
2627
logger = logging.getLogger(__name__)
2728

2829

30+
CHECK_EXPLANATION = textwrap.dedent('''
31+
-------------------------
32+
33+
Non-constant global variables are generally not supported
34+
in the CPython repo. We use a tool to analyze the C code
35+
and report if any unsupported globals are found. The tool
36+
may be run manually with:
37+
38+
./python Tools/c-analyzer/check-c-globals.py --format summary [FILE]
39+
40+
Occasionally the tool is unable to parse updated code.
41+
If this happens then add the file to the "EXCLUDED" list
42+
in Tools/c-analyzer/cpython/_parser.py and create a new
43+
issue for fixing the tool (and CC ericsnowcurrently
44+
on the issue).
45+
46+
If the tool reports an unsupported global variable and
47+
it is actually const (and thus supported) then first try
48+
fixing the declaration appropriately in the code. If that
49+
doesn't work then add the variable to the "should be const"
50+
section of Tools/c-analyzer/cpython/ignored.tsv.
51+
52+
If the tool otherwise reports an unsupported global variable
53+
then first try to make it non-global, possibly adding to
54+
PyInterpreterState (for core code) or module state (for
55+
extension modules). In an emergency, you can add the
56+
variable to Tools/c-analyzer/cpython/globals-to-fix.tsv
57+
to get CI passing, but doing so should be avoided. If
58+
this course it taken, be sure to create an issue for
59+
eliminating the global (and CC ericsnowcurrently).
60+
''')
61+
62+
2963
def _resolve_filenames(filenames):
3064
if filenames:
3165
resolved = (_files.resolve_filename(f) for f in filenames)
@@ -123,14 +157,24 @@ def _cli_check(parser, **kwargs):
123157
def cmd_check(filenames=None, **kwargs):
124158
filenames = _resolve_filenames(filenames)
125159
kwargs['get_file_preprocessor'] = _parser.get_preprocessor(log_err=print)
126-
c_analyzer.cmd_check(
127-
filenames,
128-
relroot=REPO_ROOT,
129-
_analyze=_analyzer.analyze,
130-
_CHECKS=CHECKS,
131-
file_maxsizes=_parser.MAX_SIZES,
132-
**kwargs
133-
)
160+
try:
161+
c_analyzer.cmd_check(
162+
filenames,
163+
relroot=REPO_ROOT,
164+
_analyze=_analyzer.analyze,
165+
_CHECKS=CHECKS,
166+
file_maxsizes=_parser.MAX_SIZES,
167+
**kwargs
168+
)
169+
except SystemExit as exc:
170+
num_failed = exc.args[0] if getattr(exc, 'args', None) else None
171+
if isinstance(num_failed, int):
172+
if num_failed > 0:
173+
print(CHECK_EXPLANATION)
174+
raise # re-raise
175+
except Exception:
176+
print(CHECK_EXPLANATION)
177+
raise # re-raise
134178

135179

136180
def cmd_analyze(filenames=None, **kwargs):

0 commit comments

Comments
 (0)
0