Browse Source

Update module helpers to avoid Python imports.

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__.
pull/379/head
Ron DuPlain 13 years ago
parent
commit
d16491145d
  1. 31
      flask/helpers.py

31
flask/helpers.py

@ -11,8 +11,10 @@
from __future__ import with_statement from __future__ import with_statement
import imp
import os import os
import sys import sys
import pkgutil
import posixpath import posixpath
import mimetypes import mimetypes
from time import time from time import time
@ -492,14 +494,12 @@ def get_root_path(import_name):
Not to be confused with the package path returned by :func:`find_package`. Not to be confused with the package path returned by :func:`find_package`.
""" """
__import__(import_name) loader = pkgutil.get_loader(import_name)
try: if loader is None:
directory = os.path.dirname(sys.modules[import_name].__file__)
return os.path.abspath(directory)
except AttributeError:
# this is necessary in case we are running from the interactive
# python shell. It will never be used for production code however
return os.getcwd() return os.getcwd()
filepath = os.path.abspath(loader.get_filename(import_name))
# filepath for import_name.py for a module, or __init__.py for a package.
return os.path.dirname(filepath)
def find_package(import_name): def find_package(import_name):
@ -510,16 +510,17 @@ def find_package(import_name):
import the module. The prefix is the path below which a UNIX like import the module. The prefix is the path below which a UNIX like
folder structure exists (lib, share etc.). folder structure exists (lib, share etc.).
""" """
__import__(import_name) root_mod_name = import_name.split('.')[0]
root_mod = sys.modules[import_name.split('.')[0]] loader = pkgutil.get_loader(root_mod_name)
package_path = getattr(root_mod, '__file__', None) if loader is not None:
if package_path is None: filename = loader.get_filename(root_mod_name)
package_path = os.path.abspath(os.path.dirname(filename))
# package_path ends with __init__.py for a package
if loader.is_package(root_mod_name):
package_path = os.path.dirname(package_path)
else:
# support for the interactive python shell # support for the interactive python shell
package_path = os.getcwd() package_path = os.getcwd()
else:
package_path = os.path.abspath(os.path.dirname(package_path))
if hasattr(root_mod, '__path__'):
package_path = os.path.dirname(package_path)
# leave the egg wrapper folder or the actual .egg on the filesystem # leave the egg wrapper folder or the actual .egg on the filesystem
test_package_path = package_path test_package_path = package_path

Loading…
Cancel
Save