Skip to content

Commit

Permalink
Merge pull request #125 from Thriftpy/issue-121
Browse files Browse the repository at this point in the history
Support include files with dot in name
  • Loading branch information
ethe authored Aug 19, 2020
2 parents b981e8c + 62dd565 commit a8b7873
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions tests/parser-cases/issue_121.include.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct A {
1: string value
}
5 changes: 5 additions & 0 deletions tests/parser-cases/issue_121.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include "./issue_121.include.thrift"

struct B {
1: issue_121.include.A a
}
2 changes: 1 addition & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
thriftpy2.install_import_hook() # noqa

from thriftpy2.http import make_server, make_client, client_context # noqa
from thriftpy2.thrift import TApplicationException
from thriftpy2.thrift import TApplicationException # noqa


addressbook = thriftpy2.load(os.path.join(os.path.dirname(__file__),
Expand Down
4 changes: 4 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,7 @@ def test_nest_incomplete_type():
2: (15, 'field2', (15, (12, thrift.A)), False),
3: (15, 'field3', (15, (15, (12, thrift.A))), False)
}


def test_issue_121():
load('parser-cases/issue_121.thrift')
28 changes: 20 additions & 8 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,26 @@ def p_ref_type(p):
'''ref_type : IDENTIFIER'''
ref_type = thrift_stack[-1]

for index, name in enumerate(p[1].split('.')):
ref_type = getattr(ref_type, name, None)
if ref_type is None:
if index != len(p[1].split('.')) - 1:
raise ThriftParserError('No type found: %r, at line %d' %
(p[1], p.lineno(1)))
p[0] = incomplete_type.set_info((p[1], p.lineno(1)))
return
for attr in dir(ref_type):
if attr in {'__doc__', '__loader__', '__name__', '__package__',
'__spec__', '__thrift_file__', '__thrift_meta__'}:
continue
if p[1].startswith(attr + '.'):
name = p[1][len(attr)+1:]
included_ref_type = getattr(ref_type, attr)
resolved_ref_type = getattr(included_ref_type, name, None)
if resolved_ref_type is not None:
ref_type = resolved_ref_type
break
else:
for index, name in enumerate(p[1].split('.')):
ref_type = getattr(ref_type, name, None)
if ref_type is None:
if index != len(p[1].split('.')) - 1:
raise ThriftParserError('No type found: %r, at line %d' %
(p[1], p.lineno(1)))
p[0] = incomplete_type.set_info((p[1], p.lineno(1)))
return

if hasattr(ref_type, '_ttype'):
p[0] = getattr(ref_type, '_ttype'), ref_type
Expand Down

0 comments on commit a8b7873

Please sign in to comment.