MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _module_relative_path

Function _module_relative_path

tools/python-3.11.9-amd64/Lib/doctest.py:391–423  ·  view source on GitHub ↗
(module, test_path)

Source from the content-addressed store, hash-verified

389
390# [XX] Normalize with respect to os.path.pardir?
391def _module_relative_path(module, test_path):
392 if not inspect.ismodule(module):
393 raise TypeError('Expected a module: %r' % module)
394 if test_path.startswith('/'):
395 raise ValueError('Module-relative files may not have absolute paths')
396
397 # Normalize the path. On Windows, replace "/" with "\".
398 test_path = os.path.join(*(test_path.split('/')))
399
400 # Find the base directory for the path.
401 if hasattr(module, '__file__'):
402 # A normal module/package
403 basedir = os.path.split(module.__file__)[0]
404 elif module.__name__ == '__main__':
405 # An interactive session.
406 if len(sys.argv)>0 and sys.argv[0] != '':
407 basedir = os.path.split(sys.argv[0])[0]
408 else:
409 basedir = os.curdir
410 else:
411 if hasattr(module, '__path__'):
412 for directory in module.__path__:
413 fullpath = os.path.join(directory, test_path)
414 if os.path.exists(fullpath):
415 return fullpath
416
417 # A module w/o __file__ (this includes builtins)
418 raise ValueError("Can't resolve paths relative to the module "
419 "%r (it has no __file__)"
420 % module.__name__)
421
422 # Combine the base directory and the test path.
423 return os.path.join(basedir, test_path)
424
425######################################################################
426## 2. Example & DocTest

Callers 1

_load_testfileFunction · 0.85

Calls 4

startswithMethod · 0.80
joinMethod · 0.45
splitMethod · 0.45
existsMethod · 0.45

Tested by

no test coverage detected