diff options
author | Erlend E. Aasland <erlend@python.org> | 2023-08-07 14:33:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-07 14:33:52 (GMT) |
commit | 835e38891535649321230f94121410244c583966 (patch) | |
tree | ffe544e687f7c4f67768ae374daf3394d766b087 /Tools/clinic | |
parent | 8fcee6b2795a504f1bd39118759e5ce44183f689 (diff) | |
download | cpython-835e38891535649321230f94121410244c583966.zip cpython-835e38891535649321230f94121410244c583966.tar.gz cpython-835e38891535649321230f94121410244c583966.tar.bz2 |
gh-95065: Argument Clinic: Pretty-print long C strings in generated code (#107712)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Tools/clinic')
-rwxr-xr-x | Tools/clinic/clinic.py | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 2bed98c..3a8431c 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -221,6 +221,20 @@ def c_repr(s: str) -> str: return '"' + s + '"' +def wrapped_c_string_literal( + text: str, + *, + width: int = 72, + suffix: str = '', + initial_indent: int = 0, + subsequent_indent: int = 4 +) -> str: + wrapped = textwrap.wrap(text, width=width, replace_whitespace=False, + drop_whitespace=False, break_on_hyphens=False) + separator = '"' + suffix + '\n' + subsequent_indent * ' ' + '"' + return initial_indent * ' ' + '"' + separator.join(wrapped) + '"' + + is_legal_c_identifier = re.compile('^[A-Za-z_][A-Za-z0-9_]*$').match def is_legal_py_identifier(s: str) -> bool: @@ -837,17 +851,22 @@ class CLanguage(Language): """) DEPRECATED_POSITIONAL_PROTOTYPE: Final[str] = r""" #if PY_VERSION_HEX >= 0x{major:02x}{minor:02x}00C0 - # error "{cpp_message}" + # error \ + {cpp_message} #elif PY_VERSION_HEX >= 0x{major:02x}{minor:02x}00A0 # ifdef _MSC_VER - # pragma message ("{cpp_message}") + # pragma message ( \ + {cpp_message}) # else - # warning "{cpp_message}" + # warning \ + {cpp_message} # endif #endif if ({condition}) {{{{ - if (PyErr_WarnEx(PyExc_DeprecationWarning, "{depr_message}", 1)) {{{{ - goto exit; + if (PyErr_WarnEx(PyExc_DeprecationWarning, + {depr_message}, 1)) + {{{{ + goto exit; }}}} }}}} """ @@ -899,6 +918,7 @@ class CLanguage(Language): f"In {source}, update parameter(s) {pstr} in the clinic " f"input of {func.full_name!r} to be keyword-only." ) + # Format the deprecation message. if first_pos == 0: preamble = "Passing positional arguments to " @@ -926,8 +946,11 @@ class CLanguage(Language): condition=condition, major=major, minor=minor, - cpp_message=cpp_message, - depr_message=depr_message, + cpp_message=wrapped_c_string_literal(cpp_message, suffix=" \\", + width=64, + subsequent_indent=16), + depr_message=wrapped_c_string_literal(depr_message, width=64, + subsequent_indent=20), ) return normalize_snippet(code, indent=4) |