Added workaround for Python 3.3's frozen loader
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 696bffc · Browse files · 2 files·+31 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−8 removed
- other:+31 added−8 removed
Provenance
- Commit
9554844- Tree
7ed6902- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
696bffc
2 files changed+31 added−8 removed
modified:flask/helpers.py+30 added-8 removed
654 unmodified lines655 # filepath is import_name.py for a module, or __init__.py for a package.656 return os.path.dirname(os.path.abspath(filepath))657 658 659+def _matching_loader_thinks_module_is_package(loader, mod_name):660+ """Given the loader that loaded a module and the module this function661+ attempts to figure out if the given module is actually a package.662+ """663+ # If the loader can tell us if something is a package, we can664+ # directly ask the loader.665+ if hasattr(loader, 'is_package'):666+ return loader.is_package(mod_name)667+ # importlib's namespace loaders do not have this functionality but668+ # all the modules it loads are packages, so we can take advantage of669+ # this information.670+ elif (loader.__module__ == '_frozen_importlib' and671+ loader.__name__ == 'NamespaceLoader'):672+ return True673+ # Otherwise we need to fail with an error that explains what went674+ # wrong.675+ raise AttributeError(676+ ('%s.is_package() method is missing but is required by Flask of '677+ 'PEP 302 import hooks. If you do not use import hooks and '678+ 'you encounter this error please file a bug against Flask.') %679+ loader.__class__.__name__)680+681+682 def find_package(import_name):683 """Finds a package and returns the prefix (or None if the package is684 not installed) as well as the folder that contains the package or685 module as a tuple. The package path returned is the module that would21 unmodified lines707 # Fall back to imports.708 __import__(import_name)709 filename = sys.modules[import_name].__file__710 package_path = os.path.abspath(os.path.dirname(filename))688- # package_path ends with __init__.py for a package689- if hasattr(loader, 'is_package'):690- if loader.is_package(root_mod_name):691- package_path = os.path.dirname(package_path)692- else:693- raise AttributeError(694- ('%s.is_package() method is missing but is '695- 'required by Flask of PEP 302 import hooks') % loader.__class__.__name__)711 712+ # In case the root module is a pcakage we need to chop of the713+ # rightmost part. This needs to go through a helper function714+ # because of python 3.3 namespace packages.715+ if _matching_loader_thinks_module_is_package(716+ loader, root_mod_name):717+ package_path = os.path.dirname(package_path)718+719 site_parent, site_folder = os.path.split(package_path)720 py_prefix = os.path.abspath(sys.prefix)721 if package_path.startswith(py_prefix):722 return py_prefix, package_path161 unmodified linesmodified:CHANGES+1 added
21 unmodified lines22 - Added :meth:`flask.config.Config.get_namespace`.23 - Added ``TEMPLATES_AUTO_RELOAD`` config key. If disabled the 24 templates will be reloaded only if the application is running in 25 debug mode. For higher performance it’s possible to disable that.26+- Added a workaround for a limitation in Python 3.3's namespace loader.27 28 Version 0.10.229 --------------30 478 unmodified lines