Skip to contentSkip to conversation

perf build: Replace the deprecated FileType with a helper

PATCH New TAKANO Mitsuhiro 2 participants 2 messages Nov 25 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf build: Replace the deprecated FileType with a helper

pylint-tests and pylint-pmu-events hang off all, so with a pylint that reports W4904 the build fails on a tree nobody has changed. Measured on 7.4-rc4 with Python 3.14.9 and pylint 4.0.5, both targets exit 2 and pylint reports:

- W4904 deprecated-class, argparse.FileType: pmu-events/jevents.py:1494

    and :1496, tests/shell/lib/perf_json_output_lint.py:23

FileType is deprecated in the class's own terms - the docstring in 3.14 reads "Deprecated factory for creating file object types" - so the two jevents call sites and the one in the JSON output linter are replaced by a helper that opens the file, or hands back stdout or stdin for '-', which is what FileType did. The reader names its encoding, as the rest of the Python in perf does.

Verification: pylint-tests and pylint-pmu-events exit 2 and 2 before this change and 0 and 0 after it. The generated pmu-events tables are byte identical either side of the change, checked by running jevents.py for arm64 and x86 over the same input tree and comparing pmu-events.c and pmu-events-string.c.

Written with Hermes Agent, an LLM assistant; the author reviewed it and ran the build and the generator as described.

---
 tools/perf/pmu-events/jevents.py              | 19 ++++++++++++++++---
 .../tests/shell/lib/perf_json_output_lint.py  | 11 ++++++++++-
 2 files changed, 26 insertions(+), 4 deletions(-)
tools/perf/pmu-events/jevents.py +16 -3
index e083f7495e67..6974c58951a3 100755
@@ -7,7 +7,7 @@ import json
 import metric
 import os
 import sys
-from typing import (Callable, Dict, Optional, Sequence, Set, Tuple)
+from typing import (Callable, Dict, Optional, Sequence, Set, TextIO, Tuple)
 import collections
 
 # Global command line arguments.
@@ -1450,6 +1450,19 @@ def _init_worker(std_events: dict) -> None:
   global _arch_std_events
   _arch_std_events = std_events
 
+def open_output(path: str) -> TextIO:
+  """Open path for writing, or hand back stdout for '-'.
+
+  argparse.FileType is deprecated (its own docstring says so in 3.14) and
+  pylint 4 reports it, which fails the pylint-pmu-events target `all` needs.
+  """
+  if path == '-':
+    return sys.stdout
+  try:
+    return open(path, 'w', encoding='utf-8')
+  except OSError as exc:
+    raise argparse.ArgumentTypeError(str(exc)) from exc
+
 def main() -> None:
   global _args
 
@@ -1491,9 +1504,9 @@ such as "arm/cortex-a34".''',
       help='Root of tree containing architecture directories containing json files'
   )
   ap.add_argument(
-      'output_file', type=argparse.FileType('w', encoding='utf-8'), nargs='?', default=sys.stdout)
+      'output_file', type=open_output, nargs='?', default=sys.stdout)
   ap.add_argument(
-      'output_string_file', type=argparse.FileType('w', encoding='utf-8'), nargs='?', default=None)
+      'output_string_file', type=open_output, nargs='?', default=None)
   _args = ap.parse_args()
 
   _args.output_file.write(f"""/* SPDX-License-Identifier: GPL-2.0 */
tools/perf/tests/shell/lib/perf_json_output_lint.py +10 -1
index 8cde22ac9d97..9aafd03a8114 100644
@@ -6,6 +6,15 @@ import argparse
 import sys
 import json
 
+def open_input(path):
+    """Open path for reading, or hand back stdin for '-'."""
+    if path == '-':
+        return sys.stdin
+    try:
+        return open(path, encoding='utf-8')
+    except OSError as exc:
+        raise argparse.ArgumentTypeError(str(exc)) from exc
+
 ap = argparse.ArgumentParser()
 ap.add_argument('--no-args', action='store_true')
 ap.add_argument('--interval', action='store_true')
@@ -20,7 +29,7 @@ ap.add_argument('--per-die', action='store_true')
 ap.add_argument('--per-node', action='store_true')
 ap.add_argument('--per-socket', action='store_true')
 ap.add_argument('--metric-only', action='store_true')
-ap.add_argument('--file', type=argparse.FileType('r'), default=sys.stdin)
+ap.add_argument('--file', type=open_input, default=sys.stdin)
 args = ap.parse_args()
 
 Lines = args.file.readlines()

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f

Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 21 lines

pylint-tests and pylint-pmu-events hang off all, so with a pylint that reports W4904 the build fails on a tree nobody has changed. Measured on 7.4-rc4 with Python 3.14.9 and pylint 4.0.5, both targets exit 2 and pylint reports:

- W4904 deprecated-class, argparse.FileType: pmu-events/jevents.py:1494

    and :1496, tests/shell/lib/perf_json_output_lint.py:23

FileType is deprecated in the class's own terms - the docstring in 3.14 reads "Deprecated factory for creating file object types" - so the two jevents call sites and the one in the JSON output linter are replaced by a helper that opens the file, or hands back stdout or stdin for '-', which is what FileType did. The reader names its encoding, as the rest of the Python in perf does.

Verification: pylint-tests and pylint-pmu-events exit 2 and 2 before this change and 0 and 0 after it. The generated pmu-events tables are byte identical either side of the change, checked by running jevents.py for arm64 and x86 over the same input tree and comparing pmu-events.c and pmu-events-string.c.

Quoted 4 lines

Written with Hermes Agent, an LLM assistant; the author reviewed it and ran the build and the generator as described.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1