Skip to content
Myr

Update module helpers to avoid Python imports.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This avoids errors in creating Flask instances where there are import errors in the module or package matching the import name. Those runtime errors will be apparent to the user soon enough, but tools which build Flask instances meta-programmatically benefit from a Flask which does not __import__.

authored by Ron DuPlain · committed by Ron DuPlain · parent 26a9c20 · Browse files · 1 file+16 added−15 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+16 added−15 removed

  • other:+16 added−15 removed

1 file+16 added−15 removed

modified:flask/helpers.py+16 added-15 removed
9 unmodified lines
10"""1112from __future__ import with_statement1314import imp15import os16import sys17import pkgutil18import posixpath19import mimetypes20from time import time21from zlib import adler32
471 unmodified lines
493 returns the path of a package or the folder that contains a module.494495 Not to be confused with the package path returned by :func:`find_package`.496 """495 __import__(import_name)496 try:497 directory = os.path.dirname(sys.modules[import_name].__file__)498 return os.path.abspath(directory)499 except AttributeError:500 # this is necessary in case we are running from the interactive501 # python shell. It will never be used for production code however497 loader = pkgutil.get_loader(import_name)498 if loader is None:499 return os.getcwd()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)503504505def find_package(import_name):506 """Finds a package and returns the prefix (or None if the package is
2 unmodified lines
509 have to be added to the pythonpath in order to make it possible to510 import the module. The prefix is the path below which a UNIX like511 folder structure exists (lib, share etc.).512 """513 __import__(import_name)514 root_mod = sys.modules[import_name.split('.')[0]]515 package_path = getattr(root_mod, '__file__', None)516 if package_path is None:513 root_mod_name = import_name.split('.')[0]514 loader = pkgutil.get_loader(root_mod_name)515 if loader is not None:516 filename = loader.get_filename(root_mod_name)517 package_path = os.path.abspath(os.path.dirname(filename))518 # package_path ends with __init__.py for a package519 if loader.is_package(root_mod_name):520 package_path = os.path.dirname(package_path)521 else:522 # support for the interactive python shell523 package_path = os.getcwd()519 else:520 package_path = os.path.abspath(os.path.dirname(package_path))521 if hasattr(root_mod, '__path__'):522 package_path = os.path.dirname(package_path)524525 # 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'):
136 unmodified lines