[PATCH v2 5/7] tools/perf: fix Python string escapes

Benjamin Gray posted 7 patches 2 years, 3 months ago
[PATCH v2 5/7] tools/perf: fix Python string escapes
Posted by Benjamin Gray 2 years, 3 months ago
Python 3.6 introduced a DeprecationWarning for invalid escape sequences.
This is upgraded to a SyntaxWarning in Python 3.12, and will eventually
be a syntax error.

Fix these now to get ahead of it before it's an error.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
---
 tools/perf/pmu-events/jevents.py                 | 2 +-
 tools/perf/scripts/python/arm-cs-trace-disasm.py | 4 ++--
 tools/perf/scripts/python/compaction-times.py    | 2 +-
 tools/perf/scripts/python/exported-sql-viewer.py | 4 ++--
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
index a7e88332276d..980f080a5a2c 100755
--- a/tools/perf/pmu-events/jevents.py
+++ b/tools/perf/pmu-events/jevents.py
@@ -83,7 +83,7 @@ def c_len(s: str) -> int:
   """Return the length of s a C string
 
   This doesn't handle all escape characters properly. It first assumes
-  all \ are for escaping, it then adjusts as it will have over counted
+  all \\ are for escaping, it then adjusts as it will have over counted
   \\. The code uses \000 rather than \0 as a terminator as an adjacent
   number would be folded into a string of \0 (ie. "\0" + "5" doesn't
   equal a terminator followed by the number 5 but the escape of
diff --git a/tools/perf/scripts/python/arm-cs-trace-disasm.py b/tools/perf/scripts/python/arm-cs-trace-disasm.py
index d59ff53f1d94..de58991c78bb 100755
--- a/tools/perf/scripts/python/arm-cs-trace-disasm.py
+++ b/tools/perf/scripts/python/arm-cs-trace-disasm.py
@@ -45,8 +45,8 @@ parser = OptionParser(option_list=option_list)
 # Initialize global dicts and regular expression
 disasm_cache = dict()
 cpu_data = dict()
-disasm_re = re.compile("^\s*([0-9a-fA-F]+):")
-disasm_func_re = re.compile("^\s*([0-9a-fA-F]+)\s.*:")
+disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")
+disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
 cache_size = 64*1024
 
 glb_source_file_name	= None
diff --git a/tools/perf/scripts/python/compaction-times.py b/tools/perf/scripts/python/compaction-times.py
index 2560a042dc6f..9401f7c14747 100644
--- a/tools/perf/scripts/python/compaction-times.py
+++ b/tools/perf/scripts/python/compaction-times.py
@@ -260,7 +260,7 @@ def pr_help():
 
 comm_re = None
 pid_re = None
-pid_regex = "^(\d*)-(\d*)$|^(\d*)$"
+pid_regex = r"^(\d*)-(\d*)$|^(\d*)$"
 
 opt_proc = popt.DISP_DFL
 opt_disp = topt.DISP_ALL
diff --git a/tools/perf/scripts/python/exported-sql-viewer.py b/tools/perf/scripts/python/exported-sql-viewer.py
index 13f2d8a81610..121cf61ba1b3 100755
--- a/tools/perf/scripts/python/exported-sql-viewer.py
+++ b/tools/perf/scripts/python/exported-sql-viewer.py
@@ -677,8 +677,8 @@ class CallGraphModelBase(TreeModel):
 			#   sqlite supports GLOB (text only) which uses * and ? and is case sensitive
 			if not self.glb.dbref.is_sqlite3:
 				# Escape % and _
-				s = value.replace("%", "\%")
-				s = s.replace("_", "\_")
+				s = value.replace("%", "\\%")
+				s = s.replace("_", "\\_")
 				# Translate * and ? into SQL LIKE pattern characters % and _
 				trans = string.maketrans("*?", "%_")
 				match = " LIKE '" + str(s).translate(trans) + "'"
-- 
2.41.0
Re: [PATCH v2 5/7] tools/perf: fix Python string escapes
Posted by Adrian Hunter 2 years, 3 months ago
On 12/09/23 09:07, Benjamin Gray wrote:
> Python 3.6 introduced a DeprecationWarning for invalid escape sequences.
> This is upgraded to a SyntaxWarning in Python 3.12, and will eventually
> be a syntax error.
> 
> Fix these now to get ahead of it before it's an error.
> 
> Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
> ---
>  tools/perf/pmu-events/jevents.py                 | 2 +-
>  tools/perf/scripts/python/arm-cs-trace-disasm.py | 4 ++--
>  tools/perf/scripts/python/compaction-times.py    | 2 +-
>  tools/perf/scripts/python/exported-sql-viewer.py | 4 ++--
>  4 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
> index a7e88332276d..980f080a5a2c 100755
> --- a/tools/perf/pmu-events/jevents.py
> +++ b/tools/perf/pmu-events/jevents.py
> @@ -83,7 +83,7 @@ def c_len(s: str) -> int:
>    """Return the length of s a C string
>  
>    This doesn't handle all escape characters properly. It first assumes
> -  all \ are for escaping, it then adjusts as it will have over counted
> +  all \\ are for escaping, it then adjusts as it will have over counted

It looks like the whole string should be a raw string

>    \\. The code uses \000 rather than \0 as a terminator as an adjacent
>    number would be folded into a string of \0 (ie. "\0" + "5" doesn't
>    equal a terminator followed by the number 5 but the escape of
> diff --git a/tools/perf/scripts/python/arm-cs-trace-disasm.py b/tools/perf/scripts/python/arm-cs-trace-disasm.py
> index d59ff53f1d94..de58991c78bb 100755
> --- a/tools/perf/scripts/python/arm-cs-trace-disasm.py
> +++ b/tools/perf/scripts/python/arm-cs-trace-disasm.py
> @@ -45,8 +45,8 @@ parser = OptionParser(option_list=option_list)
>  # Initialize global dicts and regular expression
>  disasm_cache = dict()
>  cpu_data = dict()
> -disasm_re = re.compile("^\s*([0-9a-fA-F]+):")
> -disasm_func_re = re.compile("^\s*([0-9a-fA-F]+)\s.*:")
> +disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")
> +disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
>  cache_size = 64*1024
>  
>  glb_source_file_name	= None
> diff --git a/tools/perf/scripts/python/compaction-times.py b/tools/perf/scripts/python/compaction-times.py
> index 2560a042dc6f..9401f7c14747 100644
> --- a/tools/perf/scripts/python/compaction-times.py
> +++ b/tools/perf/scripts/python/compaction-times.py
> @@ -260,7 +260,7 @@ def pr_help():
>  
>  comm_re = None
>  pid_re = None
> -pid_regex = "^(\d*)-(\d*)$|^(\d*)$"
> +pid_regex = r"^(\d*)-(\d*)$|^(\d*)$"
>  
>  opt_proc = popt.DISP_DFL
>  opt_disp = topt.DISP_ALL
> diff --git a/tools/perf/scripts/python/exported-sql-viewer.py b/tools/perf/scripts/python/exported-sql-viewer.py
> index 13f2d8a81610..121cf61ba1b3 100755
> --- a/tools/perf/scripts/python/exported-sql-viewer.py
> +++ b/tools/perf/scripts/python/exported-sql-viewer.py
> @@ -677,8 +677,8 @@ class CallGraphModelBase(TreeModel):
>  			#   sqlite supports GLOB (text only) which uses * and ? and is case sensitive
>  			if not self.glb.dbref.is_sqlite3:
>  				# Escape % and _
> -				s = value.replace("%", "\%")
> -				s = s.replace("_", "\_")
> +				s = value.replace("%", "\\%")
> +				s = s.replace("_", "\\_")

Raw strings seem more readable, so could be
used here too

>  				# Translate * and ? into SQL LIKE pattern characters % and _
>  				trans = string.maketrans("*?", "%_")
>  				match = " LIKE '" + str(s).translate(trans) + "'"
Re: [PATCH v2 5/7] tools/perf: fix Python string escapes
Posted by Benjamin Gray 2 years, 3 months ago
On 12/9/23 8:56 pm, Adrian Hunter wrote:
> On 12/09/23 09:07, Benjamin Gray wrote:
>> diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
>> index a7e88332276d..980f080a5a2c 100755
>> --- a/tools/perf/pmu-events/jevents.py
>> +++ b/tools/perf/pmu-events/jevents.py
>> @@ -83,7 +83,7 @@ def c_len(s: str) -> int:
>>     """Return the length of s a C string
>>   
>>     This doesn't handle all escape characters properly. It first assumes
>> -  all \ are for escaping, it then adjusts as it will have over counted
>> +  all \\ are for escaping, it then adjusts as it will have over counted
> 
> It looks like the whole string should be a raw string
> 
...
>> -				s = value.replace("%", "\%")
>> -				s = s.replace("_", "\_")
>> +				s = value.replace("%", "\\%")
>> +				s = s.replace("_", "\\_")
> 
> Raw strings seem more readable, so could be
> used here too

Yeah, sounds good. I normally use r strings only for regex, but there 
shouldn't be any ambiguity here (it might have been misleading if the 
search argument to replace looked like a regex).

Having the docstring be an r string is a good catch. There's probably a 
few like that in the kernel, but finding them is a little more 
complicated because they might be 'valid' syntax (e.g., the '\000' just 
becomes a null byte. This series is focused on the syntax errors though, 
so I'll just leave it be.

How is the following?
---
Subject: [PATCH] tools/perf: fix Python string escapes

Python 3.6 introduced a DeprecationWarning for invalid escape sequences.
This is upgraded to a SyntaxWarning in Python 3.12, and will eventually
be a syntax error.

Fix these now to get ahead of it before it's an error.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
---
  tools/perf/pmu-events/jevents.py                 | 2 +-
  tools/perf/scripts/python/arm-cs-trace-disasm.py | 4 ++--
  tools/perf/scripts/python/compaction-times.py    | 2 +-
  tools/perf/scripts/python/exported-sql-viewer.py | 4 ++--
  4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/perf/pmu-events/jevents.py 
b/tools/perf/pmu-events/jevents.py
index a7e88332276d..1b4519333a28 100755
--- a/tools/perf/pmu-events/jevents.py
+++ b/tools/perf/pmu-events/jevents.py
@@ -80,7 +80,7 @@ def file_name_to_table_name(prefix: str, parents: 
Sequence[str],


  def c_len(s: str) -> int:
-  """Return the length of s a C string
+  r"""Return the length of s a C string

    This doesn't handle all escape characters properly. It first assumes
    all \ are for escaping, it then adjusts as it will have over counted
diff --git a/tools/perf/scripts/python/arm-cs-trace-disasm.py 
b/tools/perf/scripts/python/arm-cs-trace-disasm.py
index d59ff53f1d94..de58991c78bb 100755
--- a/tools/perf/scripts/python/arm-cs-trace-disasm.py
+++ b/tools/perf/scripts/python/arm-cs-trace-disasm.py
@@ -45,8 +45,8 @@ parser = OptionParser(option_list=option_list)
  # Initialize global dicts and regular expression
  disasm_cache = dict()
  cpu_data = dict()
-disasm_re = re.compile("^\s*([0-9a-fA-F]+):")
-disasm_func_re = re.compile("^\s*([0-9a-fA-F]+)\s.*:")
+disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")
+disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
  cache_size = 64*1024

  glb_source_file_name	= None
diff --git a/tools/perf/scripts/python/compaction-times.py 
b/tools/perf/scripts/python/compaction-times.py
index 2560a042dc6f..9401f7c14747 100644
--- a/tools/perf/scripts/python/compaction-times.py
+++ b/tools/perf/scripts/python/compaction-times.py
@@ -260,7 +260,7 @@ def pr_help():

  comm_re = None
  pid_re = None
-pid_regex = "^(\d*)-(\d*)$|^(\d*)$"
+pid_regex = r"^(\d*)-(\d*)$|^(\d*)$"

  opt_proc = popt.DISP_DFL
  opt_disp = topt.DISP_ALL
diff --git a/tools/perf/scripts/python/exported-sql-viewer.py 
b/tools/perf/scripts/python/exported-sql-viewer.py
index 13f2d8a81610..78763531fe5a 100755
--- a/tools/perf/scripts/python/exported-sql-viewer.py
+++ b/tools/perf/scripts/python/exported-sql-viewer.py
@@ -677,8 +677,8 @@ class CallGraphModelBase(TreeModel):
  			#   sqlite supports GLOB (text only) which uses * and ? and is case 
sensitive
  			if not self.glb.dbref.is_sqlite3:
  				# Escape % and _
-				s = value.replace("%", "\%")
-				s = s.replace("_", "\_")
+				s = value.replace("%", r"\%")
+				s = s.replace("_", r"\_")
  				# Translate * and ? into SQL LIKE pattern characters % and _
  				trans = string.maketrans("*?", "%_")
  				match = " LIKE '" + str(s).translate(trans) + "'"
-- 
2.41.0