Skip to content

Commit

Permalink
fix: sub module conflict error
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Oct 30, 2024
1 parent 8e226b1 commit 015c6b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 9 additions & 6 deletions thriftpy2/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 9 additions & 0 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 015c6b4

Please sign in to comment.