{"action":"created","issue":{"url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/135","repository_url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs","labels_url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/135/labels{/name}","comments_url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/135/comments","events_url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/135/events","html_url":"https://github.com/jmcgeheeiv/pyfakefs/issues/135","id":190101284,"number":135,"title":"Using Patcher with py.test","user":{"login":"mirosval","id":1315417,"avatar_url":"https://avatars.githubusercontent.com/u/1315417?v=3","gravatar_id":"","url":"https://api.github.com/users/mirosval","html_url":"https://github.com/mirosval","followers_url":"https://api.github.com/users/mirosval/followers","following_url":"https://api.github.com/users/mirosval/following{/other_user}","gists_url":"https://api.github.com/users/mirosval/gists{/gist_id}","starred_url":"https://api.github.com/users/mirosval/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mirosval/subscriptions","organizations_url":"https://api.github.com/users/mirosval/orgs","repos_url":"https://api.github.com/users/mirosval/repos","events_url":"https://api.github.com/users/mirosval/events{/privacy}","received_events_url":"https://api.github.com/users/mirosval/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-17T16:49:58Z","updated_at":"2016-11-17T21:17:56Z","closed_at":null,"body":"I have run into an issue using `Patcher` with py.test. \r\n\r\nThe issue is that when the code under test throws, the exception bubbles up to `py.test`, which will then fail processing it, because `os.path` is patched. \r\n\r\nThis then results into a horrible mess of tracebacks and exceptions during handling of other exceptions and the original exception is lost, so it makes debugging the underlying problem difficult.\r\n\r\nExample:\r\n```python\r\npatcher = Patcher()\r\npatcher.setUp()\r\nfoo()  # throws\r\npatcher.tearDown()\r\n```\r\n\r\nI fixed this by wrapping `Patcher` in a context manager like so:\r\n```python\r\nclass ManagedPatcher(Patcher):\r\n        def __enter__(self):\r\n            self.setUp()\r\n\r\n        def __exit__(self, exc_type, exc_val, exc_tb):\r\n            self.tearDown()\r\n\r\nwith ManagedPatcher():\r\n    foo()\r\n```\r\n\r\nThis ensures that when an exception is thrown, the patch is unapplied on exit from the context block and the exception is allowed to correctly bubble up and be caught by `py.test`.\r\n\r\nI would suggest adding the two context methods to the original `Patcher`. I could make a PR for this if you like."},"comment":{"url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/comments/261372240","html_url":"https://github.com/jmcgeheeiv/pyfakefs/issues/135#issuecomment-261372240","issue_url":"https://api.github.com/repos/jmcgeheeiv/pyfakefs/issues/135","id":261372240,"user":{"login":"mirosval","id":1315417,"avatar_url":"https://avatars.githubusercontent.com/u/1315417?v=3","gravatar_id":"","url":"https://api.github.com/users/mirosval","html_url":"https://github.com/mirosval","followers_url":"https://api.github.com/users/mirosval/followers","following_url":"https://api.github.com/users/mirosval/following{/other_user}","gists_url":"https://api.github.com/users/mirosval/gists{/gist_id}","starred_url":"https://api.github.com/users/mirosval/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mirosval/subscriptions","organizations_url":"https://api.github.com/users/mirosval/orgs","repos_url":"https://api.github.com/users/mirosval/repos","events_url":"https://api.github.com/users/mirosval/events{/privacy}","received_events_url":"https://api.github.com/users/mirosval/received_events","type":"User","site_admin":false},"created_at":"2016-11-17T21:17:56Z","updated_at":"2016-11-17T21:17:56Z","body":"Oh fantastic, I was hoping you'd say that. ;)\n\nOn Thu, Nov 17, 2016 at 9:53 PM mrbean-bremen <notifications@github.com>\nwrote:\n\n> Thanks - this sounds good! A context manager for Patcher certainly makes\n> sense in this context. I may add this tomorrow - as it's a small addition,\n> I think a PR is not really needed.\n>\n> —\n> You are receiving this because you authored the thread.\n> Reply to this email directly, view it on GitHub\n> <https://github.com/jmcgeheeiv/pyfakefs/issues/135#issuecomment-261366167>,\n> or mute the thread\n> <https://github.com/notifications/unsubscribe-auth/ABQSWXgSgaROs_5RN0DFpzmrZvCgonO6ks5q_L6-gaJpZM4K1imx>\n> .\n>\n-- \nBest Regards,\nMiroslav Zoricak\n"}}
{"actor":{"display_login":"mirosval"}}