mirror of https://github.com/pallets/flask.git
Explain is_package AttributeError in find_package
When a PEP 302 import hook is used that doesn't implement .is_package() an AttributeError is raised. This looks like a bug in Flask. This change fixes that problem in the sense that it explains, that the AttributeError is intentional.
This commit is contained in:
parent
f606a6d6a1
commit
94f4360137
3
CHANGES
3
CHANGES
|
|
@ -9,6 +9,9 @@ Version 0.10.2
|
||||||
(bugfix release, release date to be announced)
|
(bugfix release, release date to be announced)
|
||||||
|
|
||||||
- Fixed broken `test_appcontext_signals()` test case.
|
- Fixed broken `test_appcontext_signals()` test case.
|
||||||
|
- Raise an :exc:`AttributeError` in :func:`flask.helpers.find_package` with a
|
||||||
|
useful message explaining why it is raised when a PEP 302 import hook is used
|
||||||
|
without an `is_package()` method.
|
||||||
|
|
||||||
Version 0.10.1
|
Version 0.10.1
|
||||||
--------------
|
--------------
|
||||||
|
|
|
||||||
|
|
@ -679,8 +679,13 @@ def find_package(import_name):
|
||||||
filename = sys.modules[import_name].__file__
|
filename = sys.modules[import_name].__file__
|
||||||
package_path = os.path.abspath(os.path.dirname(filename))
|
package_path = os.path.abspath(os.path.dirname(filename))
|
||||||
# package_path ends with __init__.py for a package
|
# package_path ends with __init__.py for a package
|
||||||
|
if hasattr(loader, 'is_package'):
|
||||||
if loader.is_package(root_mod_name):
|
if loader.is_package(root_mod_name):
|
||||||
package_path = os.path.dirname(package_path)
|
package_path = os.path.dirname(package_path)
|
||||||
|
else:
|
||||||
|
raise AttributeError(
|
||||||
|
('%s.is_package() method is missing but is '
|
||||||
|
'required by Flask of PEP 302 import hooks') % loader.__class__.__name__)
|
||||||
|
|
||||||
site_parent, site_folder = os.path.split(package_path)
|
site_parent, site_folder = os.path.split(package_path)
|
||||||
py_prefix = os.path.abspath(sys.prefix)
|
py_prefix = os.path.abspath(sys.prefix)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import pkgutil
|
||||||
import unittest
|
import unittest
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from flask.testsuite import FlaskTestCase
|
from flask.testsuite import FlaskTestCase
|
||||||
|
from flask._compat import PY2
|
||||||
|
|
||||||
|
|
||||||
# config keys used for the ConfigTestCase
|
# config keys used for the ConfigTestCase
|
||||||
|
|
@ -291,6 +292,18 @@ class InstanceTestCase(FlaskTestCase):
|
||||||
if 'site_egg' in sys.modules:
|
if 'site_egg' in sys.modules:
|
||||||
del sys.modules['site_egg']
|
del sys.modules['site_egg']
|
||||||
|
|
||||||
|
if PY2:
|
||||||
|
def test_meta_path_loader_without_is_package(self):
|
||||||
|
class Loader(object):
|
||||||
|
def find_module(self, name):
|
||||||
|
return self
|
||||||
|
sys.meta_path.append(Loader())
|
||||||
|
try:
|
||||||
|
with self.assert_raises(AttributeError):
|
||||||
|
flask.Flask(__name__)
|
||||||
|
finally:
|
||||||
|
sys.meta_path.pop()
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue