Skip to content

Commit

Permalink
bpo-32991: Restore expectation that inspect.getfile raises TypeError …
Browse files Browse the repository at this point in the history
…on namespace package (pythonGH-5980)

* bpo-32991: Add test capturing expectation.

DocTestFinder.find should return an empty list for doctests in a namespace package.

* bpo-32991: Restore expectation that inspect.getfile on a namespace package raises TypeError.
  • Loading branch information
jaraco authored Mar 5, 2018
1 parent 6921e73 commit b9650a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,13 @@ def cleandoc(doc):
def getfile(object):
"""Work out which source or compiled file an object was defined in."""
if ismodule(object):
if hasattr(object, '__file__'):
if getattr(object, '__file__', None):
return object.__file__
raise TypeError('{!r} is a built-in module'.format(object))
if isclass(object):
if hasattr(object, '__module__'):
object = sys.modules.get(object.__module__)
if hasattr(object, '__file__'):
if getattr(object, '__file__', None):
return object.__file__
raise TypeError('{!r} is a built-in class'.format(object))
if ismethod(object):
Expand Down
19 changes: 18 additions & 1 deletion Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import functools
import os
import sys
import importlib
import unittest


# NOTE: There are some additional tests relating to interaction with
Expand Down Expand Up @@ -435,7 +437,7 @@ def basics(): r"""
>>> tests = finder.find(sample_func)
>>> print(tests) # doctest: +ELLIPSIS
[<DocTest sample_func from ...:19 (1 example)>]
[<DocTest sample_func from ...:21 (1 example)>]
The exact name depends on how test_doctest was invoked, so allow for
leading path components.
Expand Down Expand Up @@ -681,6 +683,17 @@ def non_Python_modules(): r"""
and 'int' is a type.
"""


class TestDocTestFinder(unittest.TestCase):

def test_empty_namespace_package(self):
pkg_name = 'doctest_empty_pkg'
os.mkdir(pkg_name)
mod = importlib.import_module(pkg_name)
assert doctest.DocTestFinder().find(mod) == []
os.rmdir(pkg_name)


def test_DocTestParser(): r"""
Unit tests for the `DocTestParser` class.
Expand Down Expand Up @@ -2945,6 +2958,10 @@ def test_main():
from test import test_doctest
support.run_doctest(test_doctest, verbosity=True)

# Run unittests
support.run_unittest(__name__)


def test_coverage(coverdir):
trace = support.import_module('trace')
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
Expand Down

0 comments on commit b9650a0

Please sign in to comment.