Commit 8049ffd
committed
THRIFT-4623 Fix python complex nested struct includes
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.1 parent 405a95f commit 8049ffd
4 files changed
Lines changed: 100 additions & 4 deletions
File tree
- compiler/cpp/src/thrift/generate
- lib/py
- test/test_compiler
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
| 455 | + | |
455 | 456 | | |
456 | 457 | | |
457 | 458 | | |
| |||
907 | 908 | | |
908 | 909 | | |
909 | 910 | | |
910 | | - | |
| 911 | + | |
911 | 912 | | |
912 | | - | |
913 | | - | |
| 913 | + | |
914 | 914 | | |
915 | 915 | | |
916 | 916 | | |
| |||
1319 | 1319 | | |
1320 | 1320 | | |
1321 | 1321 | | |
| 1322 | + | |
1322 | 1323 | | |
1323 | 1324 | | |
1324 | 1325 | | |
| |||
2784 | 2785 | | |
2785 | 2786 | | |
2786 | 2787 | | |
2787 | | - | |
| 2788 | + | |
| 2789 | + | |
| 2790 | + | |
| 2791 | + | |
| 2792 | + | |
| 2793 | + | |
2788 | 2794 | | |
2789 | 2795 | | |
2790 | 2796 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
0 commit comments