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 29, 2024
1 parent 591325f commit b740707
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions tests/parser-cases/foo.bar.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include "foo/bar.thrift"
Empty file.
6 changes: 6 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def test_include():
assert thrift.datetime == 1422009523


def test_include_conflict():
with pytest.raises(ThriftParserError) as excinfo:
load('parser-cases/foo.bar.thrift', module_name='foo.bar_thrift')
assert 'Module name conflict between "parser-cases/foo.bar.thrift" and "parser-cases/foo/bar.thrift"' == str(excinfo.value)


def test_cpp_include():
load('parser-cases/cpp_include.thrift')

Expand Down
20 changes: 12 additions & 8 deletions thriftpy2/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ def load(path,
# add sub modules to sys.modules recursively
if real_module:
sys.modules[module_name] = thrift
include_thrifts = thrift.__thrift_meta__["includes"][:]
include_thrifts = list(zip(thrift.__thrift_meta__["includes"][:],
thrift.__thrift_meta__["sub_modules"][:]))
while include_thrifts:
include_thrift = include_thrifts.pop()
lost_sub_modules = [
m for m in thrift.__thrift_meta__["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"])
registered_thrift = sys.modules.get(include_thrift[1].__name__)
if registered_thrift is None:
sys.modules[include_thrift[1].__name__] = include_thrift[0]
include_thrifts.extend(include_thrift[0].__thrift_meta__["includes"])
else:
if registered_thrift.__thrift_file__ != include_thrift[0].__thrift_file__:
raise ThriftParserError(
'Module name conflict between "%s" and "%s"' %
(registered_thrift.__thrift_file__, include_thrift[0].__thrift_file__)
)
return thrift


Expand Down
11 changes: 2 additions & 9 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,8 @@ 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__).replace("_thrift", "").replace(".", os.sep)) + os.sep + p[2])

child_path = child_path.lstrip(os.sep)

child_module_name = str(
child_path).replace(os.sep,
".").replace(
".thrift", "_thrift")
child_rel_path = os.path.relpath(str(path), os.path.dirname(thrift.__thrift_file__))
child_module_name = str(child_rel_path).replace(os.sep, ".").replace(".thrift", "_thrift")

child = parse(path, module_name=child_module_name)
setattr(thrift, str(child.__name__).replace("_thrift", ""), child)
Expand Down

0 comments on commit b740707

Please sign in to comment.