Skip to content
Myr

Explain is_package AttributeError in find_package

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

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.

authored by Daniel Neuhäuser · committed by Daniel Neuhäuser · parent f606a6d · Browse files · 3 files+23 added−2 removed
This commit

State

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

Diff stats

3 files+23 added−2 removed

  • other:+23 added−2 removed

3 files+23 added−2 removed

modified:flask/testsuite/config.py+13 added
14 unmodified lines
15import pkgutil16import unittest17from contextlib import contextmanager18from flask.testsuite import FlaskTestCase19from flask._compat import PY2202122# config keys used for the ConfigTestCase23TEST_KEY = 'foo'
267 unmodified lines
291 sys.path.remove(egg_path)292 if 'site_egg' in sys.modules:293 del sys.modules['site_egg']294295 if PY2:296 def test_meta_path_loader_without_is_package(self):297 class Loader(object):298 def find_module(self, name):299 return self300 sys.meta_path.append(Loader())301 try:302 with self.assert_raises(AttributeError):303 flask.Flask(__name__)304 finally:305 sys.meta_path.pop()306307308def suite():309 suite = unittest.TestSuite()310 suite.addTest(unittest.makeSuite(ConfigTestCase))311 suite.addTest(unittest.makeSuite(InstanceTestCase))
1 unmodified line
modified:flask/helpers.py+7 added-2 removed
677 unmodified lines
678 __import__(import_name)679 filename = sys.modules[import_name].__file__680 package_path = os.path.abspath(os.path.dirname(filename))681 # package_path ends with __init__.py for a package682 if hasattr(loader, 'is_package'):682 if loader.is_package(root_mod_name):683 package_path = os.path.dirname(package_path)683 if loader.is_package(root_mod_name):684 package_path = os.path.dirname(package_path)685 else:686 raise AttributeError(687 ('%s.is_package() method is missing but is '688 'required by Flask of PEP 302 import hooks') % loader.__class__.__name__)689690 site_parent, site_folder = os.path.split(package_path)691 py_prefix = os.path.abspath(sys.prefix)692 if package_path.startswith(py_prefix):
162 unmodified lines
modified:CHANGES+3 added
7 unmodified lines
89(bugfix release, release date to be announced)1011- Fixed broken `test_appcontext_signals()` test case.12- Raise an :exc:`AttributeError` in :func:`flask.helpers.find_package` with a13 useful message explaining why it is raised when a PEP 302 import hook is used14 without an `is_package()` method.1516Version 0.10.117--------------18
457 unmodified lines