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

Skip to content

Commit b1511f7

Browse files
committed
doctest now supports packages
Issue #26641: doctest.DocFileTest and doctest.testfile() now support packages (module splitted into multiple directories) for the package parameter.
1 parent 6e722bc commit b1511f7

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

Lib/doctest.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,15 @@ def trace_dispatch(self, *args):
381381
sys.stdout = save_stdout
382382

383383
# [XX] Normalize with respect to os.path.pardir?
384-
def _module_relative_path(module, path):
384+
def _module_relative_path(module, test_path):
385385
if not inspect.ismodule(module):
386386
raise TypeError('Expected a module: %r' % module)
387-
if path.startswith('/'):
387+
if test_path.startswith('/'):
388388
raise ValueError('Module-relative files may not have absolute paths')
389389

390+
# Normalize the path. On Windows, replace "/" with "\".
391+
test_path = os.path.join(*(test_path.split('/')))
392+
390393
# Find the base directory for the path.
391394
if hasattr(module, '__file__'):
392395
# A normal module/package
@@ -398,13 +401,19 @@ def _module_relative_path(module, path):
398401
else:
399402
basedir = os.curdir
400403
else:
404+
if hasattr(module, '__path__'):
405+
for directory in module.__path__:
406+
fullpath = os.path.join(directory, test_path)
407+
if os.path.exists(fullpath):
408+
return fullpath
409+
401410
# A module w/o __file__ (this includes builtins)
402411
raise ValueError("Can't resolve paths relative to the module "
403412
"%r (it has no __file__)"
404413
% module.__name__)
405414

406-
# Combine the base directory and the path.
407-
return os.path.join(basedir, *(path.split('/')))
415+
# Combine the base directory and the test path.
416+
return os.path.join(basedir, test_path)
408417

409418
######################################################################
410419
## 2. Example & DocTest

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ Core and Builtins
232232
Library
233233
-------
234234

235+
- Issue #26641: doctest.DocFileTest and doctest.testfile() now support
236+
packages (module splitted into multiple directories) for the package
237+
parameter.
238+
235239
- Issue #25195: Fix a regression in mock.MagicMock. _Call is a subclass of
236240
tuple (changeset 3603bae63c13 only works for classes) so we need to
237241
implement __ne__ ourselves. Patch by Andrew Plummer.

0 commit comments

Comments
 (0)