(module, test_path)
| 389 | |
| 390 | # [XX] Normalize with respect to os.path.pardir? |
| 391 | def _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 |
no test coverage detected