MCPcopy
hub / github.com/pytest-dev/pytest / __init__

Method __init__

src/_pytest/capture.py:465–504  ·  view source on GitHub ↗
(self, targetfd: int)

Source from the content-addressed store, hash-verified

463
464class FDCaptureBase(CaptureBase[AnyStr]):
465 def __init__(self, targetfd: int) -> None:
466 self.targetfd = targetfd
467
468 try:
469 os.fstat(targetfd)
470 except OSError:
471 # FD capturing is conceptually simple -- create a temporary file,
472 # redirect the FD to it, redirect back when done. But when the
473 # target FD is invalid it throws a wrench into this lovely scheme.
474 #
475 # Tests themselves shouldn't care if the FD is valid, FD capturing
476 # should work regardless of external circumstances. So falling back
477 # to just sys capturing is not a good option.
478 #
479 # Further complications are the need to support suspend() and the
480 # possibility of FD reuse (e.g. the tmpfile getting the very same
481 # target FD). The following approach is robust, I believe.
482 self.targetfd_invalid: int | None = os.open(os.devnull, os.O_RDWR)
483 os.dup2(self.targetfd_invalid, targetfd)
484 else:
485 self.targetfd_invalid = None
486 self.targetfd_save = os.dup(targetfd)
487
488 if targetfd == 0:
489 self.tmpfile = open(os.devnull, encoding="utf-8")
490 self.syscapture: CaptureBase[str] = SysCapture(targetfd)
491 else:
492 self.tmpfile = EncodedFile(
493 TemporaryFile(buffering=0),
494 encoding="utf-8",
495 errors="replace",
496 newline="",
497 write_through=True,
498 )
499 if targetfd in patchsysdict:
500 self.syscapture = SysCapture(targetfd, self.tmpfile)
501 else:
502 self.syscapture = NoCapture(targetfd)
503
504 self._state = "initialized"
505
506 def __repr__(self) -> str:
507 return (

Callers

nothing calls this directly

Calls 4

SysCaptureClass · 0.85
EncodedFileClass · 0.85
NoCaptureClass · 0.85
openMethod · 0.80

Tested by

no test coverage detected