capture: improve NoCapture typing
This commit is contained in:
parent
54911acf8d
commit
54b8b40f83
|
@ -306,9 +306,29 @@ class CaptureBase(abc.ABC, Generic[AnyStr]):
|
||||||
patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"}
|
patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"}
|
||||||
|
|
||||||
|
|
||||||
class NoCapture:
|
class NoCapture(CaptureBase[str]):
|
||||||
EMPTY_BUFFER = None
|
EMPTY_BUFFER = ""
|
||||||
__init__ = start = done = suspend = resume = lambda *args: None
|
|
||||||
|
def __init__(self, fd: int) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def start(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def done(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def suspend(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def resume(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def snap(self) -> str:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def writeorg(self, data: str) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SysCaptureBase(CaptureBase[AnyStr]):
|
class SysCaptureBase(CaptureBase[AnyStr]):
|
||||||
|
@ -439,7 +459,7 @@ class FDCaptureBase(CaptureBase[AnyStr]):
|
||||||
|
|
||||||
if targetfd == 0:
|
if targetfd == 0:
|
||||||
self.tmpfile = open(os.devnull, encoding="utf-8")
|
self.tmpfile = open(os.devnull, encoding="utf-8")
|
||||||
self.syscapture = SysCapture(targetfd)
|
self.syscapture: CaptureBase[str] = SysCapture(targetfd)
|
||||||
else:
|
else:
|
||||||
self.tmpfile = EncodedFile(
|
self.tmpfile = EncodedFile(
|
||||||
TemporaryFile(buffering=0),
|
TemporaryFile(buffering=0),
|
||||||
|
@ -451,7 +471,7 @@ class FDCaptureBase(CaptureBase[AnyStr]):
|
||||||
if targetfd in patchsysdict:
|
if targetfd in patchsysdict:
|
||||||
self.syscapture = SysCapture(targetfd, self.tmpfile)
|
self.syscapture = SysCapture(targetfd, self.tmpfile)
|
||||||
else:
|
else:
|
||||||
self.syscapture = NoCapture()
|
self.syscapture = NoCapture(targetfd)
|
||||||
|
|
||||||
self._state = "initialized"
|
self._state = "initialized"
|
||||||
|
|
||||||
|
|
|
@ -1052,6 +1052,7 @@ class TestFDCapture:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# Should not crash with missing "_old".
|
# Should not crash with missing "_old".
|
||||||
|
assert isinstance(cap.syscapture, capture.SysCapture)
|
||||||
assert repr(cap.syscapture) == (
|
assert repr(cap.syscapture) == (
|
||||||
"<SysCapture stdout _old=<UNSET> _state='done' tmpfile={!r}>".format(
|
"<SysCapture stdout _old=<UNSET> _state='done' tmpfile={!r}>".format(
|
||||||
cap.syscapture.tmpfile
|
cap.syscapture.tmpfile
|
||||||
|
|
Loading…
Reference in New Issue