Skip to content

Commit 973b3c1

Browse files
authored
fix UnboundLocalError (Azure#19744)
* fix UnboundLocalError * update * update * update * update * update * update * update * update * update * update * update * update * update * update
1 parent f2177e3 commit 973b3c1

File tree

9 files changed

+21
-47
lines changed

9 files changed

+21
-47
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Release History
22

3-
## 1.19.2 (Unreleased)
3+
## 1.20.0 (Unreleased)
44

55
### Features Added
66

77
### Breaking Changes
88

9+
- SansIOHTTPPolicy.on_exception returns None instead of bool.
10+
911
### Bugs Fixed
1012

13+
- UnboundLocalError when SansIOHTTPPolicy handles an exception #15222
14+
1115
### Other Changes
1216

1317
## 1.19.1 (2021-11-01)

sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,7 @@ def on_response(self, request, response):
347347
"""Is executed after the request comes back from the policy."""
348348

349349
def on_exception(self, request):
350-
"""Is executed if an exception is raised while executing this policy.
351-
352-
Return True if the exception has been handled and should not
353-
be forwarded to the caller.
354-
"""
350+
"""Is executed if an exception is raised while executing this policy."""
355351
```
356352

357353
SansIOHTTPPolicy methods can be declared as coroutines, but then they can only be used with a AsyncPipeline.

sdk/core/azure-core/azure/core/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.19.2"
12+
VERSION = "1.20.0"

sdk/core/azure-core/azure/core/pipeline/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def send(self, request):
7070
try:
7171
response = self.next.send(request)
7272
except Exception: # pylint: disable=broad-except
73-
if not _await_result(self._policy.on_exception, request):
74-
raise
73+
_await_result(self._policy.on_exception, request)
74+
raise
7575
else:
7676
_await_result(self._policy.on_response, request, response)
7777
return response

sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ def send(self, request):
119119
response = self.next.send(request)
120120
self.on_response(request, response)
121121
except Exception: # pylint:disable=broad-except
122-
handled = self.on_exception(request)
123-
if not handled:
124-
raise
122+
self.on_exception(request)
123+
raise
125124
else:
126125
if response.http_response.status_code == 401:
127126
self._token = None # any cached token is invalid
@@ -132,9 +131,8 @@ def send(self, request):
132131
response = self.next.send(request)
133132
self.on_response(request, response)
134133
except Exception: # pylint:disable=broad-except
135-
handled = self.on_exception(request)
136-
if not handled:
137-
raise
134+
self.on_exception(request)
135+
raise
138136

139137
return response
140138

@@ -162,18 +160,16 @@ def on_response(self, request, response):
162160
"""
163161

164162
def on_exception(self, request):
165-
# type: (PipelineRequest) -> bool
163+
# type: (PipelineRequest) -> None
166164
"""Executed when an exception is raised while executing the next policy.
167165
168166
This method is executed inside the exception handler.
169167
170168
:param request: The Pipeline request object
171169
:type request: ~azure.core.pipeline.PipelineRequest
172-
:return: False by default, override with True to stop the exception.
173-
:rtype: bool
174170
"""
175171
# pylint: disable=no-self-use,unused-argument
176-
return False
172+
return
177173

178174

179175
class AzureKeyCredentialPolicy(SansIOHTTPPolicy):

sdk/core/azure-core/azure/core/pipeline/policies/_authentication_async.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,16 @@ def on_response(self, request: "PipelineRequest", response: "PipelineResponse")
115115
:type response: ~azure.core.pipeline.PipelineResponse
116116
"""
117117

118-
def on_exception(self, request: "PipelineRequest") -> "Union[bool, Awaitable[bool]]":
118+
def on_exception(self, request: "PipelineRequest") -> None:
119119
"""Executed when an exception is raised while executing the next policy.
120120
121121
This method is executed inside the exception handler.
122122
123123
:param request: The Pipeline request object
124124
:type request: ~azure.core.pipeline.PipelineRequest
125-
:return: False by default, override with True to stop the exception.
126-
:rtype: bool
127125
"""
128126
# pylint: disable=no-self-use,unused-argument
129-
return False
127+
return
130128

131129
def _need_new_token(self) -> bool:
132130
return not self._token or self._token.expires_on - time.time() < 300

sdk/core/azure-core/azure/core/pipeline/policies/_base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,13 @@ def on_response(self, request, response):
109109

110110
# pylint: disable=no-self-use
111111
def on_exception(self, request): # pylint: disable=unused-argument
112-
# type: (PipelineRequest) -> Union[bool, Awaitable[bool]]
112+
# type: (PipelineRequest) -> None
113113
"""Is executed if an exception is raised while executing the next policy.
114114
115-
Developer can optionally implement this method to return True
116-
if the exception has been handled and should not be forwarded to the caller.
117-
118115
This method is executed inside the exception handler.
119116
120117
:param request: The Pipeline request object
121118
:type request: ~azure.core.pipeline.PipelineRequest
122-
:return: False by default, override with True to stop the exception.
123-
:rtype: bool
124119
125120
.. admonition:: Example:
126121
@@ -130,7 +125,7 @@ def on_exception(self, request): # pylint: disable=unused-argument
130125
:language: python
131126
:dedent: 4
132127
"""
133-
return False
128+
return
134129

135130

136131
class RequestHistory(object):

sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def on_response(self, request, response):
127127
# type: (PipelineRequest, PipelineResponse) -> None
128128
self.end_span(request, response=response.http_response)
129129

130-
def on_exception(self, request): # pylint: disable=unused-argument
131-
# type: (PipelineRequest) -> bool
130+
def on_exception(self, request):
131+
# type: (PipelineRequest) -> None
132132
self.end_span(request, exc_info=sys.exc_info())
133-
return False

sdk/core/azure-core/samples/test_example_sansio.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,3 @@ def example_proxy_policy():
167167
# You can also configure proxies by setting the environment variables
168168
# HTTP_PROXY and HTTPS_PROXY.
169169
# [END proxy_policy]
170-
171-
def example_on_exception():
172-
policy = SansIOHTTPPolicy()
173-
request = HttpRequest("GET", "https://bing.com")
174-
# [START on_exception]
175-
try:
176-
response = policy.on_request(request)
177-
except Exception:
178-
if not policy.on_exception(request):
179-
raise
180-
181-
# or use
182-
exc_type, exc_value, exc_traceback = sys.exc_info()
183-
# [END on_exception]

0 commit comments

Comments
 (0)