diff options
author | Nathaniel Manista <nathaniel@google.com> | 2018-06-08 19:19:55 +0000 |
---|---|---|
committer | Nathaniel Manista <nathaniel@google.com> | 2018-06-08 21:07:14 +0000 |
commit | 369d827445d3ebc6e0f0f4013841fbde7758a5ed (patch) | |
tree | de05aae7feff767f6badfc077ffa83c1792549b4 /src/python/grpcio | |
parent | 8872a312c3f5618f48b20ab0369706dd4e909816 (diff) |
Check conformance to grpc.GenericRpcHandler type
Diffstat (limited to 'src/python/grpcio')
-rw-r--r-- | src/python/grpcio/grpc/__init__.py | 8 | ||||
-rw-r--r-- | src/python/grpcio/grpc/_server.py | 19 |
2 files changed, 23 insertions, 4 deletions
diff --git a/src/python/grpcio/grpc/__init__.py b/src/python/grpcio/grpc/__init__.py index 0f31119467..5a9762043f 100644 --- a/src/python/grpcio/grpc/__init__.py +++ b/src/python/grpcio/grpc/__init__.py @@ -1656,9 +1656,11 @@ def server(thread_pool, A Server object. """ from grpc import _server # pylint: disable=cyclic-import - return _server.Server(thread_pool, () if handlers is None else handlers, () - if interceptors is None else interceptors, () if - options is None else options, maximum_concurrent_rpcs) + return _server.create_server(thread_pool, () + if handlers is None else handlers, () + if interceptors is None else interceptors, () + if options is None else options, + maximum_concurrent_rpcs) ################################### __all__ ################################# diff --git a/src/python/grpcio/grpc/_server.py b/src/python/grpcio/grpc/_server.py index d12a2421cd..61d9d70667 100644 --- a/src/python/grpcio/grpc/_server.py +++ b/src/python/grpcio/grpc/_server.py @@ -787,7 +787,16 @@ def _start(state): thread.start() -class Server(grpc.Server): +def _validate_generic_rpc_handlers(generic_rpc_handlers): + for generic_rpc_handler in generic_rpc_handlers: + service_attribute = getattr(generic_rpc_handler, 'service', None) + if service_attribute is None: + raise AttributeError( + '"{}" must conform to grpc.GenericRpcHandler type but does ' + 'not have "service" method!'.format(generic_rpc_handler)) + + +class _Server(grpc.Server): # pylint: disable=too-many-arguments def __init__(self, thread_pool, generic_handlers, interceptors, options, @@ -800,6 +809,7 @@ class Server(grpc.Server): thread_pool, maximum_concurrent_rpcs) def add_generic_rpc_handlers(self, generic_rpc_handlers): + _validate_generic_rpc_handlers(generic_rpc_handlers) _add_generic_handlers(self._state, generic_rpc_handlers) def add_insecure_port(self, address): @@ -817,3 +827,10 @@ class Server(grpc.Server): def __del__(self): _stop(self._state, None) + + +def create_server(thread_pool, generic_rpc_handlers, interceptors, options, + maximum_concurrent_rpcs): + _validate_generic_rpc_handlers(generic_rpc_handlers) + return _Server(thread_pool, generic_rpc_handlers, interceptors, options, + maximum_concurrent_rpcs) |