Skip to content

Commit

Permalink
make py3 struct converters wrap python struct converters
Browse files Browse the repository at this point in the history
Summary: The initial implementation copied too much logic from the thrift-python converter modules. It's cleaner to just use `_convert_from_cpp` from thrift-python `types_converter` module, since all types in auto-migrate are thrift-python.

Reviewed By: Filip-F

Differential Revision: D68135029

fbshipit-source-id: 202ae4f796a5977870807f7cbc18a7229d0d260f
  • Loading branch information
ahilger authored and facebook-github-bot committed Jan 15, 2025
1 parent 3a94129 commit b744781
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 107 deletions.
21 changes: 9 additions & 12 deletions thrift/compiler/generate/templates/py3/converter.pyx.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

{{#program:auto_migrate?}}
from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_converter as {{!
}}_{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_converter
{{#program:gen_legacy_container_converters?}}
from libcpp.utility cimport move as cmove

import {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types as {{> types/current_module_types}}
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_converter as {{!
}}_{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_converter
{{#program:includeNamespaces}}
{{#hasTypes?}}
import {{#includeNamespace}}{{value}}.{{/includeNamespace}}types as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}types
Expand All @@ -41,19 +41,15 @@ cimport {{#includeNamespace}}{{value}}.{{/includeNamespace}}thrift_converter as
cimport {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types as _fbthrift_ctypes
{{/program:auto_migrate?}}

{{!
Include Constructor and Extractor specializations.
}}
{{#program:auto_migrate?}}
cdef extern from "{{program:includePrefix}}gen-python-capi/{{program:name}}/thrift_types_capi.h":
pass
{{/program:auto_migrate?}}

{{#program:filtered_structs}}
{{! direct replacement for ._cpp_obj }}
cdef shared_ptr[_fbthrift_cbindings.{{> types/c_struct }}] {{struct:name}}_convert_to_cpp(object inst) except*:
{{#program:auto_migrate?}}
return make_shared[_fbthrift_cbindings.{{> types/c_struct }}](python_to_cpp[_fbthrift_cbindings.{{> types/c_struct }}](inst))
return make_shared[_fbthrift_cbindings.{{> types/c_struct }}](
_{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_converter.{{!
}}{{struct:name}}_convert_to_cpp(inst)
)
{{/program:auto_migrate?}}
{{^program:auto_migrate?}}
return (<_fbthrift_ctypes.{{struct:name}}?>inst).{{> types/cpp_obj}}
Expand All @@ -62,7 +58,8 @@ cdef shared_ptr[_fbthrift_cbindings.{{> types/c_struct }}] {{struct:name}}_conve
{{! direct replacement for ._fbthrift_create }}
cdef object {{struct:name}}_from_cpp(const shared_ptr[_fbthrift_cbindings.{{> types/c_struct }}]& c_struct):
{{#program:auto_migrate?}}
return cpp_to_python[_fbthrift_cbindings.{{> types/c_struct }}](deref(c_struct))
return _{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_converter.{{!
}}{{struct:name}}_from_cpp(deref(const_pointer_cast(c_struct)))
{{/program:auto_migrate?}}
{{^program:auto_migrate?}}
return _fbthrift_ctypes.{{struct:name}}._create_FBTHRIFT_ONLY_DO_NOT_USE(c_struct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport includes.thrift_converter as _includes_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/includes/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cIncluded] Included_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cIncluded](python_to_cpp[_fbthrift_cbindings.cIncluded](inst))
return make_shared[_fbthrift_cbindings.cIncluded](
_includes_thrift_converter.Included_convert_to_cpp(inst)
)
cdef object Included_from_cpp(const shared_ptr[_fbthrift_cbindings.cIncluded]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cIncluded](deref(c_struct))
return _includes_thrift_converter.Included_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport module.thrift_converter as _module_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/module/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cMyStruct] MyStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cMyStruct](python_to_cpp[_fbthrift_cbindings.cMyStruct](inst))
return make_shared[_fbthrift_cbindings.cMyStruct](
_module_thrift_converter.MyStruct_convert_to_cpp(inst)
)
cdef object MyStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cMyStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cMyStruct](deref(c_struct))
return _module_thrift_converter.MyStruct_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport transitive.thrift_converter as _transitive_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/transitive/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cFoo] Foo_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cFoo](python_to_cpp[_fbthrift_cbindings.cFoo](inst))
return make_shared[_fbthrift_cbindings.cFoo](
_transitive_thrift_converter.Foo_convert_to_cpp(inst)
)
cdef object Foo_from_cpp(const shared_ptr[_fbthrift_cbindings.cFoo]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cFoo](deref(c_struct))
return _transitive_thrift_converter.Foo_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport includes.thrift_converter as _includes_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/includes/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cIncluded] Included_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cIncluded](python_to_cpp[_fbthrift_cbindings.cIncluded](inst))
return make_shared[_fbthrift_cbindings.cIncluded](
_includes_thrift_converter.Included_convert_to_cpp(inst)
)
cdef object Included_from_cpp(const shared_ptr[_fbthrift_cbindings.cIncluded]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cIncluded](deref(c_struct))
return _includes_thrift_converter.Included_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport transitive.thrift_converter as _transitive_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/transitive/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cFoo] Foo_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cFoo](python_to_cpp[_fbthrift_cbindings.cFoo](inst))
return make_shared[_fbthrift_cbindings.cFoo](
_transitive_thrift_converter.Foo_convert_to_cpp(inst)
)
cdef object Foo_from_cpp(const shared_ptr[_fbthrift_cbindings.cFoo]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cFoo](deref(c_struct))
return _transitive_thrift_converter.Foo_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport includes.thrift_converter as _includes_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/includes/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cIncluded] Included_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cIncluded](python_to_cpp[_fbthrift_cbindings.cIncluded](inst))
return make_shared[_fbthrift_cbindings.cIncluded](
_includes_thrift_converter.Included_convert_to_cpp(inst)
)
cdef object Included_from_cpp(const shared_ptr[_fbthrift_cbindings.cIncluded]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cIncluded](deref(c_struct))
return _includes_thrift_converter.Included_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport module.thrift_converter as _module_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/module/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cMyStruct] MyStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cMyStruct](python_to_cpp[_fbthrift_cbindings.cMyStruct](inst))
return make_shared[_fbthrift_cbindings.cMyStruct](
_module_thrift_converter.MyStruct_convert_to_cpp(inst)
)
cdef object MyStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cMyStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cMyStruct](deref(c_struct))
return _module_thrift_converter.MyStruct_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport service.thrift_converter as _service_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/service/thrift_types_capi.h":
pass


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport transitive.thrift_converter as _transitive_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/transitive/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cFoo] Foo_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cFoo](python_to_cpp[_fbthrift_cbindings.cFoo](inst))
return make_shared[_fbthrift_cbindings.cFoo](
_transitive_thrift_converter.Foo_convert_to_cpp(inst)
)
cdef object Foo_from_cpp(const shared_ptr[_fbthrift_cbindings.cFoo]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cFoo](deref(c_struct))
return _transitive_thrift_converter.Foo_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport transitive.thrift_converter as _transitive_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/includes/gen-python-capi/transitive/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cFoo] Foo_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cFoo](python_to_cpp[_fbthrift_cbindings.cFoo](inst))
return make_shared[_fbthrift_cbindings.cFoo](
_transitive_thrift_converter.Foo_convert_to_cpp(inst)
)
cdef object Foo_from_cpp(const shared_ptr[_fbthrift_cbindings.cFoo]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cFoo](deref(c_struct))
return _transitive_thrift_converter.Foo_from_cpp(deref(const_pointer_cast(c_struct)))


Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,79 @@
#

from libcpp.memory cimport make_shared
from thrift.python.capi.cpp_converter cimport cpp_to_python, python_to_cpp
from cython.operator cimport dereference as deref
from thrift.py3.types cimport const_pointer_cast
cimport module.thrift_converter as _module_thrift_converter

cdef extern from "thrift/compiler/test/fixtures/py3/gen-python-capi/module/thrift_types_capi.h":
pass

cdef shared_ptr[_fbthrift_cbindings.cSimpleException] SimpleException_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cSimpleException](python_to_cpp[_fbthrift_cbindings.cSimpleException](inst))
return make_shared[_fbthrift_cbindings.cSimpleException](
_module_thrift_converter.SimpleException_convert_to_cpp(inst)
)
cdef object SimpleException_from_cpp(const shared_ptr[_fbthrift_cbindings.cSimpleException]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cSimpleException](deref(c_struct))
return _module_thrift_converter.SimpleException_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cOptionalRefStruct] OptionalRefStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cOptionalRefStruct](python_to_cpp[_fbthrift_cbindings.cOptionalRefStruct](inst))
return make_shared[_fbthrift_cbindings.cOptionalRefStruct](
_module_thrift_converter.OptionalRefStruct_convert_to_cpp(inst)
)
cdef object OptionalRefStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cOptionalRefStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cOptionalRefStruct](deref(c_struct))
return _module_thrift_converter.OptionalRefStruct_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cSimpleStruct] SimpleStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cSimpleStruct](python_to_cpp[_fbthrift_cbindings.cSimpleStruct](inst))
return make_shared[_fbthrift_cbindings.cSimpleStruct](
_module_thrift_converter.SimpleStruct_convert_to_cpp(inst)
)
cdef object SimpleStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cSimpleStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cSimpleStruct](deref(c_struct))
return _module_thrift_converter.SimpleStruct_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cHiddenTypeFieldsStruct] HiddenTypeFieldsStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cHiddenTypeFieldsStruct](python_to_cpp[_fbthrift_cbindings.cHiddenTypeFieldsStruct](inst))
return make_shared[_fbthrift_cbindings.cHiddenTypeFieldsStruct](
_module_thrift_converter.HiddenTypeFieldsStruct_convert_to_cpp(inst)
)
cdef object HiddenTypeFieldsStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cHiddenTypeFieldsStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cHiddenTypeFieldsStruct](deref(c_struct))
return _module_thrift_converter.HiddenTypeFieldsStruct_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cComplexStruct] ComplexStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cComplexStruct](python_to_cpp[_fbthrift_cbindings.cComplexStruct](inst))
return make_shared[_fbthrift_cbindings.cComplexStruct](
_module_thrift_converter.ComplexStruct_convert_to_cpp(inst)
)
cdef object ComplexStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cComplexStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cComplexStruct](deref(c_struct))
return _module_thrift_converter.ComplexStruct_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cBinaryUnion] BinaryUnion_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cBinaryUnion](python_to_cpp[_fbthrift_cbindings.cBinaryUnion](inst))
return make_shared[_fbthrift_cbindings.cBinaryUnion](
_module_thrift_converter.BinaryUnion_convert_to_cpp(inst)
)
cdef object BinaryUnion_from_cpp(const shared_ptr[_fbthrift_cbindings.cBinaryUnion]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cBinaryUnion](deref(c_struct))
return _module_thrift_converter.BinaryUnion_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cBinaryUnionStruct] BinaryUnionStruct_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cBinaryUnionStruct](python_to_cpp[_fbthrift_cbindings.cBinaryUnionStruct](inst))
return make_shared[_fbthrift_cbindings.cBinaryUnionStruct](
_module_thrift_converter.BinaryUnionStruct_convert_to_cpp(inst)
)
cdef object BinaryUnionStruct_from_cpp(const shared_ptr[_fbthrift_cbindings.cBinaryUnionStruct]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cBinaryUnionStruct](deref(c_struct))
return _module_thrift_converter.BinaryUnionStruct_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cCustomFields] CustomFields_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cCustomFields](python_to_cpp[_fbthrift_cbindings.cCustomFields](inst))
return make_shared[_fbthrift_cbindings.cCustomFields](
_module_thrift_converter.CustomFields_convert_to_cpp(inst)
)
cdef object CustomFields_from_cpp(const shared_ptr[_fbthrift_cbindings.cCustomFields]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cCustomFields](deref(c_struct))
return _module_thrift_converter.CustomFields_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cCustomTypedefFields] CustomTypedefFields_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cCustomTypedefFields](python_to_cpp[_fbthrift_cbindings.cCustomTypedefFields](inst))
return make_shared[_fbthrift_cbindings.cCustomTypedefFields](
_module_thrift_converter.CustomTypedefFields_convert_to_cpp(inst)
)
cdef object CustomTypedefFields_from_cpp(const shared_ptr[_fbthrift_cbindings.cCustomTypedefFields]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cCustomTypedefFields](deref(c_struct))
return _module_thrift_converter.CustomTypedefFields_from_cpp(deref(const_pointer_cast(c_struct)))

cdef shared_ptr[_fbthrift_cbindings.cAdaptedTypedefFields] AdaptedTypedefFields_convert_to_cpp(object inst) except*:
return make_shared[_fbthrift_cbindings.cAdaptedTypedefFields](python_to_cpp[_fbthrift_cbindings.cAdaptedTypedefFields](inst))
return make_shared[_fbthrift_cbindings.cAdaptedTypedefFields](
_module_thrift_converter.AdaptedTypedefFields_convert_to_cpp(inst)
)
cdef object AdaptedTypedefFields_from_cpp(const shared_ptr[_fbthrift_cbindings.cAdaptedTypedefFields]& c_struct):
return cpp_to_python[_fbthrift_cbindings.cAdaptedTypedefFields](deref(c_struct))
return _module_thrift_converter.AdaptedTypedefFields_from_cpp(deref(const_pointer_cast(c_struct)))


Loading

0 comments on commit b744781

Please sign in to comment.