Skip to content

Commit 811f837

Browse files
committed
Added comments and docstrings in accordance to feedback from @nicoddemus.
1 parent 11b04e0 commit 811f837

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

_pytest/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,13 @@ def _locate_module(self, modulename, searchpath):
654654
In case of a presumptive namespace package return all of its possible
655655
locations.
656656
657+
Note: The only reliable way to determine whether a package is a
658+
namespace package, i.e., whether its ``__path__`` has more than one
659+
element, is to import it. This method does not do that and hence we
660+
are talking of a *presumptive* namespace package. The ``_parsearg``
661+
method is aware of this and, quite conservatively, tends to raise an
662+
exception in case of doubt.
663+
657664
"""
658665
try:
659666
fd, pathname, type_ = imp.find_module(modulename, searchpath)
@@ -666,6 +673,10 @@ def _locate_module(self, modulename, searchpath):
666673
return [pathname]
667674
else:
668675
init_file = os.path.join(pathname, '__init__.py')
676+
# The following check is a little heuristic do determine whether a
677+
# package is a namespace package. If its '__init__.py' is empty
678+
# then it should be treated as a regular package (see #1568 for
679+
# further discussion):
669680
if os.path.getsize(init_file) == 0:
670681
return [pathname]
671682
return [pathname] + self._locate_module(

testing/acceptance_test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,10 @@ def join_pythonpath(what):
559559
])
560560

561561
def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
562-
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False)
562+
"""
563+
test --pyargs option with namespace packages (#1567)
564+
"""
565+
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', raising=False)
563566

564567
search_path = []
565568
for dirname in "hello", "world":
@@ -599,16 +602,21 @@ def join_pythonpath(*dirs):
599602
monkeypatch.syspath_prepend(p)
600603

601604
# mixed module and filenames:
602-
result = testdir.runpytest("--pyargs", "ns_pkg.hello", "world/ns_pkg")
605+
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "world/ns_pkg")
603606
assert result.ret == 0
604607
result.stdout.fnmatch_lines([
608+
"*test_hello.py::test_hello*PASSED",
609+
"*test_hello.py::test_other*PASSED",
610+
"*test_world.py::test_world*PASSED",
611+
"*test_world.py::test_other*PASSED",
605612
"*4 passed*"
606613
])
607614

608615
# specify tests within a module
609-
result = testdir.runpytest("--pyargs", "ns_pkg.world.test_world::test_other")
616+
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.world.test_world::test_other")
610617
assert result.ret == 0
611618
result.stdout.fnmatch_lines([
619+
"*test_world.py::test_other*PASSED",
612620
"*1 passed*"
613621
])
614622

0 commit comments

Comments
 (0)