diff options
| author | Michael Merickel <github@m.merickel.org> | 2024-02-08 20:32:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-08 20:32:10 -0700 |
| commit | 5f7e286b0629b0a5f1225fe51172cba77eb8fda1 (patch) | |
| tree | 09b34f3f939b4cea4d059c28309e095fa83b8cae | |
| parent | 0441e47ae10f8e2006fd56860c90e96de30f6dec (diff) | |
| parent | b2e6f361d2d1a21b9a0cdc63f12e290ece72bc4c (diff) | |
| download | pyramid-5f7e286b0629b0a5f1225fe51172cba77eb8fda1.tar.gz pyramid-5f7e286b0629b0a5f1225fe51172cba77eb8fda1.tar.bz2 pyramid-5f7e286b0629b0a5f1225fe51172cba77eb8fda1.zip | |
Merge pull request #3752 from Pylons/warn-invalid-package
issue a warning if a static view is referencing a package that doesn't exist
| -rw-r--r-- | CHANGES.rst | 11 | ||||
| -rw-r--r-- | src/pyramid/static.py | 14 |
2 files changed, 25 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index e712449ca..30005c96f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -61,6 +61,17 @@ Backward Incompatibilities These have been deprecated in Python's gettext module since 3.8, and removed in Python 3.11. +Deprecations +------------ + +- Deprecated the ability to use a non-existent package with + ``pyramid.config.Configurator.add_static_view`` and + ``pyramid.static.static_view``. This can be fixed by choosing a path + located within a real package as the ``root_dir`` for your static files. + This is almost always either a misconfig or an attempt to define an alias + location for use with ``pyramid.config.Configurator.override_asset``. + See https://github.com/Pylons/pyramid/pull/3752 + Documentation Changes --------------------- diff --git a/src/pyramid/static.py b/src/pyramid/static.py index 100d17676..4091bbfbc 100644 --- a/src/pyramid/static.py +++ b/src/pyramid/static.py @@ -4,6 +4,7 @@ import mimetypes import os from os.path import exists, getmtime, getsize, isdir, join, normcase, normpath from pkg_resources import resource_exists, resource_filename, resource_isdir +import warnings from pyramid.asset import abspath_from_asset_spec, resolve_asset_spec from pyramid.httpexceptions import HTTPMovedPermanently, HTTPNotFound @@ -92,6 +93,19 @@ class static_view: if package_name is None: package_name = caller_package().__name__ package_name, docroot = resolve_asset_spec(root_dir, package_name) + if package_name: + try: + __import__(package_name) + except ImportError: + warnings.warn( + f'A "pyramid.static.static_view" is being created with an' + f' asset spec referencing a package "{package_name}" that' + f' does not exist. This will break in the future.' + f' If this is done to override an asset, you must adjust' + f' this to override a location inside a real package.', + DeprecationWarning, + stacklevel=2, + ) self.use_subpath = use_subpath self.package_name = package_name self.docroot = docroot |
