A context for prepending a directory to sys.path for a second.
| 40 | return False |
| 41 | |
| 42 | class prepended_to_syspath(object): |
| 43 | """A context for prepending a directory to sys.path for a second.""" |
| 44 | |
| 45 | def __init__(self, dir): |
| 46 | self.dir = dir |
| 47 | |
| 48 | def __enter__(self): |
| 49 | if self.dir not in sys.path: |
| 50 | sys.path.insert(0,self.dir) |
| 51 | self.added = True |
| 52 | else: |
| 53 | self.added = False |
| 54 | |
| 55 | def __exit__(self, type, value, traceback): |
| 56 | if self.added: |
| 57 | try: |
| 58 | sys.path.remove(self.dir) |
| 59 | except ValueError: |
| 60 | pass |
| 61 | # Returning False causes any exceptions to be re-raised. |
| 62 | return False |
no outgoing calls