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 c5b2411 commit 1ca925e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tests/parser-cases/include.thrift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include "included.thrift"
include "include/included.thrift"
include "include/included_1.thrift"

const included.Timestamp datetime = 1422009523
1 change: 0 additions & 1 deletion tests/parser-cases/include/included.thrift

This file was deleted.

1 change: 1 addition & 0 deletions tests/parser-cases/include/included_1.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include "included_2.thrift"
Empty file.
12 changes: 5 additions & 7 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

import sys
import threading

import pytest
Expand Down Expand Up @@ -42,12 +42,10 @@ def test_include():

def test_include_with_module_name_prefix():
thrift = load('parser-cases/include.thrift', module_name='parser_cases.include_thrift')
included1_thrift = thrift.__thrift_meta__['includes'][0]
assert included1_thrift.__name__ == 'parser_cases.include_thrift'
included2_thrift = thrift.__thrift_meta__['includes'][1]
assert included2_thrift.__name__ == 'parser_cases.included.include_thrift'
included3_thrift = included2_thrift.__thrift_meta__['includes'][0]
assert included3_thrift.__name__ == 'parser_cases.included.include_1_thrift'
assert sys.modules['parser_cases.include_thrift'] is not None
assert sys.modules['parser_cases.included_thrift'] is not None
assert sys.modules['parser_cases.include.included_1_thrift'] is not None
assert sys.modules['parser_cases.include.included_2_thrift'] is not None


def test_include_conflict():
Expand Down
6 changes: 5 additions & 1 deletion thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def p_include(p):
child_module_name = module_prefix + child_module_name

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

0 comments on commit 1ca925e

Please sign in to comment.