From 489f6790c49797c612be4f1ea3aa518a3e370ab9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 28 Apr 2024 14:12:01 +0200 Subject: [PATCH] Add some ruff rules for pyupgrade --- doc/conf.py | 15 +++++++-------- pyperf/_bench.py | 10 +++++----- pyperf/_cli.py | 4 ++-- pyperf/_collect_metadata.py | 4 ++-- pyperf/_compare.py | 2 +- pyperf/_linux_memory.py | 2 +- pyperf/_manager.py | 2 +- pyperf/_metadata.py | 2 +- pyperf/_system.py | 22 +++++++++++----------- pyperf/_utils.py | 4 ++-- pyperf/tests/__init__.py | 2 +- pyperf/tests/replay.py | 2 +- pyperf/tests/test_metadata.py | 4 ++-- pyperf/tests/test_perf_cli.py | 2 +- pyproject.toml | 7 +++++++ 15 files changed, 45 insertions(+), 39 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index f69a53f0..732abe5e 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # pyperf documentation build configuration file, created by # sphinx-quickstart on Wed Jun 1 15:28:03 2016. @@ -42,9 +41,9 @@ master_doc = 'index' # General information about the project. -project = u'pyperf' -copyright = u'2016, Victor Stinner' -author = u'Victor Stinner' +project = 'pyperf' +copyright = '2016, Victor Stinner' +author = 'Victor Stinner' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -216,8 +215,8 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'pyperf.tex', u'pyperf Documentation', - u'Victor Stinner', 'manual'), + (master_doc, 'pyperf.tex', 'pyperf Documentation', + 'Victor Stinner', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -246,7 +245,7 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'pyperf', u'pyperf Documentation', + (master_doc, 'pyperf', 'pyperf Documentation', [author], 1) ] @@ -260,7 +259,7 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'pyperf', u'pyperf Documentation', + (master_doc, 'pyperf', 'pyperf Documentation', author, 'pyperf', 'One line description of project.', 'Miscellaneous'), ] diff --git a/pyperf/_bench.py b/pyperf/_bench.py index fae94bbf..5d9bcf34 100644 --- a/pyperf/_bench.py +++ b/pyperf/_bench.py @@ -86,7 +86,7 @@ def method(self): return method -class Run(object): +class Run: # Run is immutable, so it can be shared/exchanged between two benchmarks __slots__ = ('_warmups', '_values', '_metadata') @@ -319,7 +319,7 @@ def _update_metadata(self, metadata): return self._replace(metadata=metadata2) -class Benchmark(object): +class Benchmark: def __init__(self, runs): self._runs = [] # list of Run objects self._clear_runs_cache() @@ -627,7 +627,7 @@ def update_metadata(self, metadata): self._replace_runs(new_runs) -class BenchmarkSuite(object): +class BenchmarkSuite: def __init__(self, benchmarks, filename=None): if not benchmarks: raise ValueError("benchmarks must be a non-empty " @@ -727,7 +727,7 @@ def _load_open(filename): if isinstance(filename, bytes): suffix = b'.gz' else: - suffix = u'.gz' + suffix = '.gz' if filename.endswith(suffix): # Use lazy import to limit imports on 'import pyperf' @@ -770,7 +770,7 @@ def _dump_open(filename, replace): if isinstance(filename, bytes): suffix = b'.gz' else: - suffix = u'.gz' + suffix = '.gz' if not replace and os.path.exists(filename): raise OSError(errno.EEXIST, "File already exists") diff --git a/pyperf/_cli.py b/pyperf/_cli.py index f8017018..fba0ada8 100644 --- a/pyperf/_cli.py +++ b/pyperf/_cli.py @@ -604,7 +604,7 @@ def catch_broken_pipe_error(file=None): # was closed by the consumer for file in files: file.flush() - except IOError as exc: + except OSError as exc: if exc.errno != errno.EPIPE: raise # got a broken pipe error: ignore it @@ -615,5 +615,5 @@ def catch_broken_pipe_error(file=None): for file in files: try: file.close() - except IOError: + except OSError: pass diff --git a/pyperf/_collect_metadata.py b/pyperf/_collect_metadata.py index 33481502..a9f11488 100644 --- a/pyperf/_collect_metadata.py +++ b/pyperf/_collect_metadata.py @@ -136,7 +136,7 @@ def read_proc(path): with open_text(path) as fp: for line in fp: yield line.rstrip() - except (OSError, IOError): + except OSError: return @@ -332,7 +332,7 @@ def get_cpu_temperature(path, cpu_temp): try: temp_label = read_first_line(template % 'label', error=True) - except IOError: + except OSError: break temp_input = read_first_line(template % 'input', error=True) diff --git a/pyperf/_compare.py b/pyperf/_compare.py index dc2afdc9..9b5669aa 100644 --- a/pyperf/_compare.py +++ b/pyperf/_compare.py @@ -53,7 +53,7 @@ def get_tags_for_result(result): return result.ref.benchmark.get_metadata().get("tags", []) -class CompareResult(object): +class CompareResult: def __init__(self, ref, changed, min_speed=None): # CompareData object self.ref = ref diff --git a/pyperf/_linux_memory.py b/pyperf/_linux_memory.py index e8e8f55a..7eca372d 100644 --- a/pyperf/_linux_memory.py +++ b/pyperf/_linux_memory.py @@ -54,7 +54,7 @@ def check_tracking_memory(): mem_thread = PeakMemoryUsageThread() try: mem_thread.get() - except IOError as exc: + except OSError as exc: path = proc_path("self/smaps") return "unable to read %s: %s" % (path, exc) diff --git a/pyperf/_manager.py b/pyperf/_manager.py index 3a1fcb86..1dcbd703 100644 --- a/pyperf/_manager.py +++ b/pyperf/_manager.py @@ -12,7 +12,7 @@ MAX_CALIBRATION = 5 -class Manager(object): +class Manager: """ Manager process which spawns worker processes to: - calibrate warmups diff --git a/pyperf/_metadata.py b/pyperf/_metadata.py index b7a7ba8e..e2ff96c5 100644 --- a/pyperf/_metadata.py +++ b/pyperf/_metadata.py @@ -137,7 +137,7 @@ def format_metadata(name, value): return info.formatter(value) -class Metadata(object): +class Metadata: def __init__(self, name, value): self._name = name self._value = value diff --git a/pyperf/_system.py b/pyperf/_system.py index 15aa6c8b..366a1502 100644 --- a/pyperf/_system.py +++ b/pyperf/_system.py @@ -74,7 +74,7 @@ def use_intel_pstate(): return (scaling_driver == 'intel_pstate') -class Operation(object): +class Operation: @staticmethod def available(): return True @@ -107,7 +107,7 @@ def check_permission_error(self, exc): def read_first_line(self, path): try: return read_first_line(path, error=True) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) return '' @@ -219,7 +219,7 @@ def write_msr(self, cpu, reg_num, value): os.write(fd, data) finally: os.close(fd) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) self.error("Failed to write %#x into MSR %#x using %s: %s" % (value, reg_num, path, exc)) @@ -308,7 +308,7 @@ def write(self, tune): content = '0' if enable else '1' try: write_text(self.path, content) - except IOError as exc: + except OSError as exc: # don't log a permission error if the user is root: permission # error as root means that Turbo Boost is disabled in the BIOS if not is_root(): @@ -376,7 +376,7 @@ def write(self, tune): return try: write_text(self.path, new_governor) - except IOError as exc: + except OSError as exc: self.error("Failed to set the CPU scaling governor: %s" % exc) else: self.log_action("CPU scaling governor set to %s" % new_governor) @@ -495,7 +495,7 @@ def write(self, tune): try: write_text(self.path, new_value) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) self.error("Failed to write into %s: %s" % (self.path, exc)) else: @@ -552,7 +552,7 @@ def read_freq(self, filename): try: with open(filename, "rb") as fp: return fp.readline() - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) return None @@ -579,7 +579,7 @@ def write_cpu(self, cpu, tune): filename = os.path.join(cpu_path, "scaling_min_freq") try: return self.write_freq(filename, freq) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) self.error("Unable to write scaling_max_freq of CPU %s: %s" % (cpu, exc)) @@ -768,7 +768,7 @@ def write_default(self, new_affinity): mask = format_cpus_as_mask(new_affinity) try: write_text(self.default_affinity_path, mask) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) self.error("Failed to write %r into %s: %s" % (mask, self.default_affinity_path, exc)) @@ -782,7 +782,7 @@ def write_irq(self, irq, cpus): try: write_text(path, mask) return True - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) # EIO means that the IRQ doesn't support SMP affinity: # ignore the error @@ -937,7 +937,7 @@ def write(self, tune): try: write_text(self.path, str(new_rate)) - except IOError as exc: + except OSError as exc: self.check_permission_error(exc) self.error("Failed to write into %s: %s" % (self.path, exc)) else: diff --git a/pyperf/_utils.py b/pyperf/_utils.py index 87bb1a7f..f4a7748e 100644 --- a/pyperf/_utils.py +++ b/pyperf/_utils.py @@ -155,7 +155,7 @@ def read_first_line(path, error=False): with open_text(path) as fp: line = fp.readline() return line.rstrip() - except IOError: + except OSError: if error: raise else: @@ -282,7 +282,7 @@ def create_environ(inherit_environ, locale, copy_all): return env -class _Pipe(object): +class _Pipe: _OPEN_MODE = "r" def __init__(self, fd): diff --git a/pyperf/tests/__init__.py b/pyperf/tests/__init__.py index 2b3dc8f5..8ce1bdbe 100644 --- a/pyperf/tests/__init__.py +++ b/pyperf/tests/__init__.py @@ -55,7 +55,7 @@ def temporary_directory(): def benchmark_as_json(benchmark, compact=True): with temporary_file() as tmp_name: benchmark.dump(tmp_name, compact=compact) - with io.open(tmp_name, 'r', encoding='utf-8') as tmp: + with open(tmp_name, 'r', encoding='utf-8') as tmp: return tmp.read() diff --git a/pyperf/tests/replay.py b/pyperf/tests/replay.py index 8c5dd84d..fb9aea8f 100644 --- a/pyperf/tests/replay.py +++ b/pyperf/tests/replay.py @@ -14,7 +14,7 @@ def get_raw_values(filename, run_id): return (run, raw_values) -class Replay(object): +class Replay: def __init__(self, runner, filename): self.runner = runner self.args = runner.args diff --git a/pyperf/tests/test_metadata.py b/pyperf/tests/test_metadata.py index 4b3573bb..58a2481a 100644 --- a/pyperf/tests/test_metadata.py +++ b/pyperf/tests/test_metadata.py @@ -159,7 +159,7 @@ def mock_open(filename, *args, **kw): elif filename.startswith('/sys/devices/system/cpu/nohz_full'): data = nohz_full elif filename.startswith('/sys/devices/system/cpu/cpu2'): - raise IOError + raise OSError elif filename == '/sys/devices/system/cpu/cpuidle/current_driver': data = 'IDLE_DRV\n' else: @@ -194,7 +194,7 @@ def mock_open(filename, *args, **kw): elif filename == '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor': data = 'GOVERNOR\n' elif filename.startswith('/sys/devices/system/cpu/cpu2'): - raise IOError + raise OSError else: raise ValueError("unexpect open: %r" % filename) return io.StringIO(data) diff --git a/pyperf/tests/test_perf_cli.py b/pyperf/tests/test_perf_cli.py index 2a19728b..b911f425 100644 --- a/pyperf/tests/test_perf_cli.py +++ b/pyperf/tests/test_perf_cli.py @@ -11,7 +11,7 @@ TELCO = os.path.join(TESTDIR, 'telco.json') -class BaseTestCase(object): +class BaseTestCase: maxDiff = 100 * 80 def create_bench(self, values, metadata=None): diff --git a/pyproject.toml b/pyproject.toml index d32c5856..4d7583a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,3 +61,10 @@ packages = ["pyperf", "pyperf.tests"] [tool.setuptools.dynamic] version = {attr = "pyperf.__version__"} + +[tool.ruff.lint] +extend-select = ["C90", "UP"] +extend-ignore = ["UP015", "UP031"] + +[tool.ruff.lint.mccabe] +max-complexity = 31