Skip to content

Commit efbd794

Browse files
ahilgerfacebook-github-bot
authored andcommitted
impl services cython
Summary: Cython changes to enable build. The basic idea is the invese of py3 client. To produce serialized message, py3 server uses regular serializer with cpp2 struct. Previously, the cpp2 struct was the internal data member of thrift-py3 object. Now, the cpp2 struct is converted using thrift-python C-API from thrift-python struct composed inside thrift-py3 wrapper. Reviewed By: prakashgayasen Differential Revision: D71353613 fbshipit-source-id: ed901ae1f9ed9ea702c1f69203af3bf7625a2caa
1 parent f708d69 commit efbd794

File tree

9 files changed

+45
-16
lines changed

9 files changed

+45
-16
lines changed

thrift/compiler/generate/templates/py3/services.pyx.mustache

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ from thrift.python.common cimport (
6969
MetadataBox as __MetadataBox,
7070
)
7171
72-
from thrift.py3.types cimport make_unique
72+
from thrift.py3.types cimport make_unique{{!
73+
}}{{#program:inplace_migrate?}}, deref_const{{/program:inplace_migrate?}}
7374
7475
cimport folly.futures
7576
from folly.executor cimport get_executor
@@ -84,6 +85,9 @@ from thrift.py3.stream cimport cServerStream, cServerStreamPublisher, cResponseA
8485
{{/program:has_stream?}}
8586
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types as _{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_types
8687
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.cbindings as _{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_cbindings
88+
{{#program:inplace_migrate?}}
89+
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_converter as _{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_converter
90+
{{/program:inplace_migrate?}}
8791
import {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types as _{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_types
8892
{{#program:includeNamespaces}}
8993
{{#hasServices?}}
@@ -94,6 +98,9 @@ import {{#includeNamespace}}{{value}}.{{/includeNamespace}}services as _{{#inclu
9498
import {{#includeNamespace}}{{value}}.{{/includeNamespace}}types as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}types
9599
cimport {{#includeNamespace}}{{value}}.{{/includeNamespace}}types as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}types
96100
cimport {{#includeNamespace}}{{value}}.{{/includeNamespace}}cbindings as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}cbindings
101+
{{#program:inplace_migrate?}}
102+
cimport {{#includeNamespace}}{{value}}.{{/includeNamespace}}thrift_converter as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}thrift_converter
103+
{{/program:inplace_migrate?}}
97104
{{/hasTypes?}}
98105
{{/program:includeNamespaces}}
99106

thrift/compiler/generate/templates/py3/services/callback.mustache

+7-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ async def {{service:name}}_{{function:name}}_coro(
112112
{{#function:exceptions}}
113113
{{#field:type}}
114114
except {{> types/cython_python_type}} as ex:
115-
promise.cPromise.setException(deref((<{{> types/cython_python_type}}> ex).{{> types/cpp_obj}}))
116-
{{/field:type}}
115+
promise.cPromise.setException{{!
116+
}}{{^program:inplace_migrate?}}{{!
117+
}}(deref((<{{> types/cython_python_type}}> ex).{{> types/cpp_obj}})){{!
118+
}}{{/program:inplace_migrate?}}{{#program:inplace_migrate?}}{{#type:struct}}{{!
119+
}}({{type:capi_converter_path}}.{{struct:name}}_convert_to_cpp(ex._to_python())){{!
120+
}}{{/type:struct}}{{/program:inplace_migrate?}}{{!
121+
}}{{/field:type}}
117122
{{/function:exceptions}}
118123
except __ApplicationError as ex:
119124
# If the handler raised an ApplicationError convert it to a C++ one

thrift/compiler/generate/templates/py3/services/cython_cpp_to_python.mustache

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ to be converted from unicode to binary representation.
3636
}}{{#type:float?}}{{field:py_name}}{{/type:float?}}{{!
3737
}}{{#type:string?}}(deref({{field:py_name}})).data().decode('UTF-8'){{/type:string?}}{{!
3838
}}{{#type:binary?}}(deref({{field:py_name}})){{/type:binary?}}{{!
39-
}}{{#type:struct?}}{{> types/cython_python_type}}._create_FBTHRIFT_ONLY_DO_NOT_USE({{!
39+
}}{{#type:struct}}{{^program:inplace_migrate?}}{{!
40+
}}{{> types/cython_python_type}}._create_FBTHRIFT_ONLY_DO_NOT_USE({{!
4041
}}shared_ptr[{{> types/cython_cpp_type}}]({{field:py_name}}.release())){{!
41-
}}{{/type:struct?}}{{!
42+
}}{{/program:inplace_migrate?}}{{#program:inplace_migrate?}}{{!
43+
}}{{type:capi_converter_path}}.{{struct:name}}_from_cpp(deref_const({{field:py_name}}))._to_py3(){{!
44+
}}{{/program:inplace_migrate?}}{{/type:struct}}{{!
4245
}}{{#type:container?}}{{!
4346
}}{{> types/container_from_cpp}}(deref({{field:py_name}})){{!
4447
}}{{/type:container?}}{{!

thrift/compiler/generate/templates/py3/services/cython_return_value.mustache

+4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ to the client.
5555
}}{{/type:binary?}}{{!
5656
}}{{#type:struct}}{{!
5757
}}{{^function:stack_arguments?}}make_unique[{{> types/cython_cpp_type}}]({{/function:stack_arguments?}}{{!
58+
}}{{^program:inplace_migrate?}}{{!
5859
}}deref((<{{> types/cython_python_type}}?> result).{{> types/cpp_obj}}){{!
60+
}}{{/program:inplace_migrate?}}{{#program:inplace_migrate?}}{{!
61+
}}{{type:capi_converter_path}}.{{struct:name}}_convert_to_cpp(result._to_python()){{!
62+
}}{{/program:inplace_migrate?}}{{!
5963
}}{{^function:stack_arguments?}}){{/function:stack_arguments?}}{{!
6064
}}{{/type:struct}}{{!
6165
}}{{#type:container?}}{{!

thrift/compiler/test/fixtures/basic/out/py3_inplace/gen-py3/test/fixtures/basic/module/services.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ from thrift.python.common cimport (
4848
MetadataBox as __MetadataBox,
4949
)
5050

51-
from thrift.py3.types cimport make_unique
51+
from thrift.py3.types cimport make_unique, deref_const
5252

5353
cimport folly.futures
5454
from folly.executor cimport get_executor
@@ -59,6 +59,7 @@ from folly.memory cimport to_shared_ptr as __to_shared_ptr
5959

6060
cimport test.fixtures.basic.module.types as _test_fixtures_basic_module_types
6161
cimport test.fixtures.basic.module.cbindings as _test_fixtures_basic_module_cbindings
62+
cimport test.fixtures.basic.module.thrift_converter as _test_fixtures_basic_module_thrift_converter
6263
import test.fixtures.basic.module.types as _test_fixtures_basic_module_types
6364

6465
cimport test.fixtures.basic.module.services_interface as _fbthrift_services_interface
@@ -604,7 +605,7 @@ async def FB303Service_simple_rpc_coro(
604605
cTApplicationExceptionType__UNKNOWN, (f'Application was cancelled on the server with message: {str(ex)}').encode('UTF-8')
605606
))
606607
else:
607-
promise.cPromise.setValue(make_unique[_test_fixtures_basic_module_cbindings.cReservedKeyword](deref((<_test_fixtures_basic_module_types.ReservedKeyword?> result)._cpp_obj_FBTHRIFT_ONLY_DO_NOT_USE)))
608+
promise.cPromise.setValue(make_unique[_test_fixtures_basic_module_cbindings.cReservedKeyword](_test_fixtures_basic_module_thrift_converter.ReservedKeyword_convert_to_cpp(result._to_python())))
608609

609610
async def FB303Service_onStartServing_coro(
610611
object self,

thrift/compiler/test/fixtures/py3/out/py3_inplace/gen-py3/module/services.pyx

+9-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ from thrift.python.common cimport (
4848
MetadataBox as __MetadataBox,
4949
)
5050

51-
from thrift.py3.types cimport make_unique
51+
from thrift.py3.types cimport make_unique, deref_const
5252

5353
cimport folly.futures
5454
from folly.executor cimport get_executor
@@ -59,6 +59,7 @@ from folly.memory cimport to_shared_ptr as __to_shared_ptr
5959

6060
cimport module.types as _module_types
6161
cimport module.cbindings as _module_cbindings
62+
cimport module.thrift_converter as _module_thrift_converter
6263
import module.types as _module_types
6364

6465
cimport module.services_interface as _fbthrift_services_interface
@@ -995,7 +996,7 @@ cdef api void call_cy_SimpleService_get_value(
995996
unique_ptr[_module_cbindings.cSimpleStruct] simple_struct
996997
) noexcept:
997998
cdef Promise_cint32_t __promise = Promise_cint32_t._fbthrift_create(cmove(cPromise))
998-
arg_simple_struct = _module_types.SimpleStruct._create_FBTHRIFT_ONLY_DO_NOT_USE(shared_ptr[_module_cbindings.cSimpleStruct](simple_struct.release()))
999+
arg_simple_struct = _module_thrift_converter.SimpleStruct_from_cpp(deref_const(simple_struct))._to_py3()
9991000
__context = RequestContext._fbthrift_create(ctx)
10001001
__context_token = __THRIFT_REQUEST_CONTEXT.set(__context)
10011002
asyncio.get_event_loop().create_task(
@@ -1319,7 +1320,7 @@ cdef api void call_cy_SimpleService_complex_sum_i32(
13191320
unique_ptr[_module_cbindings.cComplexStruct] counter
13201321
) noexcept:
13211322
cdef Promise_cint32_t __promise = Promise_cint32_t._fbthrift_create(cmove(cPromise))
1322-
arg_counter = _module_types.ComplexStruct._create_FBTHRIFT_ONLY_DO_NOT_USE(shared_ptr[_module_cbindings.cComplexStruct](counter.release()))
1323+
arg_counter = _module_thrift_converter.ComplexStruct_from_cpp(deref_const(counter))._to_py3()
13231324
__context = RequestContext._fbthrift_create(ctx)
13241325
__context_token = __THRIFT_REQUEST_CONTEXT.set(__context)
13251326
asyncio.get_event_loop().create_task(
@@ -1337,7 +1338,7 @@ cdef api void call_cy_SimpleService_repeat_name(
13371338
unique_ptr[_module_cbindings.cComplexStruct] counter
13381339
) noexcept:
13391340
cdef Promise_string __promise = Promise_string._fbthrift_create(cmove(cPromise))
1340-
arg_counter = _module_types.ComplexStruct._create_FBTHRIFT_ONLY_DO_NOT_USE(shared_ptr[_module_cbindings.cComplexStruct](counter.release()))
1341+
arg_counter = _module_thrift_converter.ComplexStruct_from_cpp(deref_const(counter))._to_py3()
13411342
__context = RequestContext._fbthrift_create(ctx)
13421343
__context_token = __THRIFT_REQUEST_CONTEXT.set(__context)
13431344
asyncio.get_event_loop().create_task(
@@ -1643,7 +1644,7 @@ cdef api void call_cy_SimpleService_get_binary_union_struct(
16431644
unique_ptr[_module_cbindings.cBinaryUnion] u
16441645
) noexcept:
16451646
cdef Promise__module_cbindings_cBinaryUnionStruct __promise = Promise__module_cbindings_cBinaryUnionStruct._fbthrift_create(cmove(cPromise))
1646-
arg_u = _module_types.BinaryUnion._create_FBTHRIFT_ONLY_DO_NOT_USE(shared_ptr[_module_cbindings.cBinaryUnion](u.release()))
1647+
arg_u = _module_thrift_converter.BinaryUnion_from_cpp(deref_const(u))._to_py3()
16471648
__context = RequestContext._fbthrift_create(ctx)
16481649
__context_token = __THRIFT_REQUEST_CONTEXT.set(__context)
16491650
asyncio.get_event_loop().create_task(
@@ -1981,7 +1982,7 @@ async def SimpleService_expected_exception_coro(
19811982
try:
19821983
result = await self.expected_exception()
19831984
except _module_types.SimpleException as ex:
1984-
promise.cPromise.setException(deref((<_module_types.SimpleException> ex)._cpp_obj_FBTHRIFT_ONLY_DO_NOT_USE))
1985+
promise.cPromise.setException(_module_thrift_converter.SimpleException_convert_to_cpp(ex._to_python()))
19851986
except __ApplicationError as ex:
19861987
# If the handler raised an ApplicationError convert it to a C++ one
19871988
promise.cPromise.setException(cTApplicationException(
@@ -2422,7 +2423,7 @@ async def SimpleService_get_struct_coro(
24222423
cTApplicationExceptionType__UNKNOWN, (f'Application was cancelled on the server with message: {str(ex)}').encode('UTF-8')
24232424
))
24242425
else:
2425-
promise.cPromise.setValue(make_unique[_module_cbindings.cSimpleStruct](deref((<_module_types.SimpleStruct?> result)._cpp_obj_FBTHRIFT_ONLY_DO_NOT_USE)))
2426+
promise.cPromise.setValue(make_unique[_module_cbindings.cSimpleStruct](_module_thrift_converter.SimpleStruct_convert_to_cpp(result._to_python())))
24262427

24272428
async def SimpleService_fib_coro(
24282429
object self,
@@ -2914,7 +2915,7 @@ async def SimpleService_get_binary_union_struct_coro(
29142915
cTApplicationExceptionType__UNKNOWN, (f'Application was cancelled on the server with message: {str(ex)}').encode('UTF-8')
29152916
))
29162917
else:
2917-
promise.cPromise.setValue(make_unique[_module_cbindings.cBinaryUnionStruct](deref((<_module_types.BinaryUnionStruct?> result)._cpp_obj_FBTHRIFT_ONLY_DO_NOT_USE)))
2918+
promise.cPromise.setValue(make_unique[_module_cbindings.cBinaryUnionStruct](_module_thrift_converter.BinaryUnionStruct_convert_to_cpp(result._to_python())))
29182919

29192920
async def SimpleService_onStartServing_coro(
29202921
object self,

thrift/compiler/test/fixtures/types/out/py3_inplace/gen-py3/apache/thrift/fixtures/types/module/services.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ from thrift.python.common cimport (
4848
MetadataBox as __MetadataBox,
4949
)
5050

51-
from thrift.py3.types cimport make_unique
51+
from thrift.py3.types cimport make_unique, deref_const
5252

5353
cimport folly.futures
5454
from folly.executor cimport get_executor
@@ -59,10 +59,12 @@ from folly.memory cimport to_shared_ptr as __to_shared_ptr
5959

6060
cimport apache.thrift.fixtures.types.module.types as _apache_thrift_fixtures_types_module_types
6161
cimport apache.thrift.fixtures.types.module.cbindings as _apache_thrift_fixtures_types_module_cbindings
62+
cimport apache.thrift.fixtures.types.module.thrift_converter as _apache_thrift_fixtures_types_module_thrift_converter
6263
import apache.thrift.fixtures.types.module.types as _apache_thrift_fixtures_types_module_types
6364
import apache.thrift.fixtures.types.included.types as _apache_thrift_fixtures_types_included_types
6465
cimport apache.thrift.fixtures.types.included.types as _apache_thrift_fixtures_types_included_types
6566
cimport apache.thrift.fixtures.types.included.cbindings as _apache_thrift_fixtures_types_included_cbindings
67+
cimport apache.thrift.fixtures.types.included.thrift_converter as _apache_thrift_fixtures_types_included_thrift_converter
6668

6769
cimport apache.thrift.fixtures.types.module.services_interface as _fbthrift_services_interface
6870

thrift/lib/py3/types.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ std::shared_ptr<T> constant_shared_ptr(const T& x) {
3737
return std::shared_ptr<T>(std::shared_ptr<T>{}, const_cast<T*>(&x));
3838
}
3939

40+
template <typename T>
41+
const T& deref_const(const std::unique_ptr<T>& ptr) {
42+
return *ptr;
43+
}
44+
4045
// the folly::remove_cvref_t conversions work around Cython's limitation on
4146
// const/reference qualifier when custom cpp.type with those are defined
4247
template <typename T, typename S>

thrift/lib/py3/types.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ cdef extern from "thrift/lib/py3/types.h" namespace "::thrift::py3" nogil:
5050
string_view get_field_name_by_index[T](size_t idx) except +
5151
object init_unicode_from_cpp(...)
5252
T* get_union_field_value[T](...) except +
53+
const T& deref_const[T](const unique_ptr[T]& ptr)
5354

5455
cdef cppclass set_iter[T]:
5556
set_iter()

0 commit comments

Comments
 (0)