diff --git a/thriftpy2/parser/__init__.py b/thriftpy2/parser/__init__.py index 2f4ff2c..525de49 100644 --- a/thriftpy2/parser/__init__.py +++ b/thriftpy2/parser/__init__.py @@ -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 @@ -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__) ) diff --git a/thriftpy2/parser/exc.py b/thriftpy2/parser/exc.py index 4130587..7c80c72 100644 --- a/thriftpy2/parser/exc.py +++ b/thriftpy2/parser/exc.py @@ -10,6 +10,10 @@ class ThriftParserError(Exception): pass +class ThriftModuleNameConflict(ThriftParserError): + pass + + class ThriftLexerError(ThriftParserError): pass diff --git a/thriftpy2/parser/parser.py b/thriftpy2/parser/parser.py index d1bc627..7ad0510 100644 --- a/thriftpy2/parser/parser.py +++ b/thriftpy2/parser/parser.py @@ -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])