Github user Jens-G commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/982#discussion_r58929379
  
    --- Diff: compiler/cpp/src/generate/t_py_generator.cc ---
    @@ -384,6 +406,55 @@ void t_py_generator::init_generator() {
         py_autogen_comment() << endl <<
         py_imports() << endl <<
         "from .ttypes import *" << endl;
    +
    +  construct_object_dependent_graph();
    +}
    +
    +/**
    + * Construct object dependent graph to determine object generate order
    + * if cycle dependent is detected, generate process will be aborted
    + * object dependent example
    + * struct A {
    + * 1: B b;
    + * }
    + * struct B {
    + * }
    + *
    + * then B should be generated before A in ttypes.py, otherwise runtime 
error exception will be thrown
    + * in generated python code
    + */
    +void t_py_generator::construct_object_dependent_graph() {
    +  const std::vector<t_struct*>& objects = program_->get_objects();
    +  for (size_t i = 0; i < objects.size(); ++i) {
    +    object_ids_[objects[i]] = i;
    +  }
    +
    +  std::vector<DependentGraphEdge> edges;
    +  for (size_t i = 0; i < objects.size(); i++) {
    +    const vector<t_field*>& members = objects[i]->get_members();
    +    for(vector<t_field*>::const_iterator m_iter = members.begin(); m_iter 
!= members.end(); ++m_iter) {
    +      t_type* type = get_true_type((*m_iter)->get_type());
    +      if (!type->is_struct() && !type->is_xception()) {
    +        continue;
    +      }
    +      map<t_struct*, int>::const_iterator it = 
object_ids_.find((t_struct*)type);
    +      if (it != object_ids_.end()) {
    +        edges.push_back(DependentGraphEdge(it->second, i));
    +      }
    +    }
    +  }
    +
    +  DependentGraph dependent_graph(edges.begin(), edges.end(), 
objects.size());
    +  // check cycle dependent
    +  bool has_cycle = false;
    +  cycle_detector detector(has_cycle);
    +  boost::depth_first_search(dependent_graph, boost::visitor(detector));
    +  if (has_cycle) {
    +    throw "type error: cycel dependent found in " + program_->get_path();
    --- End diff --
    
    Would be nice to know which type(s) are affected.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to