[ https://issues.apache.org/jira/browse/THRIFT-2642?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14970521#comment-14970521 ]
Eric Conner edited comment on THRIFT-2642 at 10/23/15 6:21 AM: --------------------------------------------------------------- The problem is in thrift_spec which is used only by fastbinary. Right now the only option seems to be to manually modify the code as Julien has suggested. As far as fixing this for fastbinary it gets tricky. 1) The thrift_spec either needs to move from the class level into a method or thrift_spec should be added to the class after it is defined. 2) As written the thrift_spec tuple tries to be self-referential which is not really possible in python unless you create lots of inner copies of the tuple. It'd work in C with a pointer back to the object since fastbinary is really just reading the tuple to see what fields to expect. It may be possible to modify fastbinary to handle this case specifically, but the generated python would also need to change as well and its not clear to me how to do that. Maybe just using some sentinel value that fastbinary looks for which means "ref to my parent". was (Author: econner724): The problem is in thrift_spec which is used only by fastbinary. Right now the only option seems to be to manually modify the code as Julien has suggested: A.thrift_spec = None for idx in xrange(10): A.thrift_spec = ( 1, TType.STRUCT, 'field', (A, A.thrift_spec), None, ) ) As far as fixing this for fastbinary it gets tricky. 1) The thrift_spec either needs to move from the class level into a method or thrift_spec should be added to the class after it is defined. 2) As written the thrift_spec tuple tries to be self-referential which is not really possible in python unless you create lots of inner copies of the tuple. It'd work in C with a pointer back to the object since fastbinary is really just reading the tuple to see what fields to expect. It may be possible to modify fastbinary to handle this case specifically, but the generated python would also need to change as well and its not clear to me how to do that. Maybe just using some sentinel value that fastbinary looks for which means "ref to my parent". > 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 > > 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.3.4#6332)