Fixes for PEP451 import loaders and pytest 5.x
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- pytest 5.x drops python2 compatibility and therefore only implements PEP 451 - pytest 5.x made the repr of `ExcInfo` less confusing (fixed tests depending on the old format)
authored by Anthony Sottile · committed by David Lord · parent b9c2267 · Browse files · 6 files·+57 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+57 added−25 removed
- tests:+9 added−9 removed
- other:+48 added−16 removed
Provenance
- Commit
a5ecdfa- Tree
766b129- Author
Anthony Sottile <asottile@umich.edu>- Committer
David Lord <davidism@gmail.com>- Parent
b9c2267
6 files changed+57 added−25 removed
modified:docs/tutorial/tests.rst+1 added-1 removed
186 unmodified lines187 188 with pytest.raises(sqlite3.ProgrammingError) as e:189 db.execute('SELECT 1')190 191- assert 'closed' in str(e)191+ assert 'closed' in str(e.value)192 193 The ``init-db`` command should call the ``init_db`` function and output194 a message.195 366 unmodified linesmodified:examples/tutorial/tests/test_db.py+1 added-1 removed
10 unmodified lines11 12 with pytest.raises(sqlite3.ProgrammingError) as e:13 db.execute('SELECT 1')14 15- assert 'closed' in str(e)15+ assert 'closed' in str(e.value)16 17 18 def test_init_db_command(runner, monkeypatch):19 class Recorder(object):9 unmodified linesmodified:flask/helpers.py+46 added-15 removed
787 unmodified lines788 'you encounter this error please file a bug against Flask.') %789 loader.__class__.__name__)790 791 792-def find_package(import_name):793- """Finds a package and returns the prefix (or None if the package is794- not installed) as well as the folder that contains the package or795- module as a tuple. The package path returned is the module that would796- have to be added to the pythonpath in order to make it possible to797- import the module. The prefix is the path below which a UNIX like798- folder structure exists (lib, share etc.).799- """800- root_mod_name = import_name.split('.')[0]792+def _find_package_path(root_mod_name):793+ """Find the path where the module's root exists in"""794+ if sys.version_info >= (3, 4):795+ import importlib.util796+797+ try:798+ spec = importlib.util.find_spec(root_mod_name)799+ if spec is None:800+ raise ValueError("not found")801+ # ImportError: the machinery told us it does not exist802+ # ValueError:803+ # - the module name was invalid804+ # - the module name is __main__805+ # - *we* raised `ValueError` due to `spec` being `None`806+ except (ImportError, ValueError):807+ pass # handled below808+ else:809+ # namespace package810+ if spec.origin in {"namespace", None}:811+ return os.path.dirname(next(iter(spec.submodule_search_locations)))812+ # a package (with __init__.py)813+ elif spec.submodule_search_locations:814+ return os.path.dirname(os.path.dirname(spec.origin))815+ # just a normal module816+ else:817+ return os.path.dirname(spec.origin)818+819+ # we were unable to find the `package_path` using PEP 451 loaders820 loader = pkgutil.get_loader(root_mod_name)802- if loader is None or import_name == '__main__':821+ if loader is None or root_mod_name == '__main__':822 # import name is not found, or interactive/main module804- package_path = os.getcwd()823+ return os.getcwd()824 else:825 # For .egg, zipimporter does not have get_filename until Python 2.7.826 if hasattr(loader, 'get_filename'):827 filename = loader.get_filename(root_mod_name)5 unmodified lines833 # At least one loader is missing both get_filename and archive:834 # Google App Engine's HardenedModulesHook835 #836 # Fall back to imports.818- __import__(import_name)819- filename = sys.modules[import_name].__file__837+ __import__(root_mod_name)838+ filename = sys.modules[root_mod_name].__file__839 package_path = os.path.abspath(os.path.dirname(filename))840 841 # In case the root module is a package we need to chop of the842 # rightmost part. This needs to go through a helper function843 # because of python 3.3 namespace packages.825- if _matching_loader_thinks_module_is_package(826- loader, root_mod_name):844+ if _matching_loader_thinks_module_is_package(loader, root_mod_name):845 package_path = os.path.dirname(package_path)846 847+ return package_path848+849+850+def find_package(import_name):851+ """Finds a package and returns the prefix (or None if the package is852+ not installed) as well as the folder that contains the package or853+ module as a tuple. The package path returned is the module that would854+ have to be added to the pythonpath in order to make it possible to855+ import the module. The prefix is the path below which a UNIX like856+ folder structure exists (lib, share etc.).857+ """858+ root_mod_name, _, _ = import_name.partition('.')859+ package_path = _find_package_path(root_mod_name)860 site_parent, site_folder = os.path.split(package_path)861 py_prefix = os.path.abspath(sys.prefix)862 if package_path.startswith(py_prefix):863 return py_prefix, package_path219 unmodified linesmodified:tests/test_basic.py+6 added-6 removed
1217 unmodified lines1218 c = app.test_client()1219 1220 with pytest.raises(TypeError) as e:1221 c.get('/none')1222- assert 'returned None' in str(e)1222+ assert 'returned None' in str(e.value)1223 1224 with pytest.raises(TypeError) as e:1225 c.get('/small_tuple')1226- assert 'tuple must have the form' in str(e)1226+ assert 'tuple must have the form' in str(e.value)1227 1228 pytest.raises(TypeError, c.get, '/large_tuple')1229 1230 with pytest.raises(TypeError) as e:1231 c.get('/bad_type')1232- assert 'it was a bool' in str(e)1232+ assert 'it was a bool' in str(e.value)1233 1234 pytest.raises(TypeError, c.get, '/bad_wsgi')1235 1236 384 unmodified lines1621 with pytest.raises(AssertionError) as e:1622 @app.route('/foo')1623 def broken():1624 return 'Meh'1625- assert 'A setup function was called' in str(e)1625+ assert 'A setup function was called' in str(e.value)1626 1627 app.debug = False1628 1629 @app.route('/foo')46 unmodified lines1676 1677 with client:1678 with pytest.raises(AssertionError) as e:1679 client.post('/foo', data={})1680- assert 'http://localhost/foo/' in str(e)1680+ assert 'http://localhost/foo/' in str(e.value)1681 assert ('Make sure to directly send '1682- 'your POST-request to this URL') in str(e)1682+ 'your POST-request to this URL') in str(e.value)1683 1684 rv = client.get('/foo', data={}, follow_redirects=True)1685 assert rv.data == b'success'1686 262 unmodified linesmodified:tests/test_helpers.py+2 added-2 removed
466 unmodified lines467 468 def test_send_file_object_without_mimetype(self, app, req_ctx):469 with pytest.raises(ValueError) as excinfo:470 flask.send_file(StringIO("LOL"))471- assert 'Unable to infer MIME-type' in str(excinfo)472- assert 'no filename is available' in str(excinfo)471+ assert 'Unable to infer MIME-type' in str(excinfo.value)472+ assert 'no filename is available' in str(excinfo.value)473 474 flask.send_file(StringIO("LOL"), attachment_filename='filename')475 476 def test_send_file_object(self, app, req_ctx):482 unmodified linesmodified:CHANGES.rst+1 added
8 unmodified lines9 outside debug mode, so error handlers can still access it. This10 requires upgrading to Werkzeug 0.15.5. :issue:`3249`11 - ``send_file`` url quotes the ":" and "/" characters for more12 compatible UTF-8 filename support in some browsers. :issue:`3074`13+- Fixes for PEP451 import loaders and pytest 5.x. :issue:`3275`14 15 16 Version 1.0.317 -------------881 unmodified lines