Skip to content

Commit c2e860f

Browse files
committed
Use pytest's vendored TerminalWriter and pathlib. Replace old use of pyimport. Ref #226.
1 parent 7a2b924 commit c2e860f

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def read(*names, **kwargs):
6969
install_requires=[
7070
'pytest>=3.8',
7171
'py-cpuinfo',
72-
'py>=1.8.2',
7372
],
7473
extras_require={
7574
'aspect': ['aspectlib'],

src/pytest_benchmark/cli.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import argparse
22
from functools import partial
33

4-
import py
4+
from _pytest import pathlib
5+
from _pytest._io import TerminalWriter
56

67
from pytest_benchmark.csv import CSVResults
78

@@ -118,13 +119,15 @@ def make_parser():
118119

119120

120121
class HookDispatch(object):
121-
def __init__(self):
122-
conftest_file = py.path.local('conftest.py')
123-
if conftest_file.check():
124-
self.conftest = conftest_file.pyimport()
122+
def __init__(self, **kwargs):
123+
conftest_file = pathlib.Path('conftest.py')
124+
if conftest_file.exists():
125+
self.conftest = pathlib.import_path(conftest_file, **kwargs)
125126
else:
126127
self.conftest = None
127128

129+
print(f'>>>>>>>>>> {self.conftest}')
130+
128131
def __getattr__(self, item):
129132
default = getattr(plugin, item)
130133
return getattr(self.conftest, item, default)
@@ -139,7 +142,7 @@ def main():
139142
logger = Logger(level)
140143
storage = load_storage(args.storage, logger=logger, netrc=args.netrc)
141144

142-
hook = HookDispatch()
145+
hook = HookDispatch(mode=args.importmode, root=pathlib.Path('.'))
143146

144147
if args.command == 'list':
145148
for file in storage.query():
@@ -172,7 +175,7 @@ def main():
172175

173176
class TerminalReporter(object):
174177
def __init__(self):
175-
self._tw = py.io.TerminalWriter()
178+
self._tw = TerminalWriter()
176179

177180
def ensure_newline(self):
178181
pass
@@ -181,8 +184,8 @@ def write(self, content, **markup):
181184
self._tw.write(content, **markup)
182185

183186
def write_line(self, line, **markup):
184-
if not py.builtin._istext(line):
185-
line = py.builtin.text(line, errors="replace")
187+
if not isinstance(line, str):
188+
line = line.decode(errors="replace")
186189
self._tw.line(line, **markup)
187190

188191
def rewrite(self, line, **markup):

src/pytest_benchmark/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import warnings
66

7-
import py
7+
from _pytest._io import TerminalWriter
88
from pytest import PytestWarning
99

1010

@@ -17,7 +17,7 @@ class Logger(object):
1717

1818
def __init__(self, level=NORMAL, config=None):
1919
self.level = level
20-
self.term = py.io.TerminalWriter(file=sys.stderr)
20+
self.term = TerminalWriter(file=sys.stderr)
2121
self.suspend_capture = None
2222
self.resume_capture = None
2323
if config:

src/pytest_benchmark/plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def add_global_options(addoption, prefix="benchmark-"):
126126
action="store_true", default=False,
127127
help="Disable reporting. Verbose mode takes precedence."
128128
)
129+
if not prefix:
130+
addoption(
131+
"--import-mode",
132+
default="prepend",
133+
choices=["prepend", "append", "importlib"],
134+
dest="importmode",
135+
help="How to attempt loading hooks from conftest. Akin to pytest's --import-mode. Default: %(default)r.",
136+
)
129137

130138

131139
def pytest_addoption(parser):

0 commit comments

Comments
 (0)