Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/cpp/src/thrift/generate/t_py_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ void t_py_generator::init_generator() {
} else {
f_types_ << "all_structs = []" << '\n';
}
f_types_ << "_THRIFT_DEFAULT = object()" << '\n';

f_consts_ <<
py_autogen_comment() << '\n' <<
Expand Down Expand Up @@ -907,10 +908,9 @@ void t_py_generator::generate_py_struct_definition(ostream& out,

for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
// Initialize fields
t_type* type = (*m_iter)->get_type();
t_type* type = get_true_type((*m_iter)->get_type());
if (!type->is_base_type() && !type->is_enum() && (*m_iter)->get_value() != nullptr) {
indent(out) << "if " << maybe_escape_identifier((*m_iter)->get_name()) << " is "
<< "self.thrift_spec[" << (*m_iter)->get_key() << "][4]:" << '\n';
indent(out) << "if " << maybe_escape_identifier((*m_iter)->get_name()) << " is _THRIFT_DEFAULT:" << '\n';
indent_up();
indent(out) << maybe_escape_identifier((*m_iter)->get_name()) << " = " << render_field_default_value(*m_iter)
<< '\n';
Expand Down Expand Up @@ -1319,6 +1319,7 @@ void t_py_generator::generate_service(t_service* tservice) {
}

f_service_ << "all_structs = []" << '\n';
f_service_ << "_THRIFT_DEFAULT = object()" << '\n';

// Generate the three main parts of the service
generate_service_interface(tservice);
Expand Down Expand Up @@ -2784,7 +2785,12 @@ string t_py_generator::declare_argument(t_field* tfield) {

result << " = ";
if (tfield->get_value() != nullptr) {
result << render_field_default_value(tfield);
t_type* type = get_true_type(tfield->get_type());
if (!type->is_base_type() && !type->is_enum()) {
result << "_THRIFT_DEFAULT";
} else {
result << render_field_default_value(tfield);
}
} else {
result << "None";
}
Expand Down
1 change: 1 addition & 0 deletions lib/py/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ check-local: all py3-test
$(PYTHON) test/thrift_TNonblockingServer.py
$(PYTHON) test/thrift_TSerializer.py
THRIFT=${THRIFT} $(PYTHON) test/test_compiler/test_keyword_escape.py
THRIFT=${THRIFT} $(PYTHON) test/test_compiler/test_default_struct.py
$(PYTHON) test/test_recursion_depth.py


Expand Down
31 changes: 31 additions & 0 deletions lib/py/test/test_compiler/Thrift4623.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

namespace py thrift4623

struct C {
1: i32 value
}

struct A {
1: C nested = {}
}

struct B {
1: A itm = {}
}
68 changes: 68 additions & 0 deletions lib/py/test/test_compiler/test_default_struct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import importlib
import os
import subprocess
import sys
import tempfile

TEST_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, TEST_DIR)

import _import_local_thrift # noqa: E402,F401
from test_keyword_escape import find_thrift # noqa: E402


def test_default_struct_value():
thrift_file = os.path.join(os.path.dirname(__file__), 'Thrift4623.thrift')
thrift_bin = find_thrift()
if not thrift_bin:
print("WARNING: thrift compiler not found, skipping test")
return 0

with tempfile.TemporaryDirectory() as tmpdir:
result = subprocess.run(
[thrift_bin, '-gen', 'py', '-out', tmpdir, thrift_file],
capture_output=True, text=True)
if result.returncode != 0:
raise AssertionError("thrift compiler failed: " + result.stderr)

sys.path.insert(0, tmpdir)
try:
types = importlib.import_module('thrift4623.ttypes')
first = types.B()
second = types.B()

assert isinstance(first.itm, types.A)
assert isinstance(first.itm.nested, types.C)
assert first.itm.nested.value is None
assert first.itm is not second.itm
assert first.itm.nested is not second.itm.nested
assert types.B(itm=None).itm is None
finally:
sys.path.pop(0)
for name in list(sys.modules):
if name == 'thrift4623' or name.startswith('thrift4623.'):
del sys.modules[name]

print('OK: default-valued struct fields are usable')


if __name__ == '__main__':
test_default_struct_value()
Loading