summaryrefslogtreecommitdiff
path: root/src/_pytest/_io
diff options
context:
space:
mode:
authorRan Benita <ran@unusedvar.com>2020-04-29 16:04:57 +0300
committerRan Benita <ran@unusedvar.com>2020-04-30 16:44:02 +0300
commit94a57d235322588ccde609bd9c2c384f0e81a337 (patch)
treeebb816f91e2b2db67de96ce67734377c8e14de97 /src/_pytest/_io
parentdac05ccd9a290ac232d933dfb2c4b0f651468867 (diff)
downloadpytest-94a57d235322588ccde609bd9c2c384f0e81a337.tar.gz
io: combine _io.TerminalWriter and _io.terminalwriter.TerminalWriter
Previously it extended an external type but now it come move to the type itself.
Diffstat (limited to 'src/_pytest/_io')
-rw-r--r--src/_pytest/_io/__init__.py41
-rw-r--r--src/_pytest/_io/terminalwriter.py34
2 files changed, 38 insertions, 37 deletions
diff --git a/src/_pytest/_io/__init__.py b/src/_pytest/_io/__init__.py
index d401cda8e..880c3c87a 100644
--- a/src/_pytest/_io/__init__.py
+++ b/src/_pytest/_io/__init__.py
@@ -1,39 +1,6 @@
-from typing import List
-from typing import Sequence
+from .terminalwriter import TerminalWriter
-from .terminalwriter import TerminalWriter as BaseTerminalWriter
-
-class TerminalWriter(BaseTerminalWriter):
- def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None:
- """Write lines of source code possibly highlighted.
-
- Keeping this private for now because the API is clunky. We should discuss how
- to evolve the terminal writer so we can have more precise color support, for example
- being able to write part of a line in one color and the rest in another, and so on.
- """
- if indents and len(indents) != len(lines):
- raise ValueError(
- "indents size ({}) should have same size as lines ({})".format(
- len(indents), len(lines)
- )
- )
- if not indents:
- indents = [""] * len(lines)
- source = "\n".join(lines)
- new_lines = self._highlight(source).splitlines()
- for indent, new_line in zip(indents, new_lines):
- self.line(indent + new_line)
-
- def _highlight(self, source):
- """Highlight the given source code if we have markup support"""
- if not self.hasmarkup:
- return source
- try:
- from pygments.formatters.terminal import TerminalFormatter
- from pygments.lexers.python import PythonLexer
- from pygments import highlight
- except ImportError:
- return source
- else:
- return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
+__all__ = [
+ "TerminalWriter",
+]
diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py
index 437c96794..e4e5db228 100644
--- a/src/_pytest/_io/terminalwriter.py
+++ b/src/_pytest/_io/terminalwriter.py
@@ -5,6 +5,7 @@ import sys
import unicodedata
from functools import lru_cache
from io import StringIO
+from typing import Sequence
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
@@ -191,6 +192,39 @@ class TerminalWriter:
self.write(s, **kw)
self.write("\n")
+ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> None:
+ """Write lines of source code possibly highlighted.
+
+ Keeping this private for now because the API is clunky. We should discuss how
+ to evolve the terminal writer so we can have more precise color support, for example
+ being able to write part of a line in one color and the rest in another, and so on.
+ """
+ if indents and len(indents) != len(lines):
+ raise ValueError(
+ "indents size ({}) should have same size as lines ({})".format(
+ len(indents), len(lines)
+ )
+ )
+ if not indents:
+ indents = [""] * len(lines)
+ source = "\n".join(lines)
+ new_lines = self._highlight(source).splitlines()
+ for indent, new_line in zip(indents, new_lines):
+ self.line(indent + new_line)
+
+ def _highlight(self, source):
+ """Highlight the given source code if we have markup support"""
+ if not self.hasmarkup:
+ return source
+ try:
+ from pygments.formatters.terminal import TerminalFormatter
+ from pygments.lexers.python import PythonLexer
+ from pygments import highlight
+ except ImportError:
+ return source
+ else:
+ return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
+
if win32_and_ctypes:
import ctypes # noqa: F811