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 Nov 4, 2024
1 parent e1d5a54 commit 43126be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion thriftpy2/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def load(path,
sub_modules = thrift.__thrift_meta__["sub_modules"][:]
for module in sub_modules:
if module not in sys.modules:
sys.modules[module] = module
sys.modules[module.__name__] = include_thrift
if include_thrift.__name__ not in sys.modules:
include_thrifts.extend(include_thrift.__thrift_meta__["includes"])
return thrift
Expand Down
11 changes: 8 additions & 3 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def p_include(p):
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])
os.path.dirname(remove_suffix(str(thrift.__name__), "_thrift").replace(".", "/")) + "/" + p[2])

child_path = child_path.lstrip("/")

Expand All @@ -72,8 +72,8 @@ def p_include(p):
".").replace(
".thrift", "_thrift")

child = parse(path)
setattr(thrift, child.__name__, child)
child = parse(path, module_name=child_module_name)
setattr(thrift, remove_suffix(child.__name__, "_thrift"), child)
_add_thrift_meta('includes', child)
_add_thrift_meta('sub_modules', types.ModuleType(child_module_name))
return
Expand Down Expand Up @@ -960,3 +960,8 @@ def _get_ttype(inst, default_ttype=None):
if hasattr(inst, '__dict__') and '_ttype' in inst.__dict__:
return inst.__dict__['_ttype']
return default_ttype

def remove_suffix(s, suffix):
if s.endswith(suffix):
return s[:-len(suffix)]
return s

0 comments on commit 43126be

Please sign in to comment.