Skip to content

Commit

Permalink
migrate const to python [1/4]
Browse files Browse the repository at this point in the history
Summary:
Constants are proving problematic in the py3 `types.py` migration diff. Let's implement in non-cython so they can be moved out of `types.pyx`

Planned structure:
1. This diff: migrate terminals: strings and enums
2. Re-factor to allow recursive calls with mustache template
3. Containers
4. Structs

Reviewed By: createdbysk

Differential Revision: D66677805

fbshipit-source-id: 959acd7147f0cec31a46fe220bb327564ab8e488
  • Loading branch information
ahilger authored and facebook-github-bot committed Dec 4, 2024
1 parent d96ea97 commit 54c8b25
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ file.
}}{{/constant:value}}
{{/type:float?}}
{{#type:string?}}
{{constant:name}} = {{> types/current_module_cbindings}}.{{> types/c_constant }}().decode('UTF-8')
{{constant:name}} = {{#constant:value}}"{{value:string_value}}"{{/constant:value}}
{{/type:string?}}
{{#type:binary?}}
{{constant:name}} = <bytes> {{> types/current_module_cbindings}}.{{> types/c_constant }}()
{{constant:name}} = {{#constant:value}}b"{{value:string_value}}"{{/constant:value}}
{{/type:binary?}}
{{! We take the constant structs/containers and build a non-const shared_ptr
as required by the thrift-py3 wrapper api, generally this is a terrible
Expand All @@ -71,7 +71,11 @@ file.
{{constant:name}} = {{> types/container_from_cpp }}({{> types/current_module_cbindings}}.{{> types/c_constant }}())
{{/type:container?}}
{{#type:enum}}
{{constant:name}} = {{#type:need_module_path?}}{{type:modulePath}}.{{!
}}{{/type:need_module_path?}}{{enum:name}}(<int> ({{> types/current_module_cbindings}}.{{> types/c_constant }}()))
{{constant:name}} = {{#constant:value}}{{!
}}{{^value:enum_value?}}__BadEnum({{/value:enum_value?}}{{!
}}{{#type:need_module_path?}}{{type:modulePath}}.{{/type:need_module_path?}}{{!
}}{{value:enum_name}}{{#value:enum_value}}.{{enum_value:py_name}}{{/value:enum_value}}{{!
}}{{^value:enum_value?}}, {{value:integer_value}}){{/value:enum_value?}}{{!
}}{{/constant:value}}
{{/type:enum}}
{{/constant:type}}
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,5 @@ cdef object Map__MyEnum_string__from_cpp(const cmap[_test_fixtures_enumstrict_mo
return Map__MyEnum_string(py_items, private_ctor_token=thrift.py3.types._fbthrift_map_private_ctor)


kOne = MyEnum(<int> (_test_fixtures_enumstrict_module_cbindings.ckOne()))
kOne = MyEnum.ONE
enumNames = Map__MyEnum_string__from_cpp(_test_fixtures_enumstrict_module_cbindings.cenumNames())
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ OFFSET = -10
COUNT = 200
MASK = 16388846
E = 2.718281828459
DATE = _test_fixtures_basic_module_cbindings.cDATE().decode('UTF-8')
DATE = "June 28, 2017"
AList = List__i32__from_cpp(_test_fixtures_basic_module_cbindings.cAList())
ASet = Set__string__from_cpp(_test_fixtures_basic_module_cbindings.cASet())
AMap = Map__string_List__i32__from_cpp(_test_fixtures_basic_module_cbindings.cAMap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,17 +1548,17 @@ cdef object Map__i32_bool__from_cpp(const cmap[cint32_t,cbool]& c_map) except *:


myInt = 1337
name = _module_cbindings.cname().decode('UTF-8')
multi_line_string = _module_cbindings.cmulti_line_string().decode('UTF-8')
name = "Mark Zuckerberg"
multi_line_string = "This\nis a\nmulti line string.\n"
states = List__Map__string_i32__from_cpp(_module_cbindings.cstates())
x = 1
y = 1000000.0
z = 1000000000
zeroDoubleValue = 0
longDoubleValue = 2.59961000990301e-05
bin = <bytes> _module_cbindings.cbin()
my_company = Company(<int> (_module_cbindings.cmy_company()))
foo = _module_cbindings.cfoo().decode('UTF-8')
bin = b"a\000z"
my_company = Company.FACEBOOK
foo = "foo"
bar = 42
mymap = Map__string_string__from_cpp(_module_cbindings.cmymap())
my_apps = List__Company__from_cpp(_module_cbindings.cmy_apps())
Expand All @@ -1585,11 +1585,11 @@ u_2_3 = union2._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbin
u_2_4 = union2._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbindings.cu_2_4()))
u_2_5 = union2._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbindings.cu_2_5()))
u_2_6 = union2._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbindings.cu_2_6()))
apostrophe = _module_cbindings.capostrophe().decode('UTF-8')
tripleApostrophe = _module_cbindings.ctripleApostrophe().decode('UTF-8')
quotationMark = _module_cbindings.cquotationMark().decode('UTF-8')
backslash = _module_cbindings.cbackslash().decode('UTF-8')
escaped_a = _module_cbindings.cescaped_a().decode('UTF-8')
apostrophe = "'"
tripleApostrophe = "'''"
quotationMark = "\""
backslash = "\\"
escaped_a = "a"
char2ascii = Map__string_i32__from_cpp(_module_cbindings.cchar2ascii())
escaped_strings = List__string__from_cpp(_module_cbindings.cescaped_strings())
false_c = False
Expand All @@ -1599,7 +1599,7 @@ zero16 = 0
zero32 = 0
zero64 = 0
zero_dot_zero = 0
empty_string = _module_cbindings.cempty_string().decode('UTF-8')
empty_string = ""
empty_int_list = List__i32__from_cpp(_module_cbindings.cempty_int_list())
empty_string_list = List__string__from_cpp(_module_cbindings.cempty_string_list())
empty_int_set = Set__i32__from_cpp(_module_cbindings.cempty_int_set())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4816,7 +4816,7 @@ a32BitInt = 123
a64BitInt = 1234
aFloat = 0.1
aDouble = 0.12
aString = _module_cbindings.caString().decode('UTF-8')
aString = "Joe Doe"
aList = List__bool__from_cpp(_module_cbindings.caList())
anEmptyMap = Map__string_i32__from_cpp(_module_cbindings.canEmptyMap())
aMap = Map__i32_string__from_cpp(_module_cbindings.caMap())
Expand All @@ -4826,8 +4826,8 @@ states = List__Map__string_i32__from_cpp(_module_cbindings.cstates())
AConstList = List__MyEnumA__from_cpp(_module_cbindings.cAConstList())
AnIntegerEnum2 = 2
ListOfIntsFromEnums = List__i32__from_cpp(_module_cbindings.cListOfIntsFromEnums())
constEnumA = MyEnumA(<int> (_module_cbindings.cconstEnumA()))
constEnumB = MyEnumA(<int> (_module_cbindings.cconstEnumB()))
constEnumA = MyEnumA.fieldB
constEnumB = __BadEnum(MyEnumA, 3)
AStruct = _includes_types.AStruct
CustomProtocolType = _fbthrift_iobuf.IOBuf
simpleTypeDef = int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2232,8 +2232,8 @@ A_NUMBER = 84
A_BIG_NUMBER = 102
A_REAL_NUMBER = 3.14
A_FAKE_NUMBER = 3.0
A_WORD = _module_cbindings.cA_WORD().decode('UTF-8')
SOME_BYTES = <bytes> _module_cbindings.cSOME_BYTES()
A_WORD = "Good word"
SOME_BYTES = b"bytes"
A_STRUCT = SimpleStruct._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbindings.cA_STRUCT()))
EMPTY = SimpleStruct._create_FBTHRIFT_ONLY_DO_NOT_USE(constant_shared_ptr(_module_cbindings.cEMPTY()))
WORD_LIST = List__string__from_cpp(_module_cbindings.cWORD_LIST())
Expand Down

0 comments on commit 54c8b25

Please sign in to comment.