diff options
author | Thomas Kluyver <takowl@gmail.com> | 2014-12-19 17:05:18 -0800 |
---|---|---|
committer | Thomas Kluyver <takowl@gmail.com> | 2014-12-19 17:05:36 -0800 |
commit | fbe8a38de0fd59b525ea479e19a55f4d20edf107 (patch) | |
tree | 89562ccf23b29d485c58ded605217475526bad9b /pexpect/spawnbase.py | |
parent | 77357d107b1ebb38287695149d7cf1e66df4addd (diff) | |
download | pexpect-git-fbe8a38de0fd59b525ea479e19a55f4d20edf107.tar.gz |
Allow spawn() and friends to be used as context managers
Closes gh-111
Diffstat (limited to 'pexpect/spawnbase.py')
-rw-r--r-- | pexpect/spawnbase.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/pexpect/spawnbase.py b/pexpect/spawnbase.py index b1bc2f3..d79c5c0 100644 --- a/pexpect/spawnbase.py +++ b/pexpect/spawnbase.py @@ -443,6 +443,15 @@ class SpawnBase(object): """Overridden in subclass using tty""" return False + # For 'with spawn(...) as child:' + def __enter__(self): + return self + + def __exit__(self, etype, evalue, tb): + # We rely on subclasses to implement close(). If they don't, it's not + # clear what a context manager should do. + self.close() + class SpawnBaseUnicode(SpawnBase): if PY3: string_type = str |