Skip to content

Commit a8b7873

Browse files
authored
Merge pull request #125 from Thriftpy/issue-121
Support include files with dot in name
2 parents b981e8c + 62dd565 commit a8b7873

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct A {
2+
1: string value
3+
}

tests/parser-cases/issue_121.thrift

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include "./issue_121.include.thrift"
2+
3+
struct B {
4+
1: issue_121.include.A a
5+
}

tests/test_http.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
thriftpy2.install_import_hook() # noqa
1414

1515
from thriftpy2.http import make_server, make_client, client_context # noqa
16-
from thriftpy2.thrift import TApplicationException
16+
from thriftpy2.thrift import TApplicationException # noqa
1717

1818

1919
addressbook = thriftpy2.load(os.path.join(os.path.dirname(__file__),

tests/test_parser.py

+4
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@ def test_nest_incomplete_type():
298298
2: (15, 'field2', (15, (12, thrift.A)), False),
299299
3: (15, 'field3', (15, (15, (12, thrift.A))), False)
300300
}
301+
302+
303+
def test_issue_121():
304+
load('parser-cases/issue_121.thrift')

thriftpy2/parser/parser.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,26 @@ def p_ref_type(p):
391391
'''ref_type : IDENTIFIER'''
392392
ref_type = thrift_stack[-1]
393393

394-
for index, name in enumerate(p[1].split('.')):
395-
ref_type = getattr(ref_type, name, None)
396-
if ref_type is None:
397-
if index != len(p[1].split('.')) - 1:
398-
raise ThriftParserError('No type found: %r, at line %d' %
399-
(p[1], p.lineno(1)))
400-
p[0] = incomplete_type.set_info((p[1], p.lineno(1)))
401-
return
394+
for attr in dir(ref_type):
395+
if attr in {'__doc__', '__loader__', '__name__', '__package__',
396+
'__spec__', '__thrift_file__', '__thrift_meta__'}:
397+
continue
398+
if p[1].startswith(attr + '.'):
399+
name = p[1][len(attr)+1:]
400+
included_ref_type = getattr(ref_type, attr)
401+
resolved_ref_type = getattr(included_ref_type, name, None)
402+
if resolved_ref_type is not None:
403+
ref_type = resolved_ref_type
404+
break
405+
else:
406+
for index, name in enumerate(p[1].split('.')):
407+
ref_type = getattr(ref_type, name, None)
408+
if ref_type is None:
409+
if index != len(p[1].split('.')) - 1:
410+
raise ThriftParserError('No type found: %r, at line %d' %
411+
(p[1], p.lineno(1)))
412+
p[0] = incomplete_type.set_info((p[1], p.lineno(1)))
413+
return
402414

403415
if hasattr(ref_type, '_ttype'):
404416
p[0] = getattr(ref_type, '_ttype'), ref_type

0 commit comments

Comments
 (0)