From 54c8b2583fe45225a719204e681353f960e43489 Mon Sep 17 00:00:00 2001 From: Andrew Hilger Date: Wed, 4 Dec 2024 09:56:08 -0800 Subject: [PATCH] migrate const to python [1/4] 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 --- .../py3/types/constant_value.mustache | 12 ++++++---- .../test/fixtures/enumstrict/module/types.pyx | 2 +- .../test/fixtures/basic/module/types.pyx | 2 +- .../out/py3/gen-py3/module/types.pyx | 22 +++++++++---------- .../out/py3/gen-py3/module/types.pyx | 6 ++--- .../py3/out/py3/gen-py3/module/types.pyx | 4 ++-- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/thrift/compiler/generate/templates/py3/types/constant_value.mustache b/thrift/compiler/generate/templates/py3/types/constant_value.mustache index ba885060e12..ea5f7898d73 100644 --- a/thrift/compiler/generate/templates/py3/types/constant_value.mustache +++ b/thrift/compiler/generate/templates/py3/types/constant_value.mustache @@ -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}} = {{> 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 @@ -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}}( ({{> 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}} diff --git a/thrift/compiler/test/fixtures/basic-enum/out/py3/gen-py3/test/fixtures/enumstrict/module/types.pyx b/thrift/compiler/test/fixtures/basic-enum/out/py3/gen-py3/test/fixtures/enumstrict/module/types.pyx index f0dd8891467..27559d220f5 100644 --- a/thrift/compiler/test/fixtures/basic-enum/out/py3/gen-py3/test/fixtures/enumstrict/module/types.pyx +++ b/thrift/compiler/test/fixtures/basic-enum/out/py3/gen-py3/test/fixtures/enumstrict/module/types.pyx @@ -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( (_test_fixtures_enumstrict_module_cbindings.ckOne())) +kOne = MyEnum.ONE enumNames = Map__MyEnum_string__from_cpp(_test_fixtures_enumstrict_module_cbindings.cenumNames()) diff --git a/thrift/compiler/test/fixtures/basic/out/py3/gen-py3/test/fixtures/basic/module/types.pyx b/thrift/compiler/test/fixtures/basic/out/py3/gen-py3/test/fixtures/basic/module/types.pyx index f4fd82a2f62..ea7a1517eeb 100644 --- a/thrift/compiler/test/fixtures/basic/out/py3/gen-py3/test/fixtures/basic/module/types.pyx +++ b/thrift/compiler/test/fixtures/basic/out/py3/gen-py3/test/fixtures/basic/module/types.pyx @@ -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()) diff --git a/thrift/compiler/test/fixtures/constants/out/py3/gen-py3/module/types.pyx b/thrift/compiler/test/fixtures/constants/out/py3/gen-py3/module/types.pyx index 34f8c1b3e0f..5998be922b8 100644 --- a/thrift/compiler/test/fixtures/constants/out/py3/gen-py3/module/types.pyx +++ b/thrift/compiler/test/fixtures/constants/out/py3/gen-py3/module/types.pyx @@ -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 = _module_cbindings.cbin() -my_company = Company( (_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()) @@ -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 @@ -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()) diff --git a/thrift/compiler/test/fixtures/mcpp2-compare/out/py3/gen-py3/module/types.pyx b/thrift/compiler/test/fixtures/mcpp2-compare/out/py3/gen-py3/module/types.pyx index d170c5afb61..205e83c6de3 100644 --- a/thrift/compiler/test/fixtures/mcpp2-compare/out/py3/gen-py3/module/types.pyx +++ b/thrift/compiler/test/fixtures/mcpp2-compare/out/py3/gen-py3/module/types.pyx @@ -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()) @@ -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( (_module_cbindings.cconstEnumA())) -constEnumB = MyEnumA( (_module_cbindings.cconstEnumB())) +constEnumA = MyEnumA.fieldB +constEnumB = __BadEnum(MyEnumA, 3) AStruct = _includes_types.AStruct CustomProtocolType = _fbthrift_iobuf.IOBuf simpleTypeDef = int diff --git a/thrift/compiler/test/fixtures/py3/out/py3/gen-py3/module/types.pyx b/thrift/compiler/test/fixtures/py3/out/py3/gen-py3/module/types.pyx index 5e788aa7e1d..fd52447b7eb 100644 --- a/thrift/compiler/test/fixtures/py3/out/py3/gen-py3/module/types.pyx +++ b/thrift/compiler/test/fixtures/py3/out/py3/gen-py3/module/types.pyx @@ -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 = _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())