Skip to content

Commit ac633b8

Browse files
authored
Replace py.io.TextIO with io.StringIO (#6064)
Replace py.io.TextIO with io.StringIO
2 parents 2fc1f7b + 0b8c355 commit ac633b8

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

src/_pytest/_code/code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import traceback
55
from inspect import CO_VARARGS
66
from inspect import CO_VARKEYWORDS
7+
from io import StringIO
78
from traceback import format_exception_only
89
from types import CodeType
910
from types import TracebackType
@@ -865,7 +866,7 @@ class TerminalRepr:
865866
def __str__(self):
866867
# FYI this is called from pytest-xdist's serialization of exception
867868
# information.
868-
io = py.io.TextIO()
869+
io = StringIO()
869870
tw = py.io.TerminalWriter(file=io)
870871
self.toterminal(tw)
871872
return io.getvalue().strip()

src/_pytest/logging.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import logging
33
import re
44
from contextlib import contextmanager
5-
6-
import py
5+
from io import StringIO
76

87
import pytest
98
from _pytest.compat import nullcontext
@@ -218,7 +217,7 @@ class LogCaptureHandler(logging.StreamHandler):
218217

219218
def __init__(self):
220219
"""Creates a new log handler."""
221-
logging.StreamHandler.__init__(self, py.io.TextIO())
220+
logging.StreamHandler.__init__(self, StringIO())
222221
self.records = []
223222

224223
def emit(self, record):
@@ -228,7 +227,7 @@ def emit(self, record):
228227

229228
def reset(self):
230229
self.records = []
231-
self.stream = py.io.TextIO()
230+
self.stream = StringIO()
232231

233232

234233
class LogCaptureFixture:

src/_pytest/pytester.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import traceback
1111
from collections.abc import Sequence
1212
from fnmatch import fnmatch
13+
from io import StringIO
1314
from weakref import WeakKeyDictionary
1415

1516
import py
@@ -1218,7 +1219,7 @@ def getdecoded(out):
12181219

12191220
class LineComp:
12201221
def __init__(self):
1221-
self.stringio = py.io.TextIO()
1222+
self.stringio = StringIO()
12221223

12231224
def assert_contains_lines(self, lines2):
12241225
"""Assert that lines2 are contained (linearly) in lines1.

src/_pytest/reports.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import StringIO
12
from pprint import pprint
23
from typing import Optional
34
from typing import Union
@@ -180,7 +181,7 @@ def _from_json(cls, reportdict):
180181

181182
def _report_unserialization_failure(type_name, report_class, reportdict):
182183
url = "https://github.com/pytest-dev/pytest/issues"
183-
stream = py.io.TextIO()
184+
stream = StringIO()
184185
pprint("-" * 100, stream=stream)
185186
pprint("INTERNALERROR: Unknown entry type returned: %s" % type_name, stream=stream)
186187
pprint("report_name: %s" % report_class, stream=stream)

testing/test_capture.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import subprocess
66
import sys
77
import textwrap
8+
from io import StringIO
89
from io import UnsupportedOperation
910

10-
import py
11-
1211
import pytest
1312
from _pytest import capture
1413
from _pytest.capture import CaptureManager
@@ -892,10 +891,10 @@ def test_dupfile_on_bytesio():
892891

893892

894893
def test_dupfile_on_textio():
895-
tio = py.io.TextIO()
896-
f = capture.safe_text_dupfile(tio, "wb")
894+
sio = StringIO()
895+
f = capture.safe_text_dupfile(sio, "wb")
897896
f.write("hello")
898-
assert tio.getvalue() == "hello"
897+
assert sio.getvalue() == "hello"
899898
assert not hasattr(f, "name")
900899

901900

testing/test_resultlog.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
2-
3-
import py
2+
from io import StringIO
43

54
import _pytest._code
65
import pytest
@@ -13,15 +12,15 @@
1312

1413
def test_write_log_entry():
1514
reslog = ResultLog(None, None)
16-
reslog.logfile = py.io.TextIO()
15+
reslog.logfile = StringIO()
1716
reslog.write_log_entry("name", ".", "")
1817
entry = reslog.logfile.getvalue()
1918
assert entry[-1] == "\n"
2019
entry_lines = entry.splitlines()
2120
assert len(entry_lines) == 1
2221
assert entry_lines[0] == ". name"
2322

24-
reslog.logfile = py.io.TextIO()
23+
reslog.logfile = StringIO()
2524
reslog.write_log_entry("name", "s", "Skipped")
2625
entry = reslog.logfile.getvalue()
2726
assert entry[-1] == "\n"
@@ -30,7 +29,7 @@ def test_write_log_entry():
3029
assert entry_lines[0] == "s name"
3130
assert entry_lines[1] == " Skipped"
3231

33-
reslog.logfile = py.io.TextIO()
32+
reslog.logfile = StringIO()
3433
reslog.write_log_entry("name", "s", "Skipped\n")
3534
entry = reslog.logfile.getvalue()
3635
assert entry[-1] == "\n"
@@ -39,7 +38,7 @@ def test_write_log_entry():
3938
assert entry_lines[0] == "s name"
4039
assert entry_lines[1] == " Skipped"
4140

42-
reslog.logfile = py.io.TextIO()
41+
reslog.logfile = StringIO()
4342
longrepr = " tb1\n tb 2\nE tb3\nSome Error"
4443
reslog.write_log_entry("name", "F", longrepr)
4544
entry = reslog.logfile.getvalue()
@@ -118,7 +117,7 @@ def test_internal_exception(self, style):
118117
raise ValueError
119118
except ValueError:
120119
excinfo = _pytest._code.ExceptionInfo.from_current()
121-
reslog = ResultLog(None, py.io.TextIO())
120+
reslog = ResultLog(None, StringIO())
122121
reslog.pytest_internalerror(excinfo.getrepr(style=style))
123122
entry = reslog.logfile.getvalue()
124123
entry_lines = entry.splitlines()

testing/test_terminal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import textwrap
8+
from io import StringIO
89

910
import pluggy
1011
import py
@@ -268,7 +269,7 @@ def test_foobar():
268269

269270
def test_rewrite(self, testdir, monkeypatch):
270271
config = testdir.parseconfig()
271-
f = py.io.TextIO()
272+
f = StringIO()
272273
monkeypatch.setattr(f, "isatty", lambda *args: True)
273274
tr = TerminalReporter(config, f)
274275
tr._tw.fullwidth = 10

0 commit comments

Comments
 (0)