Skip to content

Commit fc27171

Browse files
authored
Merge pull request #7189 from asottile/backport-7186
2 parents c53d52c + d18e426 commit fc27171

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

changelog/7180.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``_is_setup_py`` for files encoded differently than locale.

src/_pytest/doctest.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ def pytest_unconfigure():
108108
RUNNER_CLASS = None
109109

110110

111-
def pytest_collect_file(path, parent):
111+
def pytest_collect_file(path: py.path.local, parent):
112112
config = parent.config
113113
if path.ext == ".py":
114-
if config.option.doctestmodules and not _is_setup_py(config, path, parent):
114+
if config.option.doctestmodules and not _is_setup_py(path):
115115
return DoctestModule.from_parent(parent, fspath=path)
116116
elif _is_doctest(config, path, parent):
117117
return DoctestTextfile.from_parent(parent, fspath=path)
118118

119119

120-
def _is_setup_py(config, path, parent):
120+
def _is_setup_py(path: py.path.local) -> bool:
121121
if path.basename != "setup.py":
122122
return False
123-
contents = path.read()
124-
return "setuptools" in contents or "distutils" in contents
123+
contents = path.read_binary()
124+
return b"setuptools" in contents or b"distutils" in contents
125125

126126

127127
def _is_doctest(config, path, parent):

testing/test_doctest.py

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from _pytest.compat import MODULE_NOT_FOUND_ERROR
66
from _pytest.doctest import _get_checker
77
from _pytest.doctest import _is_mocked
8+
from _pytest.doctest import _is_setup_py
89
from _pytest.doctest import _patch_unwrap_mock_aware
910
from _pytest.doctest import DoctestItem
1011
from _pytest.doctest import DoctestModule
@@ -1487,3 +1488,27 @@ def test_warning_on_unwrap_of_broken_object(stop):
14871488
with pytest.raises(KeyError):
14881489
inspect.unwrap(bad_instance, stop=stop)
14891490
assert inspect.unwrap.__module__ == "inspect"
1491+
1492+
1493+
def test_is_setup_py_not_named_setup_py(tmpdir):
1494+
not_setup_py = tmpdir.join("not_setup.py")
1495+
not_setup_py.write('from setuptools import setup; setup(name="foo")')
1496+
assert not _is_setup_py(not_setup_py)
1497+
1498+
1499+
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core"))
1500+
def test_is_setup_py_is_a_setup_py(tmpdir, mod):
1501+
setup_py = tmpdir.join("setup.py")
1502+
setup_py.write('from {} import setup; setup(name="foo")'.format(mod))
1503+
assert _is_setup_py(setup_py)
1504+
1505+
1506+
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core"))
1507+
def test_is_setup_py_different_encoding(tmpdir, mod):
1508+
setup_py = tmpdir.join("setup.py")
1509+
contents = (
1510+
"# -*- coding: cp1252 -*-\n"
1511+
'from {} import setup; setup(name="foo", description="€")\n'.format(mod)
1512+
)
1513+
setup_py.write_binary(contents.encode("cp1252"))
1514+
assert _is_setup_py(setup_py)

0 commit comments

Comments
 (0)