Handle zip & interactive cases in module helpers.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ron DuPlain · committed by Ron DuPlain · parent d164911 · Browse files · 1 file·+24 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−16 removed
- other:+24 added−16 removed
Provenance
- Commit
a3b30b7- Tree
5c74419- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
d164911
1 file changed+24 added−16 removed
modified:flask/helpers.py+24 added-16 removed
493 unmodified lines494 495 Not to be confused with the package path returned by :func:`find_package`.496 """497 loader = pkgutil.get_loader(import_name)498- if loader is None:498+ if loader is None or import_name == '__main__':499+ # import name is not found, or interactive/main module500 return os.getcwd()501+ # For .egg, zipimporter does not have get_filename until Python 2.7.502+ if hasattr(loader, 'get_filename'):503+ filepath = loader.get_filename(import_name)504+ else:505+ # Fall back to imports.506+ __import__(import_name)500- filepath = os.path.abspath(loader.get_filename(import_name))501- # filepath for import_name.py for a module, or __init__.py for a package.502- return os.path.dirname(filepath)507+ filepath = sys.modules[import_name].__file__508+ # filepath is import_name.py for a module, or __init__.py for a package.509+ return os.path.dirname(os.path.abspath(filepath))510 511 512 def find_package(import_name):513 """Finds a package and returns the prefix (or None if the package is4 unmodified lines518 folder structure exists (lib, share etc.).519 """520 root_mod_name = import_name.split('.')[0]521 loader = pkgutil.get_loader(root_mod_name)515- if loader is not None:516- filename = loader.get_filename(root_mod_name)522+ if loader is None or import_name == '__main__':523+ # import name is not found, or interactive/main module524+ package_path = os.getcwd()525+ else:526+ # For .egg, zipimporter does not have get_filename until Python 2.7.527+ if hasattr(loader, 'get_filename'):528+ filename = loader.get_filename(root_mod_name)529+ else:530+ # zipimporter's loader.archive points to the .egg or .zip531+ # archive filename is dropped in call to dirname below.532+ filename = loader.archive533 package_path = os.path.abspath(os.path.dirname(filename))534 # package_path ends with __init__.py for a package535 if loader.is_package(root_mod_name):536 package_path = os.path.dirname(package_path)521- else:522- # support for the interactive python shell523- package_path = os.getcwd()537 525- # leave the egg wrapper folder or the actual .egg on the filesystem526- test_package_path = package_path527- if os.path.basename(test_package_path).endswith('.egg'):528- test_package_path = os.path.dirname(test_package_path)529-530- site_parent, site_folder = os.path.split(test_package_path)538+ site_parent, site_folder = os.path.split(package_path)539 py_prefix = os.path.abspath(sys.prefix)532- if test_package_path.startswith(py_prefix):540+ if package_path.startswith(py_prefix):541 return py_prefix, package_path542 elif site_folder.lower() == 'site-packages':543 parent, folder = os.path.split(site_parent)544 # Windows like installations127 unmodified lines