Do not assign new fp attribute when exiting context manager#7566
Conversation
| if hasattr(self, "fp"): | ||
| if getattr(self, "_exclusive_fp", False): | ||
| if getattr(self, "_fp", False): | ||
| if self._fp != self.fp: | ||
| self._fp.close() | ||
| self._fp = DeferredError(ValueError("Operation on closed image")) | ||
| if self.fp: | ||
| self.fp.close() | ||
| self.fp = None |
There was a problem hiding this comment.
Might be easier to read if the outermost layer of the pyramid was negated?
| if hasattr(self, "fp"): | |
| if getattr(self, "_exclusive_fp", False): | |
| if getattr(self, "_fp", False): | |
| if self._fp != self.fp: | |
| self._fp.close() | |
| self._fp = DeferredError(ValueError("Operation on closed image")) | |
| if self.fp: | |
| self.fp.close() | |
| self.fp = None | |
| if not hasattr(self, "fp"): | |
| return | |
| if getattr(self, "_exclusive_fp", False): | |
| if getattr(self, "_fp", False): | |
| if self._fp != self.fp: | |
| self._fp.close() | |
| self._fp = DeferredError(ValueError("Operation on closed image")) | |
| if self.fp: | |
| self.fp.close() | |
| self.fp = None |
There was a problem hiding this comment.
Easier to read in isolation, yes, but I kind of like the fact that this version mirrors close() -
Lines 530 to 565 in 5431b15
There was a problem hiding this comment.
Should that code be extracted from close() then and reused in both of these places? _close_fp()?
There was a problem hiding this comment.
Ok, I've pushed a commit.
| self.fp.close() | ||
| self.fp = None | ||
| if hasattr(self, "fp"): | ||
| if getattr(self, "_exclusive_fp", False): |
There was a problem hiding this comment.
Out of curiosity: does exclusive_fp mean, like I think it does, "we own this fp because we opened it"? If so, does it make sense for .close() to also close FPs that aren't "owned" by this instance?
At the moment,
gives
imdoes not have anfpattribute inside the context manager, but once it leaves, it is set.This PR keeps it unset.