Include well-known types in sys.path when using runtime protos

pull/24478/head
Richard Belleville 4 years ago
parent 897f97212a
commit f13b324941
  1. 1
      src/python/grpcio_tests/tests/unit/BUILD.bazel
  2. 47
      src/python/grpcio_tests/tests/unit/_dynamic_stubs_test.py
  3. 27
      src/python/grpcio_tests/tests/unit/data/foo/baz.proto
  4. 1
      tools/distrib/python/grpcio_tools/BUILD.bazel
  5. 2
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py

@ -125,6 +125,7 @@ py2and3_test(
srcs = ["_dynamic_stubs_test.py"],
data = [
"data/foo/bar.proto",
"data/foo/baz.proto",
],
imports = ["../../"],
main = "_dynamic_stubs_test.py",

@ -21,6 +21,8 @@ import os
import sys
import unittest
_DATA_DIR = os.path.join("tests", "unit", "data")
@contextlib.contextmanager
def _grpc_tools_unimportable():
@ -53,6 +55,18 @@ def _collect_errors(fn):
return _wrapped
def _python3_check(fn):
@functools.wraps(fn)
def _wrapped():
if sys.version_info[0] == 3:
fn()
else:
_assert_unimplemented("Python 3")
return _wrapped
def _run_in_subprocess(test_case):
sys.path.insert(
0, os.path.join(os.path.realpath(os.path.dirname(__file__)), ".."))
@ -80,24 +94,30 @@ def _assert_unimplemented(msg_substr):
@_collect_errors
@_python3_check
def _test_sunny_day():
if sys.version_info[0] == 3:
import grpc
protos, services = grpc.protos_and_services(
os.path.join("tests", "unit", "data", "foo", "bar.proto"))
assert protos.BarMessage is not None
assert services.BarStub is not None
else:
_assert_unimplemented("Python 3")
import grpc
protos, services = grpc.protos_and_services(
os.path.join(_DATA_DIR, "foo", "bar.proto"))
assert protos.BarMessage is not None
assert services.BarStub is not None
@_collect_errors
@_python3_check
def _test_well_known_types():
import grpc
protos, services = grpc.protos_and_services(
os.path.join(_DATA_DIR, "foo", "baz.proto"))
assert protos.BarMessage is not None
assert services.BarStub is not None
@_collect_errors
@_python3_check
def _test_grpc_tools_unimportable():
with _grpc_tools_unimportable():
if sys.version_info[0] == 3:
_assert_unimplemented("grpcio-tools")
else:
_assert_unimplemented("Python 3")
_assert_unimplemented("grpcio-tools")
# NOTE(rbellevi): multiprocessing.Process fails to pickle function objects
@ -109,6 +129,9 @@ class DynamicStubTest(unittest.TestCase):
def test_sunny_day(self):
_run_in_subprocess(_test_sunny_day)
def test_well_known_types(self):
_run_in_subprocess(_test_well_known_types)
def test_grpc_tools_unimportable(self):
_run_in_subprocess(_test_grpc_tools_unimportable)

@ -0,0 +1,27 @@
// Copyright 2020 The gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package tests.unit.data.foo.bar;
import "google/protobuf/wrappers.proto";
message BarMessage {
string a = 1;
};
service Bar {
rpc GetBar(BarMessage) returns (BarMessage);
};

@ -50,4 +50,5 @@ py_library(
"//src/python/grpcio/grpc:grpcio",
"@com_google_protobuf//:protobuf_python",
],
data = glob(["grpc_tools/_proto/**/*"]),
)

@ -58,6 +58,8 @@ if sys.version_info >= (3, 5, 0):
ProtoFinder(_SERVICE_MODULE_SUFFIX,
_protoc_compiler.get_services)
])
sys.path.append(
pkg_resources.resource_filename('grpc_tools', '_proto'))
_FINDERS_INSTALLED = True
def _module_name_to_proto_file(suffix, module_name):

Loading…
Cancel
Save