Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nesting of a single context manager instance for fromdir #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions codado/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class fromdir(object):
"""
def __init__(self, *paths):
paths = list(paths)
self._origDir = []
self.path = ''
for cur in paths:
cur = os.path.expanduser(cur)
Expand All @@ -173,12 +174,12 @@ def __call__(self, *args):
return os.path.join(*a)

def __enter__(self):
self._origDir = os.getcwd()
self._origDir.append(os.getcwd())
os.chdir(self.path)
return self

def __exit__(self, type, value, tb):
os.chdir(self._origDir)
os.chdir(self._origDir.pop())


def remoji():
Expand Down
15 changes: 15 additions & 0 deletions codado/test/test_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def test_fromdir():
assert py.fromdir('~')() == os.environ['HOME']


def test_fromdirNesting():
"""
Test an unrecommended but possible nesting of the same context manager instance.
"""

startingCwd = os.getcwd()

testDir = py.fromdir('..')
with testDir:
with testDir:
pass

assert startingCwd == os.getcwd()


def test_enum():
"""
Do I permit attribute access to keys?
Expand Down