Skip to content

Commit a409657

Browse files
smsearcybbc2
authored andcommitted
Added special case for __file__ in Python 2. Fixes theskumar#130
Updated tests to show that this works.
1 parent b881091 commit a409657

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

src/dotenv/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,14 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False):
288288
# will work for .py files
289289
frame = sys._getframe()
290290
# find first frame that is outside of this file
291-
while frame.f_code.co_filename == __file__:
291+
if PY2 and not __file__.endswith('.py'):
292+
# in Python2 __file__ extension could be .pyc or .pyo (this doesn't account
293+
# for edge case of Python compiled for non-standard extension)
294+
current_file = __file__.rsplit('.', 1)[0] + '.py'
295+
else:
296+
current_file = __file__
297+
298+
while frame.f_code.co_filename == current_file:
292299
frame = frame.f_back
293300
frame_filename = frame.f_code.co_filename
294301
path = os.path.dirname(os.path.abspath(frame_filename))

tests/test_core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ def test_load_dotenv_override(tmp_path):
174174
assert os.environ[key_name] == 'WORKS'
175175

176176

177-
@pytest.mark.xfail(sys.version_info < (3, 0), reason="test was incomplete")
178177
def test_load_dotenv_in_current_dir(tmp_path):
179178
dotenv_path = tmp_path / '.env'
180179
dotenv_path.write_bytes(b'a=b')

0 commit comments

Comments
 (0)