Skip to content

Commit

Permalink
Testing: pyupgrade, replace exceptions that alias to OSError; rucio#6538
Browse files Browse the repository at this point in the history


In Python3.3+, a variety of exceptions (such as IOError) were aliased
to OSError, with the latter being the preferred exception to use.

See also:
- https://docs.python.org/3/library/exceptions.html#OSError
- https://stackoverflow.com/questions/29347790/difference-between-ioerror-and-oserror
- https://docs.astral.sh/ruff/rules/os-error-alias/
  • Loading branch information
rdimaio committed Mar 18, 2024
1 parent 36b79df commit 8decbdf
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/rucio/client/baseclient.py
Expand Up @@ -412,7 +412,7 @@ def _send_request(self, url, headers=None, type_='GET', data=None, params=None,
if retry > self.request_retries:
raise
continue
except IOError as error:
except OSError as error:
# Handle Broken Pipe
# While in python3 we can directly catch 'BrokenPipeError', in python2 it doesn't exist.
if getattr(error, 'errno') != errno.EPIPE:
Expand Down Expand Up @@ -859,7 +859,7 @@ def __read_token(self):
token_file_handler = open(self.token_file, 'r')
self.auth_token = token_file_handler.readline()
self.headers['X-Rucio-Auth-Token'] = self.auth_token
except IOError as error:
except OSError as error:
print("I/O error({0}): {1}".format(error.errno, error.strerror))
except Exception:
raise
Expand Down Expand Up @@ -890,7 +890,7 @@ def __write_token(self):
with fdopen(file_d, "w") as f_exp_epoch:
f_exp_epoch.write(str(self.token_exp_epoch))
move(file_n, self.token_exp_epoch_file)
except IOError as error:
except OSError as error:
print("I/O error({0}): {1}".format(error.errno, error.strerror))
except Exception:
raise
Expand Down
2 changes: 1 addition & 1 deletion lib/rucio/common/cache.py
Expand Up @@ -35,7 +35,7 @@
import pymemcache
_mc_client = pymemcache.Client(CACHE_URL, connect_timeout=1, timeout=1)
_mc_client.version()
except IOError:
except OSError:
ENABLE_CACHING = False
except ImportError:
ENABLE_CACHING = False
Expand Down
4 changes: 2 additions & 2 deletions lib/rucio/common/pcache.py
Expand Up @@ -1127,7 +1127,7 @@ def lock_file(self, name, blocking=True):
return
try:
f = open(name, 'w')
except IOError as e:
except OSError as e:
self.log(ERROR, "open: %s", e)
return e.errno

Expand All @@ -1139,7 +1139,7 @@ def lock_file(self, name, blocking=True):
try:
status = fcntl.lockf(f, flag)
break
except IOError as e:
except OSError as e:
if e.errno in (errno.EAGAIN, errno.EACCES) and not blocking:
f.close()
del self.locks[name]
Expand Down
2 changes: 1 addition & 1 deletion lib/rucio/core/rule.py
Expand Up @@ -2791,7 +2791,7 @@ def generate_email_for_rule_ok_notification(
try:
with open(template_path, 'r') as templatefile:
template = Template(templatefile.read())
except IOError as ex:
except OSError as ex:
logger(logging.ERROR, "Couldn't open file '%s'", template_path, exc_info=ex)
return

Expand Down
2 changes: 1 addition & 1 deletion lib/rucio/daemons/auditor/hdfs.py
Expand Up @@ -32,7 +32,7 @@ def _hdfs_get(src_url, dst_path):
)
_, stderr = get.communicate()
if get.returncode != 0:
raise IOError('_hdfs_get(): "{0}": {1}. Return code {2}'.format(
raise OSError('_hdfs_get(): "{0}": {1}. Return code {2}'.format(
' '.join(cmd),
stderr,
get.returncode,
Expand Down
8 changes: 4 additions & 4 deletions lib/rucio/rse/protocols/posix.py
Expand Up @@ -70,12 +70,12 @@ def get(self, pfn, dest, transfer_timeout=None):
"""
try:
shutil.copy(self.pfn2path(pfn), dest)
except IOError as e:
except OSError as e:
try: # To check if the error happend local or remote
with open(dest, 'wb'):
pass
call(['rm', '-rf', dest])
except IOError as e:
except OSError as e:
if e.errno == 2:
raise exception.DestinationNotAccessible(e)
else:
Expand Down Expand Up @@ -109,7 +109,7 @@ def put(self, source, target, source_dir=None, transfer_timeout=None):
if not os.path.exists(dirs):
os.makedirs(dirs)
shutil.copy(sf, target)
except IOError as e:
except OSError as e:
if e.errno == 2:
raise exception.SourceNotFound(e)
elif not self.exists(self.rse['prefix']):
Expand Down Expand Up @@ -151,7 +151,7 @@ def rename(self, pfn, new_pfn):
if not os.path.exists(os.path.dirname(new_path)):
os.makedirs(os.path.dirname(new_path))
os.rename(path, new_path)
except IOError as e:
except OSError as e:
if e.errno == 2:
if self.exists(self.pfn2path(path)):
raise exception.SourceNotFound(e)
Expand Down
4 changes: 2 additions & 2 deletions lib/rucio/rse/protocols/webdav.py
Expand Up @@ -331,13 +331,13 @@ def put(self, source, target, source_dir=None, transfer_timeout=None, progressba
raise exception.RucioException(result.status_code, result.text)
except requests.exceptions.ConnectionError as error:
raise exception.ServiceUnavailable(error)
except IOError as error:
except OSError as error:
raise exception.SourceNotFound(error)
except requests.exceptions.ConnectionError as error:
raise exception.ServiceUnavailable(error)
except requests.exceptions.ReadTimeout as error:
raise exception.ServiceUnavailable(error)
except IOError as error:
except OSError as error:
raise exception.SourceNotFound(error)

def rename(self, pfn, new_pfn):
Expand Down

0 comments on commit 8decbdf

Please sign in to comment.