Skip to content
Myr

Merge pull request #3278 from asottile/importlib_spec

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

Fixes for PEP451 import loaders and pytest 5.x

authored by David Lord · committed by GitHub · parents b9c2267, a5ecdfa · 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

6 files+57 added−25 removed

modified:docs/tutorial/tests.rst+1 added-1 removed
186 unmodified lines
187188 with pytest.raises(sqlite3.ProgrammingError) as e:189 db.execute('SELECT 1')190191 assert 'closed' in str(e)191 assert 'closed' in str(e.value)192193The ``init-db`` command should call the ``init_db`` function and output194a message.195
366 unmodified lines
modified:examples/tutorial/tests/test_db.py+1 added-1 removed
10 unmodified lines
1112 with pytest.raises(sqlite3.ProgrammingError) as e:13 db.execute('SELECT 1')1415 assert 'closed' in str(e)15 assert 'closed' in str(e.value)161718def test_init_db_command(runner, monkeypatch):19 class Recorder(object):
9 unmodified lines
modified:flask/helpers.py+46 added-15 removed
787 unmodified lines
788 'you encounter this error please file a bug against Flask.') %789 loader.__class__.__name__)790791792def 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]792def _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.util796797 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)818819 # 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 lines
833 # 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))840841 # 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)846847 return package_path848849850def 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_path
219 unmodified lines
modified:tests/test_basic.py+6 added-6 removed
1217 unmodified lines
1218 c = app.test_client()12191220 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)12231224 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)12271228 pytest.raises(TypeError, c.get, '/large_tuple')12291230 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)12331234 pytest.raises(TypeError, c.get, '/bad_wsgi')12351236
384 unmodified lines
1621 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)16261627 app.debug = False16281629 @app.route('/foo')
46 unmodified lines
16761677 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)16831684 rv = client.get('/foo', data={}, follow_redirects=True)1685 assert rv.data == b'success'1686
262 unmodified lines
modified:tests/test_helpers.py+2 added-2 removed
466 unmodified lines
467468 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)473474 flask.send_file(StringIO("LOL"), attachment_filename='filename')475476 def test_send_file_object(self, app, req_ctx):
482 unmodified lines
modified:CHANGES.rst+1 added
8 unmodified lines
9 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`141516Version 1.0.317-------------
881 unmodified lines