-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Simplify away safe_text_dupfile/EncodedFile #6875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I tried to understand what the `safe_text_dupfile()` function and `EncodedFile` class do. Outside tests, `EncodedFile` is only used by `safe_text_dupfile`, and `safe_text_dupfile` is only used by `FDCaptureBinary.__init__()`. I then started to eliminate always-true conditions based on the single call site, and in the end nothing was left. The old code had some workarounds: - Ensure `errors` property is set: pytest-dev#555. It is set now, although now it will return `replace`, which is the actual method used also before, instead of `strict`. AFAIU, the issue just wanted it to be set to something, not `strict` specifically. - Infinite recursion trying to pickle `EncodedFile` (f7282b8). Not sure, I don't think this object should be pickle-able at all (and it isn't...). - A bug in `logging.Handler.__repr__` with int `file.name`. pytest-dev#2555 Was fixed in Python>=3.7.4: https://bugs.python.org/issue36015. TODO: This workaround is probably still needed?
| def snap(self): | ||
| self.tmpfile.seek(0) | ||
| res = self.tmpfile.read() | ||
| res = self.tmpfile.buffer.read() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I think is a bug fix anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe fixes #6871 ?
Might make sense to pull it out then already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But would only avoid the indirection via __getattr__, no? (with EncodedFile being used)
| EMPTY_BUFFER = str() # type: ignore | ||
|
|
||
| def snap(self): | ||
| res = super().snap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since tmpfile is now text-mode for reading also (not just for writing like the weird EncodedFile did), I switched to read directly. Also symmetric with the Sys variants which do the same.
|
Hmm, seems like |
|
Replaced by #6899. |
I tried to understand what the
safe_text_dupfile()function andEncodedFileclass do. Outside tests,EncodedFileis only used bysafe_text_dupfile, andsafe_text_dupfileis only used byFDCaptureBinary.__init__(). I then started to eliminate always-true conditions based on the single call site, and in the end nothing was left.The old code had some workarounds:
Ensure
errorsproperty is set: AttributeError: '_io.FileIO' object has no attribute 'errors' #555. It is set now, although now it will returnreplace, which is the actual method used also before, instead ofstrict. AFAIU, the issue just wanted it to be set to something, notstrictspecifically.Infinite recursion trying to pickle
EncodedFile(f7282b8). Not sure, I don't think this object should be pickle-able at all (and it isn't...).A bug in
logging.Handler.__repr__with intfile.name: TypeError with logging.StreamHandler.__repr__ and pytest's EncodedFile #2555Was fixed in Python>=3.7.4: https://bugs.python.org/issue36015.
TODO: This workaround is probably still needed?