[ https://issues.apache.org/jira/browse/THRIFT-2642?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16075532#comment-16075532 ]
ASF GitHub Bot commented on THRIFT-2642: ---------------------------------------- Github user juliengreard commented on a diff in the pull request: https://github.com/apache/thrift/pull/1293#discussion_r125757570 --- Diff: compiler/cpp/src/thrift/generate/t_py_generator.cc --- @@ -634,6 +659,49 @@ void t_py_generator::generate_py_struct(t_struct* tstruct, bool is_exception) { generate_py_struct_definition(f_types_, tstruct, is_exception); } + +/** + * Generate the thrift_spec for a struct + * e.g. (4, TType.LIST, 'struct_list', (TType.STRUCT, (RandomStuff, None), False), None, ), # 4 + */ +void t_py_generator::generate_py_thrift_spec(ofstream& out, + t_struct* tstruct, + bool /*is_exception*/) { + const vector<t_field*>& sorted_members = tstruct->get_sorted_members(); + vector<t_field*>::const_iterator m_iter; + + // Add struct definition to list so thrift_spec can be fixed for recursive structures. + indent(out) << "all_structs.append(" << tstruct->get_name() << ")" << endl; + + if (sorted_members.empty() || (sorted_members[0]->get_key() >= 0)) { + indent(out) << tstruct->get_name() << ".thrift_spec = (" << endl; + indent_up(); + + int sorted_keys_pos = 0; + for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) { + + for (; sorted_keys_pos != (*m_iter)->get_key(); sorted_keys_pos++) { + indent(out) << "None, # " << sorted_keys_pos << endl; + } + + indent(out) << "(" << (*m_iter)->get_key() << ", " << type_to_enum((*m_iter)->get_type()) --- End diff -- a comment showing an example of the python code generated from this peace of code would be a plus > Recursive structs don't work in python > -------------------------------------- > > Key: THRIFT-2642 > URL: https://issues.apache.org/jira/browse/THRIFT-2642 > Project: Thrift > Issue Type: Bug > Components: Python - Compiler, Python - Library > Affects Versions: 0.9.2 > Reporter: Igor Kostenko > Assignee: Eric Conner > > Recursive structs in 0.9.2 work fine in c++ & c#, but not in python, because > generated code trying to use objects which not constructed yet. > Struct: > {code} > struct Recursive { > 1: list<Recursive> Children > } > {code} > Python code: > {code} > class Recursive: > thrift_spec = ( > None, # 0 > (1, TType.LIST, 'Children', (TType.STRUCT,(Recursive, > Recursive.thrift_spec)), None, ), # 1 > ) > {code} > Error message: > {code} > Traceback (most recent call last): > File "ttypes.py", line 20, in <module> > class Recursive: > File "ttypes.py", line 28, in Recursive > (1, TType.LIST, 'Children', (TType.STRUCT,(Recursive, > Recursive.thrift_spec)), None, ), # 1 > NameError: name 'Recursive' is not defined > {code} -- This message was sent by Atlassian JIRA (v6.4.14#64029)