mariocesar/sorl-thumbnail — pull request #227 by carljm (2014-02-11 02:01:19+00:00)
Title: Don't call LazyObject._setup() unconditionally; can be destructive.

`ImageFile.serialize_storage()` currently calls `._setup()` unconditionally on any storage it encounters that is a `LazyObject` instance. For the case of Django's `DefaultStorage` class (which is a `LazyObject`), this call to `_setup()` is destructive; it instantiates a new instance of the underlying storage class, replacing the previous one, if any. This is probably fine for many common storage classes which are not stateful, but I use [django-inmemorystorage](https://github.com/codysoyland/django-inmemorystorage) in tests, and it maintains a "filesystem" in memory for the duration of the test run. This call to `_setup()` effectively wipes out that in-memory filesystem by instantiating a new instance of the storage class. It took me quite a while to track down the cause of this.

It would be better to follow the pattern of `LazyObject` itself, and only call `setup()` if `._wrapped is empty`. This will never destructively replace an existing wrapped instance with a new one. This pull request implements that change.