Skip to content

Commit 3f58831

Browse files
authored
printEnvVariablesToFile.py overwrites itself if invoked without arguments (#24487)
I saw the command in my terminal when launching the editor, was curious to see what it did, so I ran it again without arguments, expecting to get a `--help`-like message, instead it ended up overwriting itself. This change changes the script's default behavior (default being the invocation without arguments) from overwriting itself to throwing an exception about a missing output file argument.
1 parent dba0a4c commit 3f58831

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

python_files/printEnvVariablesToFile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import os
55
import sys
66

7-
# Last argument is the target file into which we'll write the env variables line by line.
8-
output_file = sys.argv[-1]
7+
# Prevent overwriting itself, since sys.argv[0] is the path to this file
8+
if len(sys.argv) > 1:
9+
# Last argument is the target file into which we'll write the env variables line by line.
10+
output_file = sys.argv[-1]
11+
else:
12+
raise ValueError("Missing output file argument")
913

1014
with open(output_file, "w") as outfile: # noqa: PTH123
1115
for key, val in os.environ.items():

0 commit comments

Comments
 (0)