Skip to content

Commit 3b30c93

Browse files
committed
Deprecate TerminalReporter._tw
Fix #2803
1 parent de0d19c commit 3b30c93

File tree

7 files changed

+71
-56
lines changed

7 files changed

+71
-56
lines changed

_pytest/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ def warn(self, code, message, fslocation=None, nodeid=None):
931931
fslocation=fslocation, nodeid=nodeid))
932932

933933
def get_terminal_writer(self):
934-
return self.pluginmanager.get_plugin("terminalreporter")._tw
934+
return self.pluginmanager.get_plugin("terminalreporter").writer
935935

936936
def pytest_cmdline_parse(self, pluginmanager, args):
937937
# REF1 assert self == pluginmanager.config, (self, pluginmanager.config)

_pytest/debugging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _enter_pdb(node, excinfo, rep):
8383
# XXX we re-use the TerminalReporter's terminalwriter
8484
# because this seems to avoid some encoding related troubles
8585
# for not completely clear reasons.
86-
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
86+
tw = node.config.pluginmanager.getplugin("terminalreporter").writer
8787
tw.line()
8888
tw.sep(">", "traceback")
8989
rep.toterminal(tw)

_pytest/helpconfig.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def pytest_cmdline_main(config):
107107

108108
def showhelp(config):
109109
reporter = config.pluginmanager.get_plugin('terminalreporter')
110-
tw = reporter._tw
110+
tw = reporter.writer
111111
tw.write(config._parser.optparser.format_help())
112112
tw.line()
113113
tw.line()

_pytest/pastebin.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def pytest_configure(config):
2525
if tr is not None:
2626
# pastebin file will be utf-8 encoded binary file
2727
config._pastebinfile = tempfile.TemporaryFile('w+b')
28-
oldwrite = tr._tw.write
28+
oldwrite = tr.writer.write
2929

3030
def tee_write(s, **kwargs):
3131
oldwrite(s, **kwargs)
3232
if isinstance(s, six.text_type):
3333
s = s.encode('utf-8')
3434
config._pastebinfile.write(s)
3535

36-
tr._tw.write = tee_write
36+
tr.writer.write = tee_write
3737

3838

3939
def pytest_unconfigure(config):
@@ -45,7 +45,7 @@ def pytest_unconfigure(config):
4545
del config._pastebinfile
4646
# undo our patching in the terminal reporter
4747
tr = config.pluginmanager.getplugin('terminalreporter')
48-
del tr._tw.__dict__['write']
48+
del tr.writer.__dict__['write']
4949
# write summary
5050
tr.write_sep("=", "Sending information to Paste Service")
5151
pastebinurl = create_new_paste(sessionlog)

_pytest/skipping.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ def pytest_terminal_summary(terminalreporter):
295295
show_simple(terminalreporter, lines, 'passed', "PASSED %s")
296296

297297
if lines:
298-
tr._tw.sep("=", "short test summary info")
298+
tr.writer.sep("=", "short test summary info")
299299
for line in lines:
300-
tr._tw.line(line)
300+
tr.writer.line(line)
301301

302302

303303
def show_simple(terminalreporter, lines, stat, format):

_pytest/terminal.py

+62-48
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
from __future__ import absolute_import, division, print_function
66

77
import itertools
8-
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
9-
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
10-
import pytest
11-
import py
12-
import six
8+
import platform
139
import sys
1410
import time
15-
import platform
11+
import warnings
12+
13+
import py
14+
import six
15+
1616
import pluggy
17+
import pytest
18+
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
19+
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
1720

1821

1922
def pytest_addoption(parser):
@@ -136,13 +139,22 @@ def __init__(self, config, file=None):
136139
self.startdir = py.path.local()
137140
if file is None:
138141
file = sys.stdout
139-
self._tw = self.writer = _pytest.config.create_terminal_writer(config,
140-
file)
142+
self._writer = _pytest.config.create_terminal_writer(config, file)
141143
self.currentfspath = None
142144
self.reportchars = getreportopt(config)
143-
self.hasmarkup = self._tw.hasmarkup
145+
self.hasmarkup = self.writer.hasmarkup
144146
self.isatty = file.isatty()
145147

148+
@property
149+
def writer(self):
150+
return self._writer
151+
152+
@property
153+
def _tw(self):
154+
warnings.warn(DeprecationWarning('TerminalReporter._tw is deprecated, use TerminalReporter.writer instead'),
155+
stacklevel=2)
156+
return self.writer
157+
146158
def hasopt(self, char):
147159
char = {'xfailed': 'x', 'skipped': 's'}.get(char, char)
148160
return char in self.reportchars
@@ -152,32 +164,32 @@ def write_fspath_result(self, nodeid, res):
152164
if fspath != self.currentfspath:
153165
self.currentfspath = fspath
154166
fspath = self.startdir.bestrelpath(fspath)
155-
self._tw.line()
156-
self._tw.write(fspath + " ")
157-
self._tw.write(res)
167+
self.writer.line()
168+
self.writer.write(fspath + " ")
169+
self.writer.write(res)
158170

159171
def write_ensure_prefix(self, prefix, extra="", **kwargs):
160172
if self.currentfspath != prefix:
161-
self._tw.line()
173+
self.writer.line()
162174
self.currentfspath = prefix
163-
self._tw.write(prefix)
175+
self.writer.write(prefix)
164176
if extra:
165-
self._tw.write(extra, **kwargs)
177+
self.writer.write(extra, **kwargs)
166178
self.currentfspath = -2
167179

168180
def ensure_newline(self):
169181
if self.currentfspath:
170-
self._tw.line()
182+
self.writer.line()
171183
self.currentfspath = None
172184

173185
def write(self, content, **markup):
174-
self._tw.write(content, **markup)
186+
self.writer.write(content, **markup)
175187

176188
def write_line(self, line, **markup):
177189
if not isinstance(line, six.text_type):
178190
line = six.text_type(line, errors="replace")
179191
self.ensure_newline()
180-
self._tw.line(line, **markup)
192+
self.writer.line(line, **markup)
181193

182194
def rewrite(self, line, **markup):
183195
"""
@@ -190,22 +202,22 @@ def rewrite(self, line, **markup):
190202
"""
191203
erase = markup.pop('erase', False)
192204
if erase:
193-
fill_count = self._tw.fullwidth - len(line)
205+
fill_count = self.writer.fullwidth - len(line)
194206
fill = ' ' * fill_count
195207
else:
196208
fill = ''
197209
line = str(line)
198-
self._tw.write("\r" + line + fill, **markup)
210+
self.writer.write("\r" + line + fill, **markup)
199211

200212
def write_sep(self, sep, title=None, **markup):
201213
self.ensure_newline()
202-
self._tw.sep(sep, title, **markup)
214+
self.writer.sep(sep, title, **markup)
203215

204216
def section(self, title, sep="=", **kw):
205-
self._tw.sep(sep, title, **kw)
217+
self.writer.sep(sep, title, **kw)
206218

207219
def line(self, msg, **kw):
208-
self._tw.line(msg, **kw)
220+
self.writer.line(msg, **kw)
209221

210222
def pytest_internalerror(self, excrepr):
211223
for line in six.text_type(excrepr).split("\n"):
@@ -252,7 +264,7 @@ def pytest_runtest_logreport(self, report):
252264
if not hasattr(rep, 'node') and self.showfspath:
253265
self.write_fspath_result(rep.nodeid, letter)
254266
else:
255-
self._tw.write(letter)
267+
self.writer.write(letter)
256268
else:
257269
if isinstance(word, tuple):
258270
word, markup = word
@@ -263,16 +275,18 @@ def pytest_runtest_logreport(self, report):
263275
markup = {'red': True}
264276
elif rep.skipped:
265277
markup = {'yellow': True}
278+
else:
279+
markup = {}
266280
line = self._locationline(rep.nodeid, *rep.location)
267281
if not hasattr(rep, 'node'):
268282
self.write_ensure_prefix(line, word, **markup)
269-
# self._tw.write(word, **markup)
283+
# self.writer.write(word, **markup)
270284
else:
271285
self.ensure_newline()
272286
if hasattr(rep, 'node'):
273-
self._tw.write("[%s] " % rep.node.gateway.id)
274-
self._tw.write(word, **markup)
275-
self._tw.write(" " + line)
287+
self.writer.write("[%s] " % rep.node.gateway.id)
288+
self.writer.write(word, **markup)
289+
self.writer.write(" " + line)
276290
self.currentfspath = -2
277291

278292
def pytest_collection(self):
@@ -358,9 +372,9 @@ def pytest_collection_finish(self, session):
358372
if self.config.option.collectonly:
359373
self._printcollecteditems(session.items)
360374
if self.stats.get('failed'):
361-
self._tw.sep("!", "collection failures")
375+
self.writer.sep("!", "collection failures")
362376
for rep in self.stats.get('failed'):
363-
rep.toterminal(self._tw)
377+
rep.toterminal(self.writer)
364378
return 1
365379
return 0
366380
lines = self.config.hook.pytest_report_collectionfinish(
@@ -378,12 +392,12 @@ def _printcollecteditems(self, items):
378392
name = item.nodeid.split('::', 1)[0]
379393
counts[name] = counts.get(name, 0) + 1
380394
for name, count in sorted(counts.items()):
381-
self._tw.line("%s: %d" % (name, count))
395+
self.writer.line("%s: %d" % (name, count))
382396
else:
383397
for item in items:
384398
nodeid = item.nodeid
385399
nodeid = nodeid.replace("::()::", "::")
386-
self._tw.line(nodeid)
400+
self.writer.line(nodeid)
387401
return
388402
stack = []
389403
indent = ""
@@ -398,13 +412,13 @@ def _printcollecteditems(self, items):
398412
# if col.name == "()":
399413
# continue
400414
indent = (len(stack) - 1) * " "
401-
self._tw.line("%s%s" % (indent, col))
415+
self.writer.line("%s%s" % (indent, col))
402416

403417
@pytest.hookimpl(hookwrapper=True)
404418
def pytest_sessionfinish(self, exitstatus):
405419
outcome = yield
406420
outcome.get_result()
407-
self._tw.line("")
421+
self.writer.line("")
408422
summary_exit_codes = (
409423
EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, EXIT_USAGEERROR,
410424
EXIT_NOTESTSCOLLECTED)
@@ -434,10 +448,10 @@ def _report_keyboardinterrupt(self):
434448
self.write_sep("!", msg)
435449
if "KeyboardInterrupt" in msg:
436450
if self.config.option.fulltrace:
437-
excrepr.toterminal(self._tw)
451+
excrepr.toterminal(self.writer)
438452
else:
439-
self._tw.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True)
440-
excrepr.reprcrash.toterminal(self._tw)
453+
self.writer.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True)
454+
excrepr.reprcrash.toterminal(self.writer)
441455

442456
def _locationline(self, nodeid, fspath, lineno, domain):
443457
def mkrel(nodeid):
@@ -493,14 +507,14 @@ def summary_warnings(self):
493507
grouped = itertools.groupby(all_warnings, key=lambda wr: wr.get_location(self.config))
494508

495509
self.write_sep("=", "warnings summary", yellow=True, bold=False)
496-
for location, warnings in grouped:
497-
self._tw.line(str(location) or '<undetermined location>')
498-
for w in warnings:
510+
for location, warning_records in grouped:
511+
self.writer.line(str(location) or '<undetermined location>')
512+
for w in warning_records:
499513
lines = w.message.splitlines()
500514
indented = '\n'.join(' ' + x for x in lines)
501-
self._tw.line(indented)
502-
self._tw.line()
503-
self._tw.line('-- Docs: http://doc.pytest.org/en/latest/warnings.html')
515+
self.writer.line(indented)
516+
self.writer.line()
517+
self.writer.line('-- Docs: http://doc.pytest.org/en/latest/warnings.html')
504518

505519
def summary_passes(self):
506520
if self.config.option.tbstyle != "no":
@@ -517,10 +531,10 @@ def summary_passes(self):
517531
def print_teardown_sections(self, rep):
518532
for secname, content in rep.sections:
519533
if 'teardown' in secname:
520-
self._tw.sep('-', secname)
534+
self.writer.sep('-', secname)
521535
if content[-1:] == "\n":
522536
content = content[:-1]
523-
self._tw.line(content)
537+
self.writer.line(content)
524538

525539
def summary_failures(self):
526540
if self.config.option.tbstyle != "no":
@@ -560,12 +574,12 @@ def summary_errors(self):
560574
self._outrep_summary(rep)
561575

562576
def _outrep_summary(self, rep):
563-
rep.toterminal(self._tw)
577+
rep.toterminal(self.writer)
564578
for secname, content in rep.sections:
565-
self._tw.sep("-", secname)
579+
self.writer.sep("-", secname)
566580
if content[-1:] == "\n":
567581
content = content[:-1]
568-
self._tw.line(content)
582+
self.writer.line(content)
569583

570584
def summary_stats(self):
571585
session_duration = time.time() - self._sessionstarttime

changelog/2803.removal

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``TerminalReporter._tw`` has been deprecated in favor of ``TerminalReporter.writer`` and will be removed in a future version. Also, ``TerminalReporter.writer`` is now read-only.

0 commit comments

Comments
 (0)