From f7fdb87296a67f802f8e30b2d30010656945dc92 Mon Sep 17 00:00:00 2001 From: StellarisW Date: Fri, 8 Nov 2024 11:50:37 +0800 Subject: [PATCH] fix: sub module conflict error --- thriftpy2/parser/__init__.py | 9 +++++---- thriftpy2/parser/parser.py | 9 ++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/thriftpy2/parser/__init__.py b/thriftpy2/parser/__init__.py index 07d619b..10afccc 100644 --- a/thriftpy2/parser/__init__.py +++ b/thriftpy2/parser/__init__.py @@ -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 diff --git a/thriftpy2/parser/parser.py b/thriftpy2/parser/parser.py index 6a1cd49..441f401 100644 --- a/thriftpy2/parser/parser.py +++ b/thriftpy2/parser/parser.py @@ -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) @@ -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 @@ -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 \ No newline at end of file