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 8, 2024
1 parent b16ef28 commit f7fdb87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
9 changes: 5 additions & 4 deletions thriftpy2/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ def load(path,
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.__name__] = include_thrift
lost_sub_modules = [
m for m in thrift.__thrift_meata__["sub_modules"] if m not in sys.modules
]
for module in lost_sub_modules:
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
9 changes: 2 additions & 7 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(remove_suffix(str(thrift.__name__), "_thrift").replace(".", os.sep)) + os.sep + p[2])
os.path.dirname(str(thrift.__name__).replace("_thrift", "").replace(".", os.sep)) + os.sep + p[2])

child_path = child_path.lstrip(os.sep)

Expand All @@ -73,7 +73,7 @@ def p_include(p):
".thrift", "_thrift")

child = parse(path, module_name=child_module_name)
setattr(thrift, remove_suffix(child.__name__, "_thrift"), child)
setattr(thrift, str(child.__name__).replace("_thrift", ""), child)
_add_thrift_meta('includes', child)
_add_thrift_meta('sub_modules', types.ModuleType(child_module_name))
return
Expand Down Expand Up @@ -960,8 +960,3 @@ 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 f7fdb87

Please sign in to comment.