Skip to content

Commit 49af4df

Browse files
committed
Removing usage of the gRPC beta subpackage.
Fixes #2130.
1 parent 9d52a8c commit 49af4df

File tree

20 files changed

+508
-686
lines changed

20 files changed

+508
-686
lines changed

appveyor.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,21 @@ environment:
3030
- PYTHON: "C:\\Python34"
3131
PYTHON_VERSION: "3.4.4"
3232
PYTHON_ARCH: "32"
33-
# Use mocked-up GRPC for now
3433

3534
- PYTHON: "C:\\Python34-x64"
3635
PYTHON_VERSION: "3.4.4"
3736
PYTHON_ARCH: "64"
38-
# Use mocked-up GRPC for now
3937

4038
# Python 3.5.1 is the latest Python 3.5 with a Windows installer
4139
# Python 3.5.1 is the overall latest
4240
# https://www.python.org/ftp/python/3.5.1/
4341
- PYTHON: "C:\\Python35"
4442
PYTHON_VERSION: "3.5.1"
4543
PYTHON_ARCH: "32"
46-
# Use mocked-up GRPC for now
4744

4845
- PYTHON: "C:\\Python35-x64"
4946
PYTHON_VERSION: "3.5.1"
5047
PYTHON_ARCH: "64"
51-
# Use mocked-up GRPC for now
5248

5349
install:
5450
- ECHO "Filesystem root:"
@@ -85,7 +81,6 @@ build_script:
8581
- "%CMD_IN_ENV% python setup.py build"
8682

8783
test_script:
88-
- "set PYTHONPATH=%GRPC_PATH%"
8984
- "%CMD_IN_ENV% pip list"
9085
# Run the project tests
9186
- "%CMD_IN_ENV% python setup.py nosetests"

gcloud/_helpers.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
except ImportError:
3131
app_identity = None
3232
try:
33-
from grpc.beta import implementations
33+
from google.gax.grpc import exc_to_code as beta_exc_to_code
34+
import grpc
35+
from grpc._channel import _Rendezvous
3436
except ImportError: # pragma: NO COVER
35-
implementations = None
37+
beta_exc_to_code = None
38+
grpc = None
39+
_Rendezvous = Exception
3640
import six
3741
from six.moves.http_client import HTTPConnection
3842
from six.moves import configparser
@@ -572,10 +576,10 @@ def __call__(self, unused_context, callback):
572576
callback(headers, None)
573577

574578

575-
def make_stub(credentials, user_agent, stub_factory, host, port):
579+
def make_stub(credentials, user_agent, stub_class, host, port):
576580
"""Makes a stub for an RPC service.
577581
578-
Uses / depends on the beta implementation of gRPC.
582+
Uses / depends on gRPC.
579583
580584
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
581585
:param credentials: The OAuth2 Credentials to use for creating
@@ -584,29 +588,44 @@ def make_stub(credentials, user_agent, stub_factory, host, port):
584588
:type user_agent: str
585589
:param user_agent: (Optional) The user agent to be used with API requests.
586590
587-
:type stub_factory: callable
588-
:param stub_factory: A factory which will create a gRPC stub for
589-
a given service.
591+
:type stub_class: type
592+
:param stub_class: A gRPC stub type for a given service.
590593
591594
:type host: str
592595
:param host: The host for the service.
593596
594597
:type port: int
595598
:param port: The port for the service.
596599
597-
:rtype: :class:`grpc.beta._stub._AutoIntermediary`
600+
:rtype: object, instance of ``stub_class``
598601
:returns: The stub object used to make gRPC requests to a given API.
599602
"""
600603
# Leaving the first argument to ssl_channel_credentials() as None
601604
# loads root certificates from `grpc/_adapter/credentials/roots.pem`.
602-
transport_creds = implementations.ssl_channel_credentials(None, None, None)
605+
transport_creds = grpc.ssl_channel_credentials(None, None, None)
603606
custom_metadata_plugin = MetadataPlugin(credentials, user_agent)
604-
auth_creds = implementations.metadata_call_credentials(
607+
auth_creds = grpc.metadata_call_credentials(
605608
custom_metadata_plugin, name='google_creds')
606-
channel_creds = implementations.composite_channel_credentials(
609+
channel_creds = grpc.composite_channel_credentials(
607610
transport_creds, auth_creds)
608-
channel = implementations.secure_channel(host, port, channel_creds)
609-
return stub_factory(channel)
611+
target = '%s:%d' % (host, port)
612+
channel = grpc.secure_channel(target, channel_creds)
613+
return stub_class(channel)
614+
615+
616+
def exc_to_code(exc):
617+
"""Retrieves the status code from a gRPC exception.
618+
619+
:type exc: :class:`Exception`
620+
:param exc: An exception from gRPC beta or stable.
621+
622+
:rtype: :class:`grpc.StatusCode`
623+
:returns: The status code attached to the exception.
624+
"""
625+
if isinstance(exc, _Rendezvous):
626+
return exc.code()
627+
else:
628+
return beta_exc_to_code(exc)
610629

611630

612631
try:

gcloud/_testing.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,19 @@ def __init__(self, **kw):
5858
self.__dict__.update(kw)
5959

6060
def _make_grpc_error(self, status_code):
61-
from grpc.framework.interfaces.face.face import AbortionError
61+
from grpc._channel import _Rendezvous
62+
from grpc._channel import _RPCState
6263

63-
class _DummyException(AbortionError):
64-
code = status_code
65-
66-
def __init__(self):
67-
super(_DummyException, self).__init__(
68-
None, None, self.code, None)
69-
70-
return _DummyException()
64+
details = 'Some error details.'
65+
exc_state = _RPCState((), None, None, status_code, details)
66+
return _Rendezvous(exc_state, None, None, None)
7167

7268
def _make_grpc_not_found(self):
73-
from grpc.beta.interfaces import StatusCode
69+
from grpc import StatusCode
7470
return self._make_grpc_error(StatusCode.NOT_FOUND)
7571

7672
def _make_grpc_failed_precondition(self):
77-
from grpc.beta.interfaces import StatusCode
73+
from grpc import StatusCode
7874
return self._make_grpc_error(StatusCode.FAILED_PRECONDITION)
7975

8076

gcloud/bigtable/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
"""
3434

3535
try:
36-
import grpc.beta.implementations
36+
import grpc
3737
except ImportError as exc: # pragma: NO COVER
3838
raise ImportError(_ERR_MSG, exc)

gcloud/bigtable/_testing.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ class _FakeStub(object):
2121
def __init__(self, *results):
2222
self.results = results
2323
self.method_calls = []
24-
self._entered = 0
25-
self._exited = []
26-
27-
def __enter__(self):
28-
self._entered += 1
29-
return self
30-
31-
def __exit__(self, exc_type, exc_val, exc_tb):
32-
self._exited.append((exc_type, exc_val, exc_tb))
33-
return True
3424

3525
def __getattr__(self, name):
3626
# We need not worry about attributes set in constructor
@@ -41,17 +31,16 @@ def __getattr__(self, name):
4131
class _MethodMock(object):
4232
"""Mock for API method attached to a gRPC stub.
4333
44-
In the beta implementation, these are of type.
45-
:class:`grpc.framework.crust.implementations._UnaryUnaryMultiCallable`
34+
These are of type :class:`grpc._channel._UnaryUnaryMultiCallable`.
4635
"""
4736

48-
def __init__(self, name, factory):
37+
def __init__(self, name, stub):
4938
self._name = name
50-
self._factory = factory
39+
self._stub = stub
5140

5241
def __call__(self, *args, **kwargs):
5342
"""Sync method meant to mock a gRPC stub request."""
54-
self._factory.method_calls.append((self._name, args, kwargs))
55-
curr_result, self._factory.results = (self._factory.results[0],
56-
self._factory.results[1:])
43+
self._stub.method_calls.append((self._name, args, kwargs))
44+
curr_result, self._stub.results = (self._stub.results[0],
45+
self._stub.results[1:])
5746
return curr_result

0 commit comments

Comments
 (0)