Skip to content

Commit

Permalink
Merge #446
Browse files Browse the repository at this point in the history
446: vfs: Handle if __main__ has no __file__ r=pathunstrom a=astronouth7303

Closes #267
Closes #437

Co-authored-by: Jamie Bliss <jamie@ivyleav.es>
Co-authored-by: Piper Thunstrom <pathunstrom@gmail.com>
  • Loading branch information
3 people authored May 9, 2020
2 parents 93f177a + 93d5d4f commit 8cfff31
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ppb/vfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

def _main_path():
main = sys.modules['__main__']
mainpath = getattr(main, '__file__')
mainpath = getattr(main, '__file__', None)
if mainpath:
mainpath = Path(mainpath)
return mainpath.absolute().parent
Expand Down
49 changes: 49 additions & 0 deletions tests/test_vfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import contextlib
import os
import tempfile

import __main__ # The pytest entry point

import pytest

import ppb.vfs


@contextlib.contextmanager
def save_main_file():
value = __main__.__file__
try:
yield
finally:
__main__.__file__ = value


@pytest.fixture
def cwd_file():
# Windows doesn't like multiple open handles, so we need to close and
# delete the temp file manually.

with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as ntf:
ntf.close()
yield os.path.basename(ntf.name)
os.remove(ntf.name)


def test_main_normal(cwd_file):
with pytest.raises(FileNotFoundError):
with ppb.vfs.open(cwd_file):
pass


def test_main_none(cwd_file):
with save_main_file():
__main__.__file__ = None
with ppb.vfs.open(cwd_file):
pass


def test_main_missing(cwd_file):
with save_main_file():
del __main__.__file__
with ppb.vfs.open(cwd_file):
pass

0 comments on commit 8cfff31

Please sign in to comment.