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 Jan 8, 2025
1 parent 08a290d commit 431ab63
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 6 additions & 6 deletions thriftpy2/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import types

from .parser import parse, parse_fp, threadlocal, _cast
from .exc import ThriftParserError
from .exc import ThriftParserError, ThriftModuleNameConflict
from ..thrift import TPayloadMeta


Expand Down Expand Up @@ -42,21 +42,21 @@ def load(path,
if real_module:
sys.modules[module_name] = thrift
include_thrifts = list(zip(thrift.__thrift_meta__["includes"][:],
thrift.__thrift_meta__["sub_modules"][:]))
[module.__thrift_module_name__ for module in thrift.__thrift_meta__["includes"][:]]))
while include_thrifts:
include_thrift = include_thrifts.pop()
registered_thrift = sys.modules.get(include_thrift[1].__name__)
registered_thrift = sys.modules.get(include_thrift[1])
if registered_thrift is None:
sys.modules[include_thrift[1].__name__] = include_thrift[0]
sys.modules[include_thrift[1]] = include_thrift[0]
if hasattr(include_thrift[0], "__thrift_meta__"):
include_thrifts.extend(
list(
zip(
include_thrift[0].__thrift_meta__["includes"],
include_thrift[0].__thrift_meta__["sub_modules"])))
[module.__thrift_module_name__ for module in include_thrift[0].__thrift_meta__["includes"]])))
else:
if registered_thrift.__thrift_file__ != include_thrift[0].__thrift_file__:
raise ThriftParserError(
raise ThriftModuleNameConflict(
'Module name conflict between "%s" and "%s"' %
(registered_thrift.__thrift_file__, include_thrift[0].__thrift_file__)
)
Expand Down
4 changes: 4 additions & 0 deletions thriftpy2/parser/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class ThriftParserError(Exception):
pass


class ThriftModuleNameConflict(ThriftParserError):
pass


class ThriftLexerError(ThriftParserError):
pass

Expand Down
4 changes: 2 additions & 2 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def p_include(p):
child_include_module_name = os.path.basename(path)
if child_include_module_name.endswith(".thrift"):
child_include_module_name = child_include_module_name[:-7]
child.__name__ = child_include_module_name
setattr(child, '__name__', child_include_module_name)
setattr(child, '__thrift_module_name__', child_module_name)
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 431ab63

Please sign in to comment.