Instance paths are now moved into virtualenv/share/appname-instance if installed
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 0520425 · Browse files · 3 files·+93 added−9 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+93 added−9 removed
- tests:+56 added−1 removed
- other:+37 added−8 removed
Provenance
- Commit
175d43b- Tree
5fffa7a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0520425
3 files changed+93 added−9 removed
modified:flask/app.py+35 added-6 removed
445 unmodified lines446 self.error_handler_spec[None] = value447 error_handlers = property(_get_error_handlers, _set_error_handlers)448 del _get_error_handlers, _set_error_handlers449 450+ @locked_cached_property451+ def name(self):452+ """The name of the application. This is usually the import name453+ with the difference that it's guessed from the run file if the454+ import name is main. This name is used as a display name when455+ Flask needs the name of the application. It can be set and overriden456+ to change the value.457+458+ .. versionadded:: 0.8459+ """460+ if self.import_name == '__main__':461+ fn = getattr(sys.modules['__main__'], '__file__', None)462+ if fn is None:463+ return 'unknown'464+ return os.path.splitext(os.path.basename(fn))[0]465+ return self.import_name466+467 @property468 def propagate_exceptions(self):469 """Returns the value of the `PROPAGATE_EXCEPTIONS` configuration470 value in case it's set, otherwise a sensible default is returned.90 unmodified lines561 562 .. versionadded:: 0.8563 """564 root_mod = sys.modules[self.import_name.split('.')[0]]548- instance_path = None565+ # we're not using root_mod.__file__ here since the module could be566+ # virtual. We're trusting the _PackageBoundObject to have calculated567+ # the proper name.568+ package_path = self.root_path569 if hasattr(root_mod, '__path__'):550- package_dir = os.path.dirname(root_mod.__file__)551- instance_path = os.path.join(package_dir, os.path.pardir)570+ package_path = os.path.dirname(package_path)571+ site_parent, site_folder = os.path.split(package_path)572+573+ py_prefix = os.path.abspath(sys.prefix)574+ if package_path.startswith(py_prefix):575+ base_dir = py_prefix576+ elif site_folder == 'site-packages':577+ parent, folder = os.path.split(site_parent)578+ if folder.lower() == 'lib':579+ base_dir = parent580+ else:581+ base_dir = site_parent582 else:553- instance_path = os.path.dirname(root_mod.__file__)554- basedir = os.path.normpath(os.path.abspath(instance_path))555- return os.path.join(basedir, 'instance')583+ return os.path.join(package_path, 'instance')584+ return os.path.join(base_dir, 'share', self.name + '-instance')585 586 def open_instance_resource(self, resource, mode='rb'):587 """Opens a resource from the application's instance folder588 (:attr:`instance_path`). Otherwise works like964 unmodified linesmodified:flask/helpers.py+2 added-2 removed
464 unmodified lines465 raise NotFound()466 return send_file(filename, conditional=True, **options)467 468 469-def _get_package_path(name):469+def get_package_path(name):470 """Returns the path to a package or cwd if that cannot be found."""471 try:472 return os.path.abspath(os.path.dirname(sys.modules[name].__file__))473 except (KeyError, AttributeError):37 unmodified lines511 #: exposed.512 self.template_folder = template_folder513 514 #: Where is the app root located?515- self.root_path = _get_package_path(self.import_name)515+ self.root_path = get_package_path(self.import_name)516 517 self._static_folder = None518 self._static_url_path = None519 73 unmodified linesmodified:tests/flask_tests.py+56 added-1 removed
1003 unmodified lines1004 with app.test_client() as c:1005 rv = c.post('/foo', data={}, follow_redirects=True)1006 self.assertEqual(rv.data, 'success')1007 1008+1009+class InstanceTestCase(unittest.TestCase):1010+1008- def test_basic_instance_paths(self):1011+ def test_uninstalled_module_paths(self):1012 here = os.path.abspath(os.path.dirname(__file__))1013 app = flask.Flask(__name__)1014 self.assertEqual(app.instance_path, os.path.join(here, 'instance'))1015 6 unmodified lines1022 self.assert_('must be absolute' in str(e))1023 else:1024 self.fail('Expected value error')1025 1026+ def test_uninstalled_package_paths(self):1027 from blueprintapp import app1028+ here = os.path.abspath(os.path.dirname(__file__))1029 self.assertEqual(app.instance_path, os.path.join(here, 'instance'))1030 1031+ def test_installed_module_paths(self):1032+ import types1033+ expected_prefix = os.path.abspath('foo')1034+ mod = types.ModuleType('myapp')1035+ mod.__file__ = os.path.join(expected_prefix, 'lib',1036+ 'site-packages', 'myapp.py')1037+ sys.modules['myapp'] = mod1038+ try:1039+ mod.app = flask.Flask(mod.__name__)1040+ self.assertEqual(mod.app.instance_path,1041+ os.path.join(expected_prefix, 'share',1042+ 'myapp-instance'))1043+ finally:1044+ sys.modules['myapp'] = None1045 1046+ def test_installed_package_paths(self):1047+ import types1048+ expected_prefix = os.path.abspath('foo')1049+ package_path = os.path.join(expected_prefix, 'lib',1050+ 'site-packages', 'myapp')1051+ mod = types.ModuleType('myapp')1052+ mod.__path__ = [package_path]1053+ mod.__file__ = os.path.join(package_path, '__init__.py')1054+ sys.modules['myapp'] = mod1055+ try:1056+ mod.app = flask.Flask(mod.__name__)1057+ self.assertEqual(mod.app.instance_path,1058+ os.path.join(expected_prefix, 'share',1059+ 'myapp-instance'))1060+ finally:1061+ sys.modules['myapp'] = None1062+1063+ def test_prefix_installed_paths(self):1064+ import types1065+ expected_prefix = os.path.abspath(sys.prefix)1066+ package_path = os.path.join(expected_prefix, 'lib',1067+ 'site-packages', 'myapp')1068+ mod = types.ModuleType('myapp')1069+ mod.__path__ = [package_path]1070+ mod.__file__ = os.path.join(package_path, '__init__.py')1071+ sys.modules['myapp'] = mod1072+ try:1073+ mod.app = flask.Flask(mod.__name__)1074+ self.assertEqual(mod.app.instance_path,1075+ os.path.join(expected_prefix, 'share',1076+ 'myapp-instance'))1077+ finally:1078+ sys.modules['myapp'] = None1079+1080+1081 class JSONTestCase(unittest.TestCase):1082 1083 def test_json_bad_requests(self):1084 app = flask.Flask(__name__)1082 unmodified lines2167 suite.addTest(unittest.makeSuite(ConfigTestCase))2168 suite.addTest(unittest.makeSuite(SubdomainTestCase))2169 suite.addTest(unittest.makeSuite(ViewTestCase))2170 suite.addTest(unittest.makeSuite(DeprecationsTestCase))2171+ suite.addTest(unittest.makeSuite(InstanceTestCase))2172 if flask.json_available:2173 suite.addTest(unittest.makeSuite(JSONTestCase))2174 if flask.signals_available:2175 suite.addTest(unittest.makeSuite(TestSignals))7 unmodified lines