Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix UnboundLocalError #19744

Merged
merged 21 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Release History

## 1.19.2 (Unreleased)
## 1.20.0 (Unreleased)

### Features Added

### Breaking Changes

- SansIOHTTPPolicy.on_exception returns None instead of bool.

### Bugs Fixed

- UnboundLocalError when SansIOHTTPPolicy handles an exception #15222

### Other Changes

## 1.19.1 (2021-11-01)
Expand Down
6 changes: 1 addition & 5 deletions sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,7 @@ def on_response(self, request, response):
"""Is executed after the request comes back from the policy."""

def on_exception(self, request):
"""Is executed if an exception is raised while executing this policy.

Return True if the exception has been handled and should not
be forwarded to the caller.
"""
"""Is executed if an exception is raised while executing this policy."""
```

SansIOHTTPPolicy methods can be declared as coroutines, but then they can only be used with a AsyncPipeline.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# regenerated.
# --------------------------------------------------------------------------

VERSION = "1.19.2"
VERSION = "1.20.0"
4 changes: 2 additions & 2 deletions sdk/core/azure-core/azure/core/pipeline/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def send(self, request):
try:
response = self.next.send(request)
except Exception: # pylint: disable=broad-except
if not _await_result(self._policy.on_exception, request):
raise
_await_result(self._policy.on_exception, request)
raise
else:
_await_result(self._policy.on_response, request, response)
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ def send(self, request):
response = self.next.send(request)
self.on_response(request, response)
except Exception: # pylint:disable=broad-except
handled = self.on_exception(request)
if not handled:
raise
self.on_exception(request)
raise
else:
if response.http_response.status_code == 401:
self._token = None # any cached token is invalid
Expand All @@ -132,9 +131,8 @@ def send(self, request):
response = self.next.send(request)
self.on_response(request, response)
except Exception: # pylint:disable=broad-except
handled = self.on_exception(request)
if not handled:
raise
self.on_exception(request)
raise

return response

Expand Down Expand Up @@ -162,18 +160,16 @@ def on_response(self, request, response):
"""

def on_exception(self, request):
# type: (PipelineRequest) -> bool
# type: (PipelineRequest) -> None
"""Executed when an exception is raised while executing the next policy.

This method is executed inside the exception handler.

:param request: The Pipeline request object
:type request: ~azure.core.pipeline.PipelineRequest
:return: False by default, override with True to stop the exception.
:rtype: bool
"""
# pylint: disable=no-self-use,unused-argument
return False
return


class AzureKeyCredentialPolicy(SansIOHTTPPolicy):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,16 @@ def on_response(self, request: "PipelineRequest", response: "PipelineResponse")
:type response: ~azure.core.pipeline.PipelineResponse
"""

def on_exception(self, request: "PipelineRequest") -> "Union[bool, Awaitable[bool]]":
def on_exception(self, request: "PipelineRequest") -> None:
"""Executed when an exception is raised while executing the next policy.

This method is executed inside the exception handler.

:param request: The Pipeline request object
:type request: ~azure.core.pipeline.PipelineRequest
:return: False by default, override with True to stop the exception.
:rtype: bool
"""
# pylint: disable=no-self-use,unused-argument
return False
return

def _need_new_token(self) -> bool:
return not self._token or self._token.expires_on - time.time() < 300
9 changes: 2 additions & 7 deletions sdk/core/azure-core/azure/core/pipeline/policies/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,13 @@ def on_response(self, request, response):

# pylint: disable=no-self-use
def on_exception(self, request): # pylint: disable=unused-argument
# type: (PipelineRequest) -> Union[bool, Awaitable[bool]]
# type: (PipelineRequest) -> None
"""Is executed if an exception is raised while executing the next policy.

Developer can optionally implement this method to return True
if the exception has been handled and should not be forwarded to the caller.

This method is executed inside the exception handler.

:param request: The Pipeline request object
:type request: ~azure.core.pipeline.PipelineRequest
:return: False by default, override with True to stop the exception.
:rtype: bool

.. admonition:: Example:

Expand All @@ -130,7 +125,7 @@ def on_exception(self, request): # pylint: disable=unused-argument
:language: python
:dedent: 4
"""
return False
return


class RequestHistory(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ def on_response(self, request, response):
# type: (PipelineRequest, PipelineResponse) -> None
self.end_span(request, response=response.http_response)

def on_exception(self, request): # pylint: disable=unused-argument
# type: (PipelineRequest) -> bool
def on_exception(self, request):
# type: (PipelineRequest) -> None
self.end_span(request, exc_info=sys.exc_info())
return False
14 changes: 0 additions & 14 deletions sdk/core/azure-core/samples/test_example_sansio.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,3 @@ def example_proxy_policy():
# You can also configure proxies by setting the environment variables
# HTTP_PROXY and HTTPS_PROXY.
# [END proxy_policy]

def example_on_exception():
policy = SansIOHTTPPolicy()
request = HttpRequest("GET", "https://bing.com")
# [START on_exception]
try:
response = policy.on_request(request)
except Exception:
if not policy.on_exception(request):
raise

# or use
exc_type, exc_value, exc_traceback = sys.exc_info()
# [END on_exception]