Skip to content

Commit 137bc3d

Browse files
authored
Gracefully handle code which has been imported from a zipfile (#456)
1 parent dd1af68 commit 137bc3d

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/dotenv/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ def _is_interactive():
291291
frame = sys._getframe()
292292
current_file = __file__
293293

294-
while frame.f_code.co_filename == current_file:
294+
while frame.f_code.co_filename == current_file or not os.path.exists(
295+
frame.f_code.co_filename
296+
):
295297
assert frame.f_back is not None
296298
frame = frame.f_back
297299
frame_filename = frame.f_code.co_filename

tests/test_zip_imports.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import sys
3+
import sh
4+
import textwrap
5+
from typing import List
6+
from unittest import mock
7+
from zipfile import ZipFile
8+
9+
10+
def walk_to_root(path: str):
11+
last_dir = None
12+
current_dir = path
13+
while last_dir != current_dir:
14+
yield current_dir
15+
(parent_dir, _) = os.path.split(current_dir)
16+
last_dir, current_dir = current_dir, parent_dir
17+
18+
19+
class FileToAdd:
20+
def __init__(self, content: str, path: str):
21+
self.content = content
22+
self.path = path
23+
24+
25+
def setup_zipfile(path, files: List[FileToAdd]):
26+
zip_file_path = path / "test.zip"
27+
dirs_init_py_added_to = set()
28+
with ZipFile(zip_file_path, "w") as zip:
29+
for f in files:
30+
zip.writestr(data=f.content, zinfo_or_arcname=f.path)
31+
for dir in walk_to_root(os.path.dirname(f.path)):
32+
if dir not in dirs_init_py_added_to:
33+
print(os.path.join(dir, "__init__.py"))
34+
zip.writestr(
35+
data="", zinfo_or_arcname=os.path.join(dir, "__init__.py")
36+
)
37+
dirs_init_py_added_to.add(dir)
38+
return zip_file_path
39+
40+
41+
@mock.patch.object(sys, "path", list(sys.path))
42+
def test_load_dotenv_gracefully_handles_zip_imports_when_no_env_file(tmp_path):
43+
zip_file_path = setup_zipfile(
44+
tmp_path,
45+
[
46+
FileToAdd(
47+
content=textwrap.dedent(
48+
"""
49+
from dotenv import load_dotenv
50+
51+
load_dotenv()
52+
"""
53+
),
54+
path="child1/child2/test.py",
55+
),
56+
],
57+
)
58+
59+
# Should run without an error
60+
sys.path.append(str(zip_file_path))
61+
import child1.child2.test # noqa
62+
63+
64+
def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path):
65+
zip_file_path = setup_zipfile(
66+
tmp_path,
67+
[
68+
FileToAdd(
69+
content=textwrap.dedent(
70+
"""
71+
from dotenv import load_dotenv
72+
73+
load_dotenv()
74+
"""
75+
),
76+
path="child1/child2/test.py",
77+
),
78+
],
79+
)
80+
dotenv_path = tmp_path / ".env"
81+
dotenv_path.write_bytes(b"a=b")
82+
code_path = tmp_path / "code.py"
83+
code_path.write_text(
84+
textwrap.dedent(
85+
f"""
86+
import os
87+
import sys
88+
89+
sys.path.append("{zip_file_path}")
90+
91+
import child1.child2.test
92+
93+
print(os.environ['a'])
94+
"""
95+
)
96+
)
97+
os.chdir(str(tmp_path))
98+
99+
result = sh.Command(sys.executable)(code_path)
100+
101+
assert result == "b\n"

0 commit comments

Comments
 (0)