Skip to content

Commit e8ef67a

Browse files
committed
Refactor devnull file handling into a function and reuse it
1 parent 9fcbf91 commit e8ef67a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

_pytest/capture.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ def snap(self):
380380
f.seek(0)
381381
res = f.read()
382382
except (OSError, ValueError):
383-
self.tmpfile = open(os.devnull, "r")
384-
self.tmpfile_fd = self.tmpfile.fileno()
383+
self._open_tmpfile_as_devnull()
385384
res = ''
386385
if res:
387386
enc = getattr(f, "encoding", None)
@@ -410,10 +409,13 @@ def resume(self):
410409
try:
411410
os.dup2(self.tmpfile_fd, self.targetfd)
412411
except (OSError, ValueError):
413-
self.tmpfile = open(os.devnull, "r")
414-
self.tmpfile_fd = self.tmpfile.fileno()
412+
self._open_tmpfile_as_devnull()
415413
self.targetfd = open(os.devnull, "r").fileno()
416414

415+
def _open_tmpfile_as_devnull(self):
416+
self.tmpfile = open(os.devnull, "r")
417+
self.tmpfile_fd = self.tmpfile.fileno()
418+
417419
def writeorg(self, data):
418420
""" write to original file descriptor. """
419421
if py.builtin._istext(data):

0 commit comments

Comments
 (0)