Skip to content

Commit

Permalink
Ensure --outfile isn't a directory (#800)
Browse files Browse the repository at this point in the history
Co-authored-by: sarahec <sarahec@nextquestion.net>
  • Loading branch information
sarahec and sarahec committed Mar 30, 2024
1 parent b03a596 commit 8e78842
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scalene/scalene_parseargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def clean_exit(code: object = 0) -> NoReturn:
"""Replacement for sys.exit that exits cleanly from within Jupyter notebooks."""
raise StopJupyterExecution



@staticmethod
def parse_args() -> Tuple[argparse.Namespace, List[str]]:
# In IPython, intercept exit cleanly (because sys.exit triggers a backtrace).
Expand Down Expand Up @@ -353,6 +355,10 @@ def parse_args() -> Tuple[argparse.Namespace, List[str]]:
# https://stackoverflow.com/questions/35733262/is-there-any-way-to-instruct-argparse-python-2-7-to-remove-found-arguments-fro
args, left = parser.parse_known_args()

# Validate file/directory arguments
if args.outfile and os.path.isdir(args.outfile):
parser.error(f"outfile {args.outfile} is a directory")

# Hack to simplify functionality for Windows platforms.
if sys.platform == "win32":
args.on = True
Expand Down
10 changes: 10 additions & 0 deletions test/issues/test-issue691.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys
import subprocess
import tempfile

dir = tempfile.TemporaryDirectory()
cmd = [sys.executable, "-m", "scalene", "--cli", "--outfile", dir.name, "../testme.py", "--cpu-only"]

print(cmd)
print(f'If bug 691 is fixed, you will see \n scalene: error: outfile {dir.name} is a directory')
proc = subprocess.run(cmd)

0 comments on commit 8e78842

Please sign in to comment.