Skip to content

Commit

Permalink
Refactor: Replace deprecated IOError with OSError (#6441)
Browse files Browse the repository at this point in the history
PEP 3151 reworked the builtin exception hierarchy and `IOError` was
deprecated (kept as an alias) in favor of `OSError`. The change was
implemented in Python 3.3: https://peps.python.org/pep-3151
  • Loading branch information
sphuber authored Jun 1, 2024
1 parent 7b7958c commit 7f9129f
Show file tree
Hide file tree
Showing 27 changed files with 135 additions and 135 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check_release_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_version_from_module(content: str) -> str:
try:
module = ast.parse(content)
except SyntaxError as exc:
raise IOError(f'Unable to parse module: {exc}')
raise OSError(f'Unable to parse module: {exc}')
try:
return next(
ast.literal_eval(statement.value)
Expand All @@ -21,7 +21,7 @@ def get_version_from_module(content: str) -> str:
if isinstance(target, ast.Name) and target.id == '__version__'
)
except StopIteration:
raise IOError('Unable to find __version__ in module')
raise OSError('Unable to find __version__ in module')


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def parse(self, **kwargs):
try:
with output_folder.open(self.node.get_option('output_filename'), 'r') as handle:
result = self.parse_stdout(handle)
except (OSError, IOError):
except OSError:
return self.exit_codes.ERROR_READING_OUTPUT_FILE

if result is None:
Expand Down
14 changes: 7 additions & 7 deletions docs/source/topics/transport_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def chdir(self, path):
"""Change directory to 'path'.
:param str path: path to change working directory into.
:raises: IOError, if the requested path does not exist
:raises: OSError, if the requested path does not exist
:rtype: string
"""

Expand All @@ -35,7 +35,7 @@ def copy(self, remotesource, remotedestination, *args, **kwargs):
:param str remotesource: path of the remote source directory / file
:param str remotedestination: path of the remote destination directory / file
:raises: IOError, if source or destination does not exist
:raises: OSError, if source or destination does not exist
"""

def copyfile(self, remotesource, remotedestination, *args, **kwargs):
Expand All @@ -44,7 +44,7 @@ def copyfile(self, remotesource, remotedestination, *args, **kwargs):
:param str remotesource: path of the remote source directory / file
:param str remotedestination: path of the remote destination directory / file
:raises IOError: if one of src or dst does not exist
:raises OSError: if one of src or dst does not exist
"""

def copytree(self, remotesource, remotedestination, *args, **kwargs):
Expand All @@ -53,7 +53,7 @@ def copytree(self, remotesource, remotedestination, *args, **kwargs):
:param str remotesource: path of the remote source directory / file
:param str remotedestination: path of the remote destination directory / file
:raise IOError: if one of src or dst does not exist
:raise OSError: if one of src or dst does not exist
"""

def exec_command_wait_bytes(self, command, stdin=None, **kwargs):
Expand Down Expand Up @@ -177,7 +177,7 @@ def normalize(self, path='.'):
"current folder".
:param str path: path to be normalized
:raise IOError: if the path can't be resolved on the server
:raise OSError: if the path can't be resolved on the server
"""

def put(self, localpath, remotepath, *args, **kwargs):
Expand Down Expand Up @@ -214,7 +214,7 @@ def rename(self, src, dst):
:param str oldpath: existing name of the file or folder
:param str newpath: new name for the file or folder
:raises IOError: if src/dst is not found
:raises OSError: if src/dst is not found
:raises ValueError: if src/dst is not a valid string
"""

Expand All @@ -224,7 +224,7 @@ def remove(self, path):
This only works on files; for removing folders (directories), use rmdir.
:param str path: path to file to remove
:raise IOError: if the path is a directory
:raise OSError: if the path is a directory
"""

def rmdir(self, path):
Expand Down
2 changes: 1 addition & 1 deletion src/aiida/cmdline/commands/cmd_calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def calcjob_remotecat(calcjob, path):
remote_folder.getfile(path, tmp_path.name)
with open(tmp_path.name, 'rb') as handle:
shutil.copyfileobj(handle, sys.stdout.buffer)
except IOError as exception:
except OSError as exception:
echo.echo_critical(str(exception))


Expand Down
2 changes: 1 addition & 1 deletion src/aiida/cmdline/commands/cmd_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _computer_create_temp_file(transport, scheduler, authinfo, computer):

try:
transport.chdir(workdir)
except IOError:
except OSError:
transport.makedirs(workdir)
transport.chdir(workdir)

Expand Down
2 changes: 1 addition & 1 deletion src/aiida/cmdline/commands/cmd_data/cmd_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def cif_content(data):
for node in data:
try:
echo.echo(node.get_content())
except IOError as exception:
except OSError as exception:
echo.echo_warning(f'could not read the content for CifData<{node.pk}>: {exception!s}')


Expand Down
4 changes: 2 additions & 2 deletions src/aiida/cmdline/commands/cmd_data/cmd_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def remote_ls(ls_long, path, datum):

try:
content = datum.listdir_withattributes(path=path)
except (IOError, OSError) as err:
except OSError as err:
echo.echo_critical(f'Unable to access the remote folder or file, check if it exists.\nOriginal error: {err!s}')
for metadata in content:
if ls_long:
Expand Down Expand Up @@ -71,7 +71,7 @@ def remote_cat(datum, path):
datum.getfile(path, tmpf.name)
with open(tmpf.name, encoding='utf8') as fhandle:
sys.stdout.write(fhandle.read())
except IOError as err:
except OSError as err:
echo.echo_critical(f'{err.errno}: {err!s}')

try:
Expand Down
2 changes: 1 addition & 1 deletion src/aiida/cmdline/commands/cmd_data/cmd_singlefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ def singlefile_content(datum):
"""Show the content of the file."""
try:
echo.echo(datum.get_content())
except (IOError, OSError) as exception:
except OSError as exception:
echo.echo_critical(f'could not read the content for SinglefileData<{datum.pk}>: {exception!s}')
6 changes: 3 additions & 3 deletions src/aiida/common/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def md5_from_filelike(filelike, block_size_factor=128):
:param block_size_factor: the file is read at chunks of size ``block_size_factor * md5.block_size``, where
``md5.block_size`` is the block_size used internally by the hashlib module.
:returns: a string with the hexdigest md5.
:raises: no checks are done on the filelike object, so it may raise IOError if it cannot be read from.
:raises: no checks are done on the filelike object, so it may raise OSError if it cannot be read from.
"""
md5 = hashlib.md5()

Expand All @@ -37,7 +37,7 @@ def md5_file(filepath, block_size_factor=128):
``md5.block_size`` is the block_size used internally by the hashlib module.
:returns: a string with the hexdigest md5.
:raises: No checks are done on the file, so if it doesn't exists it may
raise IOError.
raise OSError.
"""
with open(filepath, 'rb', encoding=None) as handle:
return md5_from_filelike(handle, block_size_factor=block_size_factor)
Expand All @@ -55,7 +55,7 @@ def sha1_file(filename, block_size_factor=128):
:returns: a string with the hexdigest sha1.
:raises: No checks are done on the file, so if it doesn't exists it may
raise IOError.
raise OSError.
"""
sha1 = hashlib.sha1()
with open(filename, 'rb', encoding=None) as fhandle:
Expand Down
10 changes: 5 additions & 5 deletions src/aiida/common/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def insert_path(self, src, dest_name=None, overwrite=True):
# This automatically overwrites files
shutil.copyfile(src, dest_abs_path)
else:
raise IOError(f'destination already exists: {os.path.join(dest_abs_path)}')
raise OSError(f'destination already exists: {os.path.join(dest_abs_path)}')
else:
shutil.copyfile(src, dest_abs_path)
elif os.path.isdir(src):
Expand All @@ -197,7 +197,7 @@ def insert_path(self, src, dest_name=None, overwrite=True):
# This automatically overwrites files
shutil.copytree(src, dest_abs_path)
else:
raise IOError(f'destination already exists: {os.path.join(dest_abs_path)}')
raise OSError(f'destination already exists: {os.path.join(dest_abs_path)}')
else:
shutil.copytree(src, dest_abs_path)
else:
Expand Down Expand Up @@ -339,11 +339,11 @@ def replace_with_folder(self, srcdir, move=False, overwrite=False):
:param move: if True, the srcdir is moved to the repository. Otherwise, it is only copied.
:type move: bool
:param overwrite: if True, the folder will be erased first.
if False, an IOError is raised if the folder already exists.
if False, an OSError is raised if the folder already exists.
Whatever the value of this flag, parent directories will be created, if needed.
:type overwrite: bool
:raises IOError: in case of problems accessing or writing the files.
:raises OSError: in case of problems accessing or writing the files.
:raises OSError: in case of problems accessing or writing the files (from ``shutil`` module).
:raises ValueError: if the section is not recognized.
"""
Expand All @@ -352,7 +352,7 @@ def replace_with_folder(self, srcdir, move=False, overwrite=False):
if overwrite:
self.erase()
elif self.exists():
raise IOError(f'Location {self.abspath} already exists, and overwrite is set to False')
raise OSError(f'Location {self.abspath} already exists, and overwrite is set to False')

# Create parent dir, if needed, with the right mode
pardir = os.path.dirname(self.abspath)
Expand Down
6 changes: 3 additions & 3 deletions src/aiida/engine/daemon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def get_circus_port(self) -> int:
try:
with open(self.circus_port_file, 'r', encoding='utf8') as fhandle:
return int(fhandle.read().strip())
except (ValueError, IOError):
except (ValueError, OSError):
raise RuntimeError('daemon is running so port file should have been there but could not read it')
else:
port = self.get_available_port()
Expand Down Expand Up @@ -241,7 +241,7 @@ def get_circus_socket_directory(self) -> str:
with open(self.circus_socket_file, 'r', encoding='utf8') as fhandle:
content = fhandle.read().strip()
return content
except (ValueError, IOError):
except (ValueError, OSError):
raise RuntimeError('daemon is running so sockets file should have been there but could not read it')
else:
# The SOCKET_DIRECTORY is already set, a temporary directory was already created and the same should be used
Expand All @@ -265,7 +265,7 @@ def get_daemon_pid(self) -> int | None:
with open(self.circus_pid_file, 'r', encoding='utf8') as fhandle:
content = fhandle.read().strip()
return int(content)
except (ValueError, IOError):
except (ValueError, OSError):
return None
else:
return None
Expand Down
10 changes: 5 additions & 5 deletions src/aiida/engine/daemon/execmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def upload_calculation(
# If it already exists, no exception is raised
try:
transport.chdir(remote_working_directory)
except IOError:
except OSError:
logger.debug(
f'[submission of calculation {node.pk}] Unable to '
f'chdir in {remote_working_directory}, trying to create it'
Expand Down Expand Up @@ -296,7 +296,7 @@ def _copy_remote_files(logger, node, computer, transport, remote_copy_list, remo
f'[submission of calculation {node.pk}] Unable to copy remote '
f'resource from {remote_abs_path} to {dest_rel_path}! NOT Stopping but just ignoring!.'
)
except (IOError, OSError):
except OSError:
logger.warning(
f'[submission of calculation {node.pk}] Unable to copy remote '
f'resource from {remote_abs_path} to {dest_rel_path}! Stopping.'
Expand All @@ -318,14 +318,14 @@ def _copy_remote_files(logger, node, computer, transport, remote_copy_list, remo
try:
transport.makedirs(remote_dirname, ignore_existing=True)
transport.symlink(remote_abs_path, dest_rel_path)
except (IOError, OSError):
except OSError:
logger.warning(
f'[submission of calculation {node.pk}] Unable to create remote symlink '
f'from {remote_abs_path} to {dest_rel_path}! Stopping.'
)
raise
else:
raise IOError(
raise OSError(
f'It is not possible to create a symlink between two different machines for calculation {node.pk}'
)

Expand Down Expand Up @@ -459,7 +459,7 @@ def stash_calculation(calculation: CalcJobNode, transport: Transport) -> None:

try:
transport.copy(str(source_filepath), str(target_filepath))
except (IOError, ValueError) as exception:
except (OSError, ValueError) as exception:
EXEC_LOGGER.warning(f'failed to stash {source_filepath} to {target_filepath}: {exception}')
else:
EXEC_LOGGER.debug(f'stashed {source_filepath} to {target_filepath}')
Expand Down
2 changes: 1 addition & 1 deletion src/aiida/engine/processes/workchains/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def on_terminated(self):
try:
called_descendant.outputs.remote_folder._clean()
cleaned_calcs.append(str(called_descendant.pk))
except (IOError, OSError, KeyError):
except (OSError, OSError, KeyError):
pass

if cleaned_calcs:
Expand Down
24 changes: 12 additions & 12 deletions src/aiida/orm/nodes/data/remote/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def is_empty(self):
with transport:
try:
transport.chdir(self.get_remote_path())
except IOError:
# If the transport IOError the directory no longer exists and was deleted
except OSError:
# If the transport OSError the directory no longer exists and was deleted
return True

return not transport.listdir()
Expand All @@ -78,9 +78,9 @@ def getfile(self, relpath, destpath):
try:
full_path = os.path.join(self.get_remote_path(), relpath)
transport.getfile(full_path, destpath)
except IOError as exception:
except OSError as exception:
if exception.errno == 2: # file does not exist
raise IOError(
raise OSError(
'The required remote file {} on {} does not exist or has been deleted.'.format(
full_path, self.computer.label
)
Expand All @@ -99,9 +99,9 @@ def listdir(self, relpath='.'):
try:
full_path = os.path.join(self.get_remote_path(), relpath)
transport.chdir(full_path)
except IOError as exception:
except OSError as exception:
if exception.errno in (2, 20): # directory not existing or not a directory
exc = IOError(
exc = OSError(
f'The required remote folder {full_path} on {self.computer.label} does not exist, is not a '
'directory or has been deleted.'
)
Expand All @@ -112,9 +112,9 @@ def listdir(self, relpath='.'):

try:
return transport.listdir()
except IOError as exception:
except OSError as exception:
if exception.errno in (2, 20): # directory not existing or not a directory
exc = IOError(
exc = OSError(
f'The required remote folder {full_path} on {self.computer.label} does not exist, is not a '
'directory or has been deleted.'
)
Expand All @@ -135,9 +135,9 @@ def listdir_withattributes(self, path='.'):
try:
full_path = os.path.join(self.get_remote_path(), path)
transport.chdir(full_path)
except IOError as exception:
except OSError as exception:
if exception.errno in (2, 20): # directory not existing or not a directory
exc = IOError(
exc = OSError(
f'The required remote folder {full_path} on {self.computer.label} does not exist, is not a '
'directory or has been deleted.'
)
Expand All @@ -148,9 +148,9 @@ def listdir_withattributes(self, path='.'):

try:
return transport.listdir_withattributes()
except IOError as exception:
except OSError as exception:
if exception.errno in (2, 20): # directory not existing or not a directory
exc = IOError(
exc = OSError(
f'The required remote folder {full_path} on {self.computer.label} does not exist, is not a '
'directory or has been deleted.'
)
Expand Down
4 changes: 2 additions & 2 deletions src/aiida/orm/nodes/process/calculation/calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def get_scheduler_stdout(self) -> Optional[AnyStr]:

try:
stdout = retrieved_node.base.repository.get_object_content(filename)
except IOError:
except OSError:
stdout = None

return stdout
Expand All @@ -538,7 +538,7 @@ def get_scheduler_stderr(self) -> Optional[AnyStr]:

try:
stderr = retrieved_node.base.repository.get_object_content(filename)
except IOError:
except OSError:
stderr = None

return stderr
Expand Down
4 changes: 2 additions & 2 deletions src/aiida/orm/utils/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def store_source_info(self, func) -> None:

try:
source_list, starting_line_number = inspect.getsourcelines(func)
except (IOError, OSError):
except OSError:
pass
else:
self._set_function_starting_line_number(starting_line_number)
Expand All @@ -63,7 +63,7 @@ def store_source_info(self, func) -> None:
self.base.repository.put_object_from_filelike( # type: ignore[attr-defined]
handle, self.FUNCTION_SOURCE_FILE_PATH
)
except (IOError, OSError):
except OSError:
pass

@property
Expand Down
Loading

0 comments on commit 7f9129f

Please sign in to comment.