astrofrog/astropy — pull request #14 by eteq (2012-10-22 21:39:49+00:00)
Title: fix to make both context manager and regular funcional form work

I *think* this works as I described.

The key point is that you don't need the ``contextmanager`` decorator if the thing thing that's returned had `_enter__` and `__exit__` methods. This ensures that by attaching the methods if they're missing (which is true in py 2.x for `StringIO` and the result of `urlopen`, but *not* in py 3.x, where they do have the necessary methods).

As for the need for the `NamedTemporaryFile` closer, in my experiments it seems that if I open the `NamedTemporaryFile`, feed it to `BZ2File`, and then close the temp file, `BZ2File.read()` runs without complaint. If that's not working, a more complicated solution is needed anyway, because in the current form (before this PR), the `NamedTemporaryFile` closes as soon as this function returns (e.g. at the *start* of the with statement), because it goes out of scope.