From 1f5ba0ca874dbfacc60ee6f68f90db0314cfa6cf Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sat, 19 Oct 2024 23:08:04 +0900 Subject: [PATCH] Run pyupgrade to clean up Python 2 syntaxes Update all .py source files by $ pyupgrade --py3-only $(git ls-files | grep ".py$") to modernize the code according to Python 3 syntaxes. Also add the pyupgrade hook to pre-commit to avoid merging additional Python 2 syntaxes. Change-Id: I9d54c11642acb341cc5db1267f5a91d4740f932c --- .pre-commit-config.yaml | 11 +++++--- doc/source/conf.py | 1 - oslo_concurrency/fixture/lockutils.py | 4 +-- oslo_concurrency/lockutils.py | 6 ++--- oslo_concurrency/processutils.py | 12 ++++----- oslo_concurrency/tests/unit/test_lockutils.py | 20 +++++++-------- .../tests/unit/test_processutils.py | 25 ++++++++++--------- oslo_concurrency/watchdog.py | 2 +- releasenotes/source/conf.py | 1 - 9 files changed, 43 insertions(+), 39 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6937157..b2b7d41 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: trailing-whitespace # Replaces or checks mixed line ending @@ -19,12 +19,17 @@ repos: - id: check-yaml files: .*\.(yaml|yml)$ - repo: https://opendev.org/openstack/hacking - rev: 6.1.0 + rev: 7.0.0 hooks: - id: hacking additional_dependencies: [] - repo: https://github.com/PyCQA/bandit - rev: 1.7.6 + rev: 1.7.10 hooks: - id: bandit args: ['-x', 'tests', '-s', 'B311,B404,B603,B606'] + - repo: https://github.com/asottile/pyupgrade + rev: v3.18.0 + hooks: + - id: pyupgrade + args: [--py3-only] diff --git a/doc/source/conf.py b/doc/source/conf.py index 87c6ed1..e605e00 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/oslo_concurrency/fixture/lockutils.py b/oslo_concurrency/fixture/lockutils.py index e53816d..60d5e34 100644 --- a/oslo_concurrency/fixture/lockutils.py +++ b/oslo_concurrency/fixture/lockutils.py @@ -47,7 +47,7 @@ class LockFixture(fixtures.Fixture): self.mgr = lockutils.lock(name, lock_file_prefix, True) def setUp(self): - super(LockFixture, self).setUp() + super().setUp() self.addCleanup(self.mgr.__exit__, None, None, None) self.lock = self.mgr.__enter__() @@ -72,7 +72,7 @@ class ExternalLockFixture(fixtures.Fixture): .. versionadded:: 0.3 """ def setUp(self): - super(ExternalLockFixture, self).setUp() + super().setUp() temp_dir = self.useFixture(fixtures.TempDir()) conf = self.useFixture(config.Config(lockutils.CONF)).config conf(lock_path=temp_dir.path, group='oslo_concurrency') diff --git a/oslo_concurrency/lockutils.py b/oslo_concurrency/lockutils.py index b0ffaee..2111486 100644 --- a/oslo_concurrency/lockutils.py +++ b/oslo_concurrency/lockutils.py @@ -104,7 +104,7 @@ class ReaderWriterLock(fasteners.ReaderWriterLock): InterProcessLock = fasteners.InterProcessLock -class FairLocks(object): +class FairLocks: """A garbage collected container of fair locks. With a fair lock, contending lockers will get the lock in the order in @@ -147,7 +147,7 @@ def internal_fair_lock(name): return _fair_locks.get(name) -class Semaphores(object): +class Semaphores: """A garbage collected container of semaphores. This collection internally uses a weak value dictionary so that when a @@ -192,7 +192,7 @@ def _get_lock_path(name, lock_file_prefix, lock_path=None): name = name.replace(os.sep, '_') if lock_file_prefix: sep = '' if lock_file_prefix.endswith('-') else '-' - name = '%s%s%s' % (lock_file_prefix, sep, name) + name = '{}{}{}'.format(lock_file_prefix, sep, name) local_lock_path = lock_path or CONF.oslo_concurrency.lock_path diff --git a/oslo_concurrency/processutils.py b/oslo_concurrency/processutils.py index 99f48d1..af3d8bd 100644 --- a/oslo_concurrency/processutils.py +++ b/oslo_concurrency/processutils.py @@ -70,18 +70,18 @@ LOG = logging.getLogger(__name__) class InvalidArgumentError(Exception): def __init__(self, message=None): - super(InvalidArgumentError, self).__init__(message) + super().__init__(message) class UnknownArgumentError(Exception): def __init__(self, message=None): - super(UnknownArgumentError, self).__init__(message) + super().__init__(message) class ProcessExecutionError(Exception): def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None, description=None): - super(ProcessExecutionError, self).__init__( + super().__init__( stdout, stderr, exit_code, cmd, description) self.exit_code = exit_code self.stderr = stderr @@ -112,7 +112,7 @@ class ProcessExecutionError(Exception): class NoRootWrapSpecified(Exception): def __init__(self, message=None): - super(NoRootWrapSpecified, self).__init__(message) + super().__init__(message) def _subprocess_setup(on_preexec_fn): @@ -146,7 +146,7 @@ LOG_FINAL_ERROR = LogErrors.FINAL LOG_DEFAULT_ERROR = LogErrors.DEFAULT -class ProcessLimits(object): +class ProcessLimits: """Resource limits on a process. Attributes: @@ -192,7 +192,7 @@ class ProcessLimits(object): for limit in self._LIMITS: val = getattr(self, limit) if val is not None: - args.append("%s=%s" % (self._LIMITS[limit], val)) + args.append("{}={}".format(self._LIMITS[limit], val)) return args diff --git a/oslo_concurrency/tests/unit/test_lockutils.py b/oslo_concurrency/tests/unit/test_lockutils.py index ad312d2..993157b 100644 --- a/oslo_concurrency/tests/unit/test_lockutils.py +++ b/oslo_concurrency/tests/unit/test_lockutils.py @@ -68,7 +68,7 @@ def lock_files(handles_dir, out_queue): lock_file(handle) count += 1 unlock_file(handle) - except IOError: + except OSError: os._exit(2) finally: handle.close() @@ -78,7 +78,7 @@ def lock_files(handles_dir, out_queue): class LockTestCase(test_base.BaseTestCase): def setUp(self): - super(LockTestCase, self).setUp() + super().setUp() self.config = self.useFixture(config.Config(lockutils.CONF)).config def test_synchronized_wrapped_function_metadata(self): @@ -363,7 +363,7 @@ class LockTestCase(test_base.BaseTestCase): class FileBasedLockingTestCase(test_base.BaseTestCase): def setUp(self): - super(FileBasedLockingTestCase, self).setUp() + super().setUp() self.lock_dir = tempfile.mkdtemp() def test_lock_file_exists(self): @@ -399,7 +399,7 @@ class FileBasedLockingTestCase(test_base.BaseTestCase): lock1.trylock() lock1.unlock() time.sleep(0) - except IOError: + except OSError: # This is what we expect to happen break else: @@ -415,7 +415,7 @@ class FileBasedLockingTestCase(test_base.BaseTestCase): try: lock2.trylock() have_lock = True - except IOError: + except OSError: pass finally: # NOTE(bnemec): This is racy, but I don't want to add any @@ -557,7 +557,7 @@ class FileBasedLockingTestCase(test_base.BaseTestCase): class LockutilsModuleTestCase(test_base.BaseTestCase): def setUp(self): - super(LockutilsModuleTestCase, self).setUp() + super().setUp() self.old_env = os.environ.get('OSLO_LOCK_PATH') if self.old_env is not None: del os.environ['OSLO_LOCK_PATH'] @@ -565,7 +565,7 @@ class LockutilsModuleTestCase(test_base.BaseTestCase): def tearDown(self): if self.old_env is not None: os.environ['OSLO_LOCK_PATH'] = self.old_env - super(LockutilsModuleTestCase, self).tearDown() + super().tearDown() def test_main(self): script = '\n'.join([ @@ -597,7 +597,7 @@ class LockutilsModuleTestCase(test_base.BaseTestCase): class TestLockFixture(test_base.BaseTestCase): def setUp(self): - super(TestLockFixture, self).setUp() + super().setUp() self.config = self.useFixture(config.Config(lockutils.CONF)).config self.tempdir = tempfile.mkdtemp() @@ -606,7 +606,7 @@ class TestLockFixture(test_base.BaseTestCase): def tearDown(self): self._check_in_lock() - super(TestLockFixture, self).tearDown() + super().tearDown() def test_lock_fixture(self): # Setup lock fixture to test that teardown is inside the lock @@ -619,7 +619,7 @@ class TestLockFixture(test_base.BaseTestCase): class TestGetLockPath(test_base.BaseTestCase): def setUp(self): - super(TestGetLockPath, self).setUp() + super().setUp() self.conf = self.useFixture(config.Config(lockutils.CONF)).conf def test_get_default(self): diff --git a/oslo_concurrency/tests/unit/test_processutils.py b/oslo_concurrency/tests/unit/test_processutils.py index 5d636c3..b4f1493 100644 --- a/oslo_concurrency/tests/unit/test_processutils.py +++ b/oslo_concurrency/tests/unit/test_processutils.py @@ -89,7 +89,7 @@ class UtilsTest(test_base.BaseTestCase): on_completion_callback = mock.Mock() def fake_communicate(*args, timeout=None): - raise IOError("Broken pipe") + raise OSError("Broken pipe") mock_comm.side_effect = fake_communicate @@ -128,7 +128,7 @@ class UtilsTest(test_base.BaseTestCase): mock_comm.return_value = None mock_tpool.execute.return_value = mock_comm.return_value - fake_pinput = 'fake pinput'.encode('utf-8') + fake_pinput = b'fake pinput' with mock.patch.object(processutils, 'eventlet_patched', use_eventlet): @@ -240,7 +240,7 @@ exit 1 tmpfilename, tmpfilename2, attempts=10, process_input=b'foo', delay_on_retry=False) - fp = open(tmpfilename2, 'r') + fp = open(tmpfilename2) runs = fp.read() fp.close() self.assertNotEqual('failure', 'stdin did not ' @@ -320,7 +320,7 @@ grep foo # This test and the one below ensures that when communicate raises # an OSError, we do the right thing(s) def test_exception_on_communicate_error(self): - mock = self.useFixture(fixtures.MockPatch( + mock_comm = self.useFixture(fixtures.MockPatch( 'subprocess.Popen.communicate', side_effect=OSError(errno.EAGAIN, 'fake-test'))) @@ -330,10 +330,10 @@ grep foo 'false', check_exit_code=False) - self.assertEqual(1, mock.mock.call_count) + self.assertEqual(1, mock_comm.mock.call_count) def test_retry_on_communicate_error(self): - mock = self.useFixture(fixtures.MockPatch( + mock_comm = self.useFixture(fixtures.MockPatch( 'subprocess.Popen.communicate', side_effect=OSError(errno.EAGAIN, 'fake-test'))) @@ -344,11 +344,11 @@ grep foo check_exit_code=False, attempts=5) - self.assertEqual(5, mock.mock.call_count) + self.assertEqual(5, mock_comm.mock.call_count) def _test_and_check_logging_communicate_errors(self, log_errors=None, attempts=None): - mock = self.useFixture(fixtures.MockPatch( + mock_comm = self.useFixture(fixtures.MockPatch( 'subprocess.Popen.communicate', side_effect=OSError(errno.EAGAIN, 'fake-test'))) @@ -367,7 +367,8 @@ grep foo 'false', **kwargs) - self.assertEqual(attempts if attempts else 1, mock.mock.call_count) + self.assertEqual(attempts if attempts else 1, + mock_comm.mock.call_count) self.assertIn('Got an OSError', fixture.output) self.assertIn('errno: %d' % errno.EAGAIN, fixture.output) self.assertIn("'/usr/bin/env false'", fixture.output) @@ -530,7 +531,7 @@ grep foo class ProcessExecutionErrorLoggingTest(test_base.BaseTestCase): def setUp(self): - super(ProcessExecutionErrorLoggingTest, self).setUp() + super().setUp() self.tmpfilename = self.create_tempfiles( [["process_execution_error_logging_test", PROCESS_EXECUTION_ERROR_LOGGING_TEST]], @@ -624,7 +625,7 @@ class TryCmdTestCase(test_base.BaseTestCase): self.assertEqual('', e) -class FakeSshChannel(object): +class FakeSshChannel: def __init__(self, rc): self.rc = rc @@ -637,7 +638,7 @@ class FakeSshStream(io.BytesIO): self.channel = FakeSshChannel(rc) -class FakeSshConnection(object): +class FakeSshConnection: def __init__(self, rc, out=b'stdout', err=b'stderr'): self.rc = rc self.out = out diff --git a/oslo_concurrency/watchdog.py b/oslo_concurrency/watchdog.py index 7e48f20..01e98ba 100644 --- a/oslo_concurrency/watchdog.py +++ b/oslo_concurrency/watchdog.py @@ -64,7 +64,7 @@ def watch(logger, action, level=logging.DEBUG, after=5.0): watch.start() def log(): - msg = "%s not completed after %0.3fs" % (action, watch.elapsed()) + msg = "{} not completed after {:0.3f}s".format(action, watch.elapsed()) logger.log(level, msg) timer = threading.Timer(after, log) diff --git a/releasenotes/source/conf.py b/releasenotes/source/conf.py index a3dd5fb..8ba2192 100644 --- a/releasenotes/source/conf.py +++ b/releasenotes/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at