pymad/pymad — pull request #41 by coldfix (2014-01-24 04:47:37+00:00)
Title: Test a previously uncovered scenario

The added test checks that the `PackageResource.filename()` method is
working as expected when called multiple times (subsequently).

**TODO:**

There is one more uncovered case that will misbehave with the current
implementation:

```python
with res.filename() as f1:
with res.filename() as f2:
assert(f1 == f2) # True
assert(os.exists(f1)) # False (as a bad consequence)
# will probably raise exception at this point (when exiting the
# context manager)
```

The easiest fix for this problem is to drop the cleanup of the file in
the __exit__ method. Another possibility might be a reference count. Not
sure how to do this, though.

There is another reason to remove the cleanup code: When calling
pkg_resources.get_resource_filename() the requested resource might not
be the only resource that is extracted at this point,
[c.f.](http://pythonhosted.org/setuptools/pkg_resources.html#resource-extraction):

If the named resource is a directory, then all resources within that
directory (including subdirectories) are also extracted. If the
named resource is a C extension or “eager resource” (see the
setuptools documentation for details), then all C extensions and
eager resources are extracted at the same time.

Apparently, pkg_resources uses
[`tempfile.mkstemp`](http://docs.python.org/3.3/library/tempfile.html#tempfile.mkstemp)
to create a temporary file. If there are *useful* guarantees about the
life time of this temporary file, removing the cleanup clause should
definitely be considered. I will try to find out if this requirement is
satisfied.