Skip to content

Commit a9fcee8

Browse files
hembergernedbat
authored andcommitted
Return to the original directory after exec'ing a file
If the file that is exec'd chdirs to a directory that doesn't exist at the end of the execution, then we will fail to connect to the SQLite database (due to a failing `os.getcwd` command). We can easily fix this if we ensure we are in a directory that exists after executing the foreign code. Returning to the original directory seems to be a sensible choice.
1 parent 9ae35a5 commit a9fcee8

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

coverage/execfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ def run(self):
196196
# a non-existent directory.
197197
cwd = os.getcwd()
198198
try:
199+
# Return to the original directory in case the test code exits in
200+
# a non-existent directory.
201+
cwd = os.getcwd()
199202
exec(code, main_mod.__dict__)
203+
os.chdir(cwd)
200204
except SystemExit: # pylint: disable=try-except-raise
201205
# The user called sys.exit(). Just pass it along to the upper
202206
# layers, where it will be handled.

0 commit comments

Comments
 (0)