thomasbruggink opened a new pull request, #3753:
URL: https://github.com/apache/thrift/pull/3753

   <!-- Explain the changes in the pull request below: -->
   Fix the bug where the following:
   ```
   struct C {
       1: i32 value
   }
   
   struct A {
       1: C nested = {}
   }
   
   struct B {
       1: A itm = {}
   }
   ```
   would generate the following invalid python code:
   ```
   class C:
       thrift_spec = None
   
       def __init__(self, value=None):
           self.value = value
   
   class A:
       thrift_spec = None
   
       def __init__(self, nested=C()):
           if nested is self.thrift_spec[1][4]:
               nested = C()
           self.nested = nested
   
   class B:
       thrift_spec = None
   
       def __init__(self, itm=A()):
           if itm is self.thrift_spec[1][4]:
               itm = A()
           self.itm = itm
   ```
   The problem with the above code is that `self.thrift_spec` was initialized 
and set after the module was loaded. In python this would work when B is 
instantiated but since B immediately invokes A() it happens before the 
thrift_spec is set and this would result in `TypeError: 'NoneType' object is 
not subscriptable`. By first initializing a temporary placeholder with a unique 
address to compare to called `_THRIFT_DEFAULT` the generated code would look 
like:
   ```
   _THRIFT_DEFAULT = object()
   
   class C:
       def __init__(self, value=None):
           self.value = value
   
   class A:
       def __init__(self, nested=_THRIFT_DEFAULT):
           if nested is _THRIFT_DEFAULT:
               nested = C()
   
   class B:
       def __init__(self, itm=_THRIFT_DEFAULT):
           if itm is _THRIFT_DEFAULT:
               itm = A()
   ```
   Here the instantiation of the default object is defered until after the 
module has been fully loaded resulting in the correct value being assigned.
     
   
   <!-- We recommend you review the checklist/tips before submitting a pull 
request. -->
   
   - [x] Did you create an [Apache 
Jira](https://issues.apache.org/jira/projects/THRIFT/issues/) ticket?  
([Request account here](https://selfserve.apache.org/jira-account.html), not 
required for trivial changes)
   - [x] If a ticket exists: Does your pull request title follow the pattern 
"THRIFT-NNNN: describe my issue"?
   - [x] Did you squash your changes to a single commit?  (not required, but 
preferred)
   - [x] Did you do your best to avoid breaking changes?  If one was needed, 
did you label the Jira ticket with "Breaking-Change"?
   - [x] If your change does not involve any code, include `[skip ci]` anywhere 
in the commit message to free up build resources.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to