Skip to content

Commit

Permalink
Fix compat with Sanic v21.9.0+
Browse files Browse the repository at this point in the history
Update sptk to 1.2.0
  • Loading branch information
ashleysommer committed Oct 3, 2021
1 parent 0c51057 commit 8f58715
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ credential'ed requests, and please make sure you add some sort of
`CSRF <http://en.wikipedia.org/wiki/Cross-site_request_forgery>`__
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:**
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

33 changes: 21 additions & 12 deletions sanic_cors/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sanic_cors/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = '1.0.1'

0 comments on commit 8f58715

Please sign in to comment.