Skip to content

capture: recover from closed files #2633

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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

patchsysdict = {0: 'stdin', 1: 'stdout', 2: 'stderr'}


def pytest_addoption(parser):
group = parser.getgroup("general")
group._addoption(
Expand Down Expand Up @@ -211,7 +210,8 @@ def readouterr(self):

@contextlib.contextmanager
def disabled(self):
capmanager = self.request.config.pluginmanager.getplugin('capturemanager')
capmanager = self.request.config.pluginmanager.getplugin(
'capturemanager')
capmanager.suspendcapture_item(self.request.node, "call", in_=True)
try:
yield
Expand Down Expand Up @@ -248,7 +248,10 @@ def __init__(self, buffer, encoding):
def write(self, obj):
if isinstance(obj, unicode):
obj = obj.encode(self.encoding, "replace")
self.buffer.write(obj)
try:
self.buffer.write(obj)
except OSError:
self.buffer = open(os.devnull, "wb+")

def writelines(self, linelist):
data = ''.join(linelist)
Expand Down Expand Up @@ -371,9 +374,14 @@ def start(self):
self.syscapture.start()

def snap(self):
f = self.tmpfile
f.seek(0)
res = f.read()
try:
f = self.tmpfile
f.seek(0)
res = f.read()
except OSError:
self.tmpfile = open(os.devnull, "r")
self.tmpfile_fd = self.tmpfile.fileno()
res = ''
if res:
enc = getattr(f, "encoding", None)
if enc and isinstance(res, bytes):
Expand All @@ -388,17 +396,23 @@ def done(self):
seeked to position zero. """
targetfd_save = self.__dict__.pop("targetfd_save")
os.dup2(targetfd_save, self.targetfd)
os.close(targetfd_save)
# os.close(targetfd_save)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this code commented out?

self.syscapture.done()
self.tmpfile.close()
# self.tmpfile.close()
raise Exception("Done method called!")

def suspend(self):
self.syscapture.suspend()
os.dup2(self.targetfd_save, self.targetfd)

def resume(self):
self.syscapture.resume()
os.dup2(self.tmpfile_fd, self.targetfd)
try:
os.dup2(self.tmpfile_fd, self.targetfd)
except OSError:
self.tmpfile = open(os.devnull, "r")
self.tmpfile_fd = self.tmpfile.fileno()
self.targetfd = open(os.devnull, "r").fileno()

def writeorg(self, data):
""" write to original file descriptor. """
Expand Down Expand Up @@ -432,7 +446,8 @@ def snap(self):
def done(self):
setattr(sys, self.name, self._old)
del self._old
self.tmpfile.close()
# self.tmpfile.close()
raise Exception("Sys method called!")

def suspend(self):
setattr(sys, self.name, self._old)
Expand Down
2 changes: 2 additions & 0 deletions changelog/2633.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In some cases on windows stderr capturing may fail. Pytest now
attempts to recover from a capturing failure.
3 changes: 3 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,3 +1159,6 @@ def test_pickling_and_unpickling_enocded_file():
ef = capture.EncodedFile(None, None)
ef_as_str = pickle.dumps(ef)
pickle.loads(ef_as_str)

def test_closed_handle():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you overwrote my update to this test, was that deliberate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus I am sorry about this; I thought that this had already been merged so I could trash the branch.

sys.stderr.close()