summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2018-03-05 23:29:08 (GMT)
committerGitHub <noreply@github.com>2018-03-05 23:29:08 (GMT)
commitb9650a04a81355c8a7dcd0464c28febfb4bfc0a9 (patch)
tree300f17ad90d32a049c7b0ce53b434679dac86a06
parent6921e73e33edc3c61bc2d78ed558eaa22a89a564 (diff)
downloadcpython-b9650a04a81355c8a7dcd0464c28febfb4bfc0a9.zip
cpython-b9650a04a81355c8a7dcd0464c28febfb4bfc0a9.tar.gz
cpython-b9650a04a81355c8a7dcd0464c28febfb4bfc0a9.tar.bz2
bpo-32991: Restore expectation that inspect.getfile raises TypeError on namespace package (GH-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.
-rw-r--r--Lib/inspect.py4
-rw-r--r--Lib/test/test_doctest.py19
2 files changed, 20 insertions, 3 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 109efc0..57c0487 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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):
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 5ad94ab..f0eb528 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -7,6 +7,8 @@ import doctest
import functools
import os
import sys
+import importlib
+import unittest
# NOTE: There are some additional tests relating to interaction with
@@ -435,7 +437,7 @@ We'll simulate a __file__ attr that ends in pyc:
>>> 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.
@@ -681,6 +683,17 @@ Note here that 'bin', 'oct', and 'hex' are functions; 'float.as_integer_ratio',
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.
@@ -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,],