Simplify implementation of TemporaryWorkingDirectory.

Closes gh-5160
Merge pull request #5163 from takluyver/i5160

Simplify implementation of TemporaryWorkingDirectory.