Skip to content

Commit

Permalink
Improve load_file documentation and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Feb 14, 2024
1 parent 02a687a commit bd48b44
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions k4FWCore/python/k4FWCore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,30 @@ def import_from(
return module


def load_file(opt_file: Union[TextIOWrapper, os.PathLike]) -> None:
"""Load the file content and run it in the current interpreter session"""
if isinstance(opt_file, os.PathLike):
opt_file = open(opt_file, "r")
code = compile(opt_file.read(), opt_file.name, "exec")
def load_file(opt_file: Union[TextIOWrapper, str, os.PathLike]) -> None:
"""Loads and executes the content of a given file in the current interpreter session.
This function takes a file object or a path to a file, reads its content,
and then executes it as Python code within the global scope of the current
interpreter session. If `opt_file` is a file handle it will not be closed.
Args:
opt_file (Union[TextIOWrapper, str, os.PathLike]): A file object or a
path to the file that
contains Python code
to be executed.
Raises:
FileNotFoundError: If `opt_file` is a path and no file exists at that path.
IOError: If there's an error opening or reading the file.
SyntaxError: If there's a syntax error in the code being executed.
Exception: Any exception raised by the executed code will be propagated.
"""
if isinstance(opt_file, (str, os.PathLike)):
with open(opt_file, "r") as file:
code = compile(file.read(), file.name, "exec")
else:
code = compile(opt_file.read(), opt_file.name, "exec")

exec(code, globals())

0 comments on commit bd48b44

Please sign in to comment.