Re: [PATCH v2] gen_compile_commands: fix invalid escape sequence warning

Justin Stitt
2024-02-14T00:40:34Z

Hi,

On Mon, Feb 12, 2024 at 6:28 PM Andrew Ballance
<andrewjballance@gmail.com> wrote:
>
> with python 12.1 '\#' results in this warning
> SyntaxWarning: invalid escape sequence '\#'

Yes, since Python 3.12 escaping invalid characters will result in a
SyntaxWarning.

The complete table of supported escape codes is available here [1].

>
> Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
> ---
> scripts/clang-tools/gen_compile_commands.py | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/clang-tools/gen_compile_commands.py b/scripts/clang-tools/gen_compile_commands.py
> index 5dea4479240b..93f64095fda9 100755
> --- a/scripts/clang-tools/gen_compile_commands.py
> +++ b/scripts/clang-tools/gen_compile_commands.py
> @@ -170,7 +170,7 @@ def process_line(root_directory, command_prefix, file_path):
> # escape the pound sign '#', either as '\#' or '$(pound)' (depending on the
> # kernel version). The compile_commands.json file is not interepreted
> # by Make, so this code replaces the escaped version with '#'.
> - prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#')
> + prefix = command_prefix.replace('\\#', '#').replace('$(pound)', '#')

I'd personally prefer using a raw string:
- prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#')
+ prefix = command_prefix.replace(r'\#', '#').replace('$(pound)', '#')

This makes it clear that a literal backslash followed by a literal
pound sign will be replaced.

>
> # Return the canonical path, eliminating any symbolic links encountered in the path.
> abs_path = os.path.realpath(os.path.join(root_directory, file_path))
> --
> 2.43.0
>

[1]: https://docs.python.org/3/reference/lexical_analysis.html#index-23

Thanks
Justin