diff --git a/thriftpy2/parser/__init__.py b/thriftpy2/parser/__init__.py index 8930ce8..479fe2e 100644 --- a/thriftpy2/parser/__init__.py +++ b/thriftpy2/parser/__init__.py @@ -41,12 +41,15 @@ def load(path, # add sub modules to sys.modules recursively if real_module: sys.modules[module_name] = thrift - sub_modules = thrift.__thrift_meta__["includes"][:] - while sub_modules: - module = sub_modules.pop() - if module not in sys.modules: - sys.modules[module.__name__] = module - sub_modules.extend(module.__thrift_meta__["includes"]) + include_thrifts = thrift.__thrift_meta__["includes"][:] + while include_thrifts: + include_thrift = include_thrifts.pop() + sub_modules = thrift.__thrift_meta__["sub_modules"][:] + for module in sub_modules: + if module not in sys.modules: + sys.modules[module] = module + if include_thrift.__name__ not in sys.modules: + include_thrifts.extend(include_thrift.__thrift_meta__["includes"]) return thrift diff --git a/thriftpy2/parser/parser.py b/thriftpy2/parser/parser.py index 9ad2ebd..8a1c599 100644 --- a/thriftpy2/parser/parser.py +++ b/thriftpy2/parser/parser.py @@ -62,9 +62,18 @@ def p_include(p): for include_dir in replace_include_dirs: path = os.path.join(include_dir, p[2]) if os.path.exists(path): + child_path = os.path.normpath( + os.path.dirname(str(thrift.__name__).rstrip("_thrift").replace(".", "/")) + "/" + p[2]) + + child_module_name = str( + child_path).replace("/", + ".").replace( + ".thrift", "_thrift") + child = parse(path) setattr(thrift, child.__name__, child) _add_thrift_meta('includes', child) + _add_thrift_meta('sub_modules', types.ModuleType(child_module_name)) return raise ThriftParserError(('Couldn\'t include thrift %s in any ' 'directories provided') % p[2])