Skip to content

Commit b62c95f

Browse files
committed
Switching _grpc_catch_rendezvous to a context manager.
1 parent 3e2788c commit b62c95f

2 files changed

Lines changed: 27 additions & 37 deletions

File tree

datastore/google/cloud/datastore/connection.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Connections to Google Cloud Datastore API servers."""
1616

17+
import contextlib
1718
import os
1819

1920
from google.rpc import status_pb2
@@ -237,35 +238,19 @@ def allocate_ids(self, project, request_pb):
237238
_datastore_pb2.AllocateIdsResponse)
238239

239240

240-
def _grpc_catch_rendezvous(to_call, *args, **kwargs):
241-
"""Call a method/function and re-map gRPC exceptions.
241+
@contextlib.contextmanager
242+
def _grpc_catch_rendezvous():
243+
"""Re-map gRPC exceptions that happen in context.
242244
243245
.. _code.proto: https://github.com/googleapis/googleapis/blob/\
244246
master/google/rpc/code.proto
245247
246248
Remaps gRPC exceptions to the classes defined in
247249
:mod:`~google.cloud.exceptions` (according to the description
248250
in `code.proto`_).
249-
250-
:type to_call: callable
251-
:param to_call: Callable that makes a request which may raise a
252-
:class:`~google.cloud.exceptions.GrpcRendezvous`.
253-
254-
:type args: tuple
255-
:param args: Positional arugments to the callable.
256-
257-
:type kwargs: dict
258-
:param kwargs: Keyword arguments to the callable.
259-
260-
:rtype: object
261-
:returns: The value returned from ``to_call``.
262-
:raises: :class:`~google.cloud.exceptions.GrpcRendezvous` if one
263-
is encountered that can't be re-mapped, otherwise maps
264-
to a :class:`~google.cloud.exceptions.GoogleCloudError`
265-
subclass.
266251
"""
267252
try:
268-
return to_call(*args, **kwargs)
253+
yield
269254
except exceptions.GrpcRendezvous as exc:
270255
error_code = exc.code()
271256
error_class = _GRPC_ERROR_MAPPING.get(error_code)
@@ -331,8 +316,8 @@ def run_query(self, project, request_pb):
331316
:returns: The returned protobuf response object.
332317
"""
333318
request_pb.project_id = project
334-
return _grpc_catch_rendezvous(
335-
self._stub.RunQuery, request_pb)
319+
with _grpc_catch_rendezvous():
320+
return self._stub.RunQuery(request_pb)
336321

337322
def begin_transaction(self, project, request_pb):
338323
"""Perform a ``beginTransaction`` request.
@@ -349,8 +334,8 @@ def begin_transaction(self, project, request_pb):
349334
:returns: The returned protobuf response object.
350335
"""
351336
request_pb.project_id = project
352-
return _grpc_catch_rendezvous(
353-
self._stub.BeginTransaction, request_pb)
337+
with _grpc_catch_rendezvous():
338+
return self._stub.BeginTransaction(request_pb)
354339

355340
def commit(self, project, request_pb):
356341
"""Perform a ``commit`` request.
@@ -366,8 +351,8 @@ def commit(self, project, request_pb):
366351
:returns: The returned protobuf response object.
367352
"""
368353
request_pb.project_id = project
369-
return _grpc_catch_rendezvous(
370-
self._stub.Commit, request_pb)
354+
with _grpc_catch_rendezvous():
355+
return self._stub.Commit(request_pb)
371356

372357
def rollback(self, project, request_pb):
373358
"""Perform a ``rollback`` request.
@@ -383,8 +368,8 @@ def rollback(self, project, request_pb):
383368
:returns: The returned protobuf response object.
384369
"""
385370
request_pb.project_id = project
386-
return _grpc_catch_rendezvous(
387-
self._stub.Rollback, request_pb)
371+
with _grpc_catch_rendezvous():
372+
return self._stub.Rollback(request_pb)
388373

389374
def allocate_ids(self, project, request_pb):
390375
"""Perform an ``allocateIds`` request.
@@ -400,8 +385,8 @@ def allocate_ids(self, project, request_pb):
400385
:returns: The returned protobuf response object.
401386
"""
402387
request_pb.project_id = project
403-
return _grpc_catch_rendezvous(
404-
self._stub.AllocateIds, request_pb)
388+
with _grpc_catch_rendezvous():
389+
return self._stub.AllocateIds(request_pb)
405390

406391

407392
class Connection(connection_module.Connection):

datastore/unit_tests/test_connection.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ def test__request_not_200(self):
109109
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
110110
class Test__grpc_catch_rendezvous(unittest.TestCase):
111111

112-
def _callFUT(self, to_call, *args, **kwargs):
112+
def _callFUT(self):
113113
from google.cloud.datastore.connection import _grpc_catch_rendezvous
114-
return _grpc_catch_rendezvous(to_call, *args, **kwargs)
114+
return _grpc_catch_rendezvous()
115115

116116
@staticmethod
117117
def _fake_method(exc, result=None):
@@ -122,7 +122,8 @@ def _fake_method(exc, result=None):
122122

123123
def test_success(self):
124124
expected = object()
125-
result = self._callFUT(self._fake_method, None, expected)
125+
with self._callFUT():
126+
result = self._fake_method(None, expected)
126127
self.assertIs(result, expected)
127128

128129
def test_failure_aborted(self):
@@ -135,7 +136,8 @@ def test_failure_aborted(self):
135136
exc_state = _RPCState((), None, None, StatusCode.ABORTED, details)
136137
exc = GrpcRendezvous(exc_state, None, None, None)
137138
with self.assertRaises(Conflict):
138-
self._callFUT(self._fake_method, exc)
139+
with self._callFUT():
140+
self._fake_method(exc)
139141

140142
def test_failure_invalid_argument(self):
141143
from grpc import StatusCode
@@ -149,7 +151,8 @@ def test_failure_invalid_argument(self):
149151
StatusCode.INVALID_ARGUMENT, details)
150152
exc = GrpcRendezvous(exc_state, None, None, None)
151153
with self.assertRaises(BadRequest):
152-
self._callFUT(self._fake_method, exc)
154+
with self._callFUT():
155+
self._fake_method(exc)
153156

154157
def test_failure_cancelled(self):
155158
from grpc import StatusCode
@@ -159,12 +162,14 @@ def test_failure_cancelled(self):
159162
exc_state = _RPCState((), None, None, StatusCode.CANCELLED, None)
160163
exc = GrpcRendezvous(exc_state, None, None, None)
161164
with self.assertRaises(GrpcRendezvous):
162-
self._callFUT(self._fake_method, exc)
165+
with self._callFUT():
166+
self._fake_method(exc)
163167

164168
def test_commit_failure_non_grpc_err(self):
165169
exc = RuntimeError('Not a gRPC error')
166170
with self.assertRaises(RuntimeError):
167-
self._callFUT(self._fake_method, exc)
171+
with self._callFUT():
172+
self._fake_method(exc)
168173

169174

170175
class Test_DatastoreAPIOverGRPC(unittest.TestCase):

0 commit comments

Comments
 (0)