From cc3b0d80803c37ea356121382762d439c6cfbd51 Mon Sep 17 00:00:00 2001 From: shane Date: Mon, 30 Sep 2024 13:19:15 +0900 Subject: [PATCH 1/5] :bug: :wrench: (#1342) fix Zappa.create_handler_venv win32 case where stderror_result is None --- tests/tests.py | 26 ++++++++++++++++++++++++++ zappa/core.py | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index fb286922d..d280c2d6c 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2634,6 +2634,32 @@ def test_wsgi_query_string_with_encodechars(self): expected = "query=Jane%26John&otherquery=B&test=hello%2Bm.te%26how%26are%26you" self.assertEqual(request["QUERY_STRING"], expected) + @mock.patch() + @mock.patch(subprocess.Popen) + def test_create_handler_venv_win32_none_stderror_result(self, popen_mock): + + class PopenMock: + return_code = 999 + + @classmethod + def communicate(cls): + return "valid_stdout", None # On win32, stderr can be None + + + popen_mock.return_value = PopenMock + + boto_mock = mock.MagicMock() + zappa_core = Zappa( + boto_session=boto_mock, + profile_name="test", + aws_region="test", + load_credentials=True, + ) + + with self.assertRaises(EnvironmentError): + zappa_core.create_handler_venv() + + if __name__ == "__main__": unittest.main() diff --git a/zappa/core.py b/zappa/core.py index 12a143be0..0698d0261 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -488,9 +488,9 @@ def create_handler_venv(self, use_zappa_release: Optional[str] = None): if pip_return_code: logger.info("command: %s", " ".join(command)) - if stdout_result.strip(): + if stdout_result and stdout_result.strip(): logger.info("stdout: %s", stdout_result.strip()) - if stderror_result.strip(): + if stderror_result and stderror_result.strip(): logger.error("stderr: %s", stderror_result) raise EnvironmentError("Pypi lookup failed") From 55436a992dc9183ae50b6b45c3a8a187b0137810 Mon Sep 17 00:00:00 2001 From: shane Date: Tue, 22 Oct 2024 15:35:03 +0900 Subject: [PATCH 2/5] :wrench: :art: fix flake8 errors --- zappa/core.py | 2 +- zappa/middleware.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/zappa/core.py b/zappa/core.py index 0698d0261..e603ce6a4 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -310,7 +310,7 @@ def __init__( self.manylinux_wheel_file_match = re.compile( rf'^.*{self.manylinux_suffix_start}-(manylinux_\d+_\d+_x86_64[.])?manylinux({"|".join(self.manylinux_suffixes)})_x86_64[.]whl$' # noqa: E501 ) - self.manylinux_wheel_abi3_file_match = re.compile(rf"^.*cp3.-abi3-manylinux.*_x86_64[.]whl$") + self.manylinux_wheel_abi3_file_match = re.compile(r"^.*cp3.-abi3-manylinux.*_x86_64[.]whl$") self.endpoint_urls = endpoint_urls self.xray_tracing = xray_tracing diff --git a/zappa/middleware.py b/zappa/middleware.py index 920db0c94..2f7bb2bfe 100644 --- a/zappa/middleware.py +++ b/zappa/middleware.py @@ -44,7 +44,11 @@ def encode_response(status, headers, exc_info=None): Related: https://github.com/Miserlou/Zappa/issues/1965 """ - new_headers = [header for header in headers if ((type(header[0]) != str) or (header[0].lower() != "set-cookie"))] + new_headers = [ + header + for header in headers + if ((type(header[0]) != str) or (header[0].lower() != "set-cookie")) + ] cookie_headers = [ (header[0].lower(), header[1]) for header in headers From 629da5ebcced14c3769ea623319dacf21ef8b131 Mon Sep 17 00:00:00 2001 From: shane Date: Tue, 22 Oct 2024 15:39:00 +0900 Subject: [PATCH 3/5] :art: :wrench: fix flake8/black > change type() check method from using type to using isinstance() --- zappa/middleware.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zappa/middleware.py b/zappa/middleware.py index 2f7bb2bfe..be6e03637 100644 --- a/zappa/middleware.py +++ b/zappa/middleware.py @@ -47,12 +47,12 @@ def encode_response(status, headers, exc_info=None): new_headers = [ header for header in headers - if ((type(header[0]) != str) or (header[0].lower() != "set-cookie")) + if isinstance(header[0], str) or header[0].lower() != "set-cookie" ] cookie_headers = [ (header[0].lower(), header[1]) for header in headers - if ((type(header[0]) == str) and (header[0].lower() == "set-cookie")) + if isinstance(header[0], str) and header[0].lower() == "set-cookie" ] new_headers = new_headers + cookie_headers From 9ddce2569ac7f3ddcc6cd6da954b1e77a752e06b Mon Sep 17 00:00:00 2001 From: shane Date: Tue, 22 Oct 2024 16:14:06 +0900 Subject: [PATCH 4/5] :wrench: run black, `black --line-length 127 .` --- tests/tests.py | 3 --- zappa/middleware.py | 6 +----- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index d280c2d6c..f77fd2e1a 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2637,7 +2637,6 @@ def test_wsgi_query_string_with_encodechars(self): @mock.patch() @mock.patch(subprocess.Popen) def test_create_handler_venv_win32_none_stderror_result(self, popen_mock): - class PopenMock: return_code = 999 @@ -2645,7 +2644,6 @@ class PopenMock: def communicate(cls): return "valid_stdout", None # On win32, stderr can be None - popen_mock.return_value = PopenMock boto_mock = mock.MagicMock() @@ -2660,6 +2658,5 @@ def communicate(cls): zappa_core.create_handler_venv() - if __name__ == "__main__": unittest.main() diff --git a/zappa/middleware.py b/zappa/middleware.py index be6e03637..2cebcebfb 100644 --- a/zappa/middleware.py +++ b/zappa/middleware.py @@ -44,11 +44,7 @@ def encode_response(status, headers, exc_info=None): Related: https://github.com/Miserlou/Zappa/issues/1965 """ - new_headers = [ - header - for header in headers - if isinstance(header[0], str) or header[0].lower() != "set-cookie" - ] + new_headers = [header for header in headers if isinstance(header[0], str) or header[0].lower() != "set-cookie"] cookie_headers = [ (header[0].lower(), header[1]) for header in headers From c9a7b79b93a738b824f0415512ce4f094f9c07a9 Mon Sep 17 00:00:00 2001 From: shane Date: Tue, 22 Oct 2024 16:38:17 +0900 Subject: [PATCH 5/5] =?UTF-8?q?:wrench:=20:white=5Fcheck=5Fmark:=20fix=20`?= =?UTF-8?q?test=5Fcreate=5Fhandler=5Fvenv=5Fwin32=5Fnone=5Fstderror=5Fresu?= =?UTF-8?q?lt`=E3=80=80PopenMock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/tests.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index f77fd2e1a..c1f1ba4c6 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2634,11 +2634,10 @@ def test_wsgi_query_string_with_encodechars(self): expected = "query=Jane%26John&otherquery=B&test=hello%2Bm.te%26how%26are%26you" self.assertEqual(request["QUERY_STRING"], expected) - @mock.patch() - @mock.patch(subprocess.Popen) + @mock.patch("subprocess.Popen") def test_create_handler_venv_win32_none_stderror_result(self, popen_mock): class PopenMock: - return_code = 999 + returncode = 999 @classmethod def communicate(cls):