Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9650a0

Browse files
authored
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.
1 parent 6921e73 commit b9650a0

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/inspect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,13 @@ def cleandoc(doc):
642642
def getfile(object):
643643
"""Work out which source or compiled file an object was defined in."""
644644
if ismodule(object):
645-
if hasattr(object, '__file__'):
645+
if getattr(object, '__file__', None):
646646
return object.__file__
647647
raise TypeError('{!r} is a built-in module'.format(object))
648648
if isclass(object):
649649
if hasattr(object, '__module__'):
650650
object = sys.modules.get(object.__module__)
651-
if hasattr(object, '__file__'):
651+
if getattr(object, '__file__', None):
652652
return object.__file__
653653
raise TypeError('{!r} is a built-in class'.format(object))
654654
if ismethod(object):

Lib/test/test_doctest.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import functools
88
import os
99
import sys
10+
import importlib
11+
import unittest
1012

1113

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

686+
687+
class TestDocTestFinder(unittest.TestCase):
688+
689+
def test_empty_namespace_package(self):
690+
pkg_name = 'doctest_empty_pkg'
691+
os.mkdir(pkg_name)
692+
mod = importlib.import_module(pkg_name)
693+
assert doctest.DocTestFinder().find(mod) == []
694+
os.rmdir(pkg_name)
695+
696+
684697
def test_DocTestParser(): r"""
685698
Unit tests for the `DocTestParser` class.
686699
@@ -2945,6 +2958,10 @@ def test_main():
29452958
from test import test_doctest
29462959
support.run_doctest(test_doctest, verbosity=True)
29472960

2961+
# Run unittests
2962+
support.run_unittest(__name__)
2963+
2964+
29482965
def test_coverage(coverdir):
29492966
trace = support.import_module('trace')
29502967
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],

0 commit comments

Comments
 (0)