From 8f58715c40ab01010f6cbffeaec8be5ebc9bf84f Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 3 Oct 2021 11:37:48 +1000 Subject: [PATCH] Fix compat with Sanic v21.9.0+ Update sptk to 1.2.0 --- CHANGELOG.md | 5 ++++- README.rst | 5 ++++- requirements.txt | 5 +++-- sanic_cors/extension.py | 33 +++++++++++++++++++++------------ sanic_cors/version.py | 2 +- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4502d..c49e5ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log - +## 1.0.1 +- Fix exception handler compatibility with Sanic v21.9.0 +- Bump min SPTK version requirement to v1.2.0 + ## 1.0.0 - Replace Sanic-Plugins-Framework (SPF) with Sanic-Plugin-Toolkit (SPTK) - Remove python 3.6 compatibility diff --git a/README.rst b/README.rst index 25bfdbf..3608ea8 100644 --- a/README.rst +++ b/README.rst @@ -17,7 +17,10 @@ credential'ed requests, and please make sure you add some sort of `CSRF `__ protection before doing so! -**Notice:** +**Sept Notice:** +Please upgrade to Sanic-CORS v1.0.1 if you need compatibility with Sanic v21.9+ + +**March Notice:** Please upgrade to Sanic-CORS v1.0.0 if you need compatibility with Sanic v21.3+ (and don't forget to replace SPF with SPTK) **Older Notice:** diff --git a/requirements.txt b/requirements.txt index 30daf59..8abff39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ -sanic-plugin-toolkit>=1.0.0,<2 -sanic>=21.3.1,<22 +sanic-plugin-toolkit>=1.2.0,<2 +sanic>=21.3.1,<22,!=21.3.0,!=21.6.0,!=21.9.0 + diff --git a/sanic_cors/extension.py b/sanic_cors/extension.py index c98f3ab..8c36719 100644 --- a/sanic_cors/extension.py +++ b/sanic_cors/extension.py @@ -25,6 +25,8 @@ SANIC_18_12_0 = LooseVersion("18.12.0") SANIC_19_9_0 = LooseVersion("19.9.0") SANIC_19_12_0 = LooseVersion("19.12.0") +SANIC_21_9_0 = LooseVersion("21.9.0") + USE_ASYNC_EXCEPTION_HANDLER = False @@ -190,11 +192,10 @@ def init_app(self, context, *args, **kwargs): resources_human = dict([(get_regexp_pattern(pattern), opts) for (pattern, opts) in resources]) debug("Configuring CORS with resources: {}".format(resources_human)) - try: - assert app.error_handler - cors_error_handler = CORSErrorHandler(context, app.error_handler) - app.error_handler = cors_error_handler - except (AttributeError, AssertionError): + if hasattr(app, "error_handler"): + cors_error_handler = CORSErrorHandler(context, app.error_handler, fallback="auto") + setattr(app, "error_handler", cors_error_handler) + else: # Blueprints have no error_handler. Just skip error_handler initialisation pass @@ -359,18 +360,26 @@ def __new__(cls, *args, **kwargs): self.response = self.sync_response return self - def __init__(self, context, orig_handler): - super(CORSErrorHandler, self).__init__() + def __init__(self, context, orig_handler, fallback="auto"): + if SANIC_21_9_0 <= SANIC_VERSION: + super(CORSErrorHandler, self).__init__(fallback=fallback) + else: + super(CORSErrorHandler, self).__init__() self.orig_handler = orig_handler self.ctx = context - def add(self, exception, handler): - self.orig_handler.add(exception, handler) - - def lookup(self, exception): - return self.orig_handler.lookup(exception) + if SANIC_21_9_0 <= SANIC_VERSION: + def add(self, exception, handler, route_names = None): + self.orig_handler.add(exception, handler, route_names=route_names) + def lookup(self, exception, route_name=None): + return self.orig_handler.lookup(exception, route_name=route_name) + else: + def add(self, exception, handler): + self.orig_handler.add(exception, handler) + def lookup(self, exception): + return self.orig_handler.lookup(exception) # wrap app's original exception response function # so that error responses have proper CORS headers @classmethod diff --git a/sanic_cors/version.py b/sanic_cors/version.py index 1f356cc..cd7ca49 100644 --- a/sanic_cors/version.py +++ b/sanic_cors/version.py @@ -1 +1 @@ -__version__ = '1.0.0' +__version__ = '1.0.1'