-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,7 @@ | |
import sys | ||
from io import UnsupportedOperation | ||
from tempfile import TemporaryFile | ||
from typing import BinaryIO | ||
from typing import Generator | ||
from typing import Iterable | ||
from typing import Optional | ||
|
||
import pytest | ||
|
@@ -390,55 +388,6 @@ def disabled(self): | |
yield | ||
|
||
|
||
def safe_text_dupfile(f, mode, default_encoding="UTF8"): | ||
""" return an open text file object that's a duplicate of f on the | ||
FD-level if possible. | ||
""" | ||
encoding = getattr(f, "encoding", None) | ||
try: | ||
fd = f.fileno() | ||
except Exception: | ||
if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"): | ||
# we seem to have a text stream, let's just use it | ||
return f | ||
else: | ||
newfd = os.dup(fd) | ||
if "b" not in mode: | ||
mode += "b" | ||
f = os.fdopen(newfd, mode, 0) # no buffering | ||
return EncodedFile(f, encoding or default_encoding) | ||
|
||
|
||
class EncodedFile: | ||
errors = "strict" # possibly needed by py3 code (issue555) | ||
|
||
def __init__(self, buffer: BinaryIO, encoding: str) -> None: | ||
self.buffer = buffer | ||
self.encoding = encoding | ||
|
||
def write(self, s: str) -> int: | ||
if not isinstance(s, str): | ||
raise TypeError( | ||
"write() argument must be str, not {}".format(type(s).__name__) | ||
) | ||
return self.buffer.write(s.encode(self.encoding, "replace")) | ||
|
||
def writelines(self, lines: Iterable[str]) -> None: | ||
self.buffer.writelines(x.encode(self.encoding, "replace") for x in lines) | ||
|
||
@property | ||
def name(self) -> str: | ||
"""Ensure that file.name is a string.""" | ||
return repr(self.buffer) | ||
|
||
@property | ||
def mode(self) -> str: | ||
return self.buffer.mode.replace("b", "") | ||
|
||
def __getattr__(self, name): | ||
return getattr(object.__getattribute__(self, "buffer"), name) | ||
|
||
|
||
CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"]) | ||
|
||
|
||
|
@@ -548,9 +497,7 @@ def __init__(self, targetfd, tmpfile=None): | |
self.syscapture = SysCapture(targetfd) | ||
else: | ||
if tmpfile is None: | ||
f = TemporaryFile() | ||
with f: | ||
tmpfile = safe_text_dupfile(f, mode="wb+") | ||
tmpfile = TemporaryFile("wt+", encoding="utf-8", errors="replace") | ||
if targetfd in patchsysdict: | ||
self.syscapture = SysCapture(targetfd, tmpfile) | ||
else: | ||
|
@@ -579,7 +526,7 @@ def _start(self): | |
|
||
def snap(self): | ||
self.tmpfile.seek(0) | ||
res = self.tmpfile.read() | ||
res = self.tmpfile.buffer.read() | ||
self.tmpfile.seek(0) | ||
self.tmpfile.truncate() | ||
return res | ||
|
@@ -621,10 +568,10 @@ class FDCapture(FDCaptureBinary): | |
EMPTY_BUFFER = str() # type: ignore | ||
|
||
def snap(self): | ||
res = super().snap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
enc = getattr(self.tmpfile, "encoding", None) | ||
if enc and isinstance(res, bytes): | ||
res = str(res, enc, "replace") | ||
self.tmpfile.seek(0) | ||
res = self.tmpfile.read() | ||
self.tmpfile.seek(0) | ||
self.tmpfile.truncate() | ||
return res | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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? (withEncodedFile
being used)