[ 
https://issues.apache.org/jira/browse/THRIFT-2642?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16056824#comment-16056824
 ] 

Eric Conner commented on THRIFT-2642:
-------------------------------------

Hi everyone,

I finally had some time to take a stab at fixing this.  

I more or less followed the fbthrift approach with a few modifications.  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 thrift_spec to (<Struct>, None) 
# Uses a method call ``fix_spec`` to fill in the spec to (<Struct>, 
<Struct>.thrift_spec)

The fbthrift approach is similar except their struct definition uses a list 
[<Struct>, <Struct>.thrift_spec].  I looked at this route for awhile, but 
Apache Thrift's fastbinary implementation depends pretty heavily on 
``thrift_spec`` being immutable.  There are ref counting assumptions I don't 
fully understand that led to lots of complications when I tried to change 
things to use lists (see 
https://github.com/apache/thrift/blob/19baeefd8c38d62085891d7956349601f79448b3/lib/py/src/ext/protocol.tcc#L341).

Instead, I'm using a modified version of fbthrift's fix_spec script 
(https://github.com/facebook/fbthrift/blob/3ef868c969464e935b39e336807af1486b2739c3/thrift/lib/py/util/Recursive.py).
  The main change I made was to convert each spec to a list, modify the list 
recursively, and then convert the list back to a tuple.  I'm unsure of the 
performance hit this adds, but I figured it wouldn't be too bad since it only 
happens at import time.

 
 







> 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)

Reply via email to