[
https://issues.apache.org/jira/browse/THRIFT-2642?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16056824#comment-16056824
]
Eric Conner edited comment on THRIFT-2642 at 6/21/17 5:13 PM:
--------------------------------------------------------------
Hi everyone,
I finally had some time to take a stab at fixing this.
I followed the fbthrift approach. At a high level, the change:
# Moves all thrift_spec definitions to the end of the ttypes file. Or, in the
case of a service, after the service definition.
# Changes the initial declaration of the spec for a TStruct to [<Struct>, None]
# Uses a method call ``fix_spec`` to fill in the spec to [<Struct>,
<Struct>.thrift_spec]
*Edit*:
Originally I had tried to keep spec as a tuple, but as far as I can tell it has
to be a list as it needs to be a reference type for recursive structures to
work. I added tests to SerializationTest.py. Note that co-recursive structures
do not work. There is an infinite loop when they are serialized. fbthrift also
suffers from this problem.
Thanks [~lyschoening], [~myTalala], [~isanych], [~juliengreard], [~jensg]
was (Author: econner724):
Hi everyone,
I finally had some time to take a stab at fixing this.
I followed the fbthrift approach. At a high level, the change:
# Moves all thrift_spec definitions to the end of the ttypes or service file.
# Changes the initial declaration of the spec for a TStruct to [<Struct>, None]
# Uses a method call ``fix_spec`` to fill in the spec to [<Struct>,
<Struct>.thrift_spec]
*Edit*:
Originally I had tried to keep spec as a tuple, but as far as I can tell it has
to be a list as it needs to be a reference type for recursive structures to
work. I added tests to SerializationTest.py. Note that co-recursive structures
do not work. There is an infinite loop when they are serialized. fbthrift also
suffers from this problem.
Thanks [~lyschoening], [~myTalala], [~isanych], [~juliengreard], [~jensg]
> 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.4.14#64029)