Skip to content

Add Python Reflection Client#29085

Merged
lidizheng merged 1 commit intogrpc:masterfrom
tomerv:python-reflection-client
Mar 29, 2022
Merged

Add Python Reflection Client#29085
lidizheng merged 1 commit intogrpc:masterfrom
tomerv:python-reflection-client

Conversation

@tomerv
Copy link
Copy Markdown
Contributor

@tomerv tomerv commented Mar 14, 2022

Implement ProtoReflectionDescriptorDatabase in Python to support
client-side reflection sevices.

See gRFC L95.

See also #29023.

@lidizheng

Implement ProtoReflectionDescriptorDatabase in Python to support
client-side reflection sevices.

See gRFC L95.
with self.assertRaises(KeyError):
res = service_desc.FindMethodByName(_INVALID_SYMBOL_NAME)
if res is None:
raise KeyError()
Copy link
Copy Markdown
Contributor

@lidizheng lidizheng Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share more detail about this PR? Are we going to remove this test, or what improvement did we add to make it compatible? Sorry, the GitHub review is not the greatest to see diff.

#29023 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original test was

        with self.assertRaises(KeyError):
            service_desc.FindMethodByName(_INVALID_SYMBOL_NAME)

I changed it to translate a None return value to a raise KeyError.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear write-up in protocolbuffers/protobuf#9592.

Checking other find* methods (Python code), they favor the raise behavior. Do you know how the C-Extension implementation will behave? Based on code, their return type is bool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the code you linked, the return type is bool but internally it calls a Python function (PyObject_CallMethod(...)). In this context we have a call chain that look like Python -> C++ -> Python, where the outer Python code is the one doing the call desc_database.FindFileByName(...) and the inner is the FindFileByName implementation. I'm not an expert on this, but I believe that if the inner Python code raises an exception then it is immediately caught by the outer Python code - it doesn't even do exception folding of that C++ function, just sort of steals the context.

If the exception is not caught by the outer Python code, it looks like this:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    desc_pool.FindFileByName("foo")
KeyError: "Couldn't find file foo"

Note how all the traceback of the inner C++ -> Python is missing as result of this unusual flow.

I hope this answers your question.

@lidizheng lidizheng added lang/Python kokoro:run release notes: yes Indicates if PR needs to be in release notes labels Mar 15, 2022
Copy link
Copy Markdown
Contributor

@lidizheng lidizheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@tomerv
Copy link
Copy Markdown
Contributor Author

tomerv commented Mar 21, 2022

LGTM.

Thanks! When can this be merged?

@lidizheng
Copy link
Copy Markdown
Contributor

lidizheng commented Mar 21, 2022

@gnossen PTAL.

@tomerv The policy was each PR requires 2 maintainers review.

Triggered test: prod:grpc/core/master/linux/aws/grpc_aws_basictests_python

@lidizheng lidizheng merged commit 2953b45 into grpc:master Mar 29, 2022
@copybara-service copybara-service Bot added the imported Specifies if the PR has been imported to the internal repository label Mar 30, 2022
@tomerv tomerv deleted the python-reflection-client branch April 3, 2022 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported Specifies if the PR has been imported to the internal repository lang/Python release notes: yes Indicates if PR needs to be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants