1414
1515"""Connections to Google Cloud Datastore API servers."""
1616
17+ import contextlib
1718import os
1819
1920from 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
407392class Connection (connection_module .Connection ):
0 commit comments