From cfc202528b0771f714c8b4299bbfdb9f22d90867 Mon Sep 17 00:00:00 2001 From: JinLiYan Date: Thu, 7 Jul 2016 15:34:58 +0800 Subject: [PATCH 1/5] =?UTF-8?q?byte=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8Cinclude=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thriftpy/parser/parser.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index f93c305..d62e5ae 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -46,12 +46,12 @@ def p_header_unit(p): def p_include(p): '''include : INCLUDE LITERAL''' thrift = thrift_stack[-1] - if thrift.__thrift_file__ is None: raise ThriftParserError('Unexcepted include statement while loading' 'from file like object.') - - for include_dir in include_dirs_: + replace_include_dirs = [os.path.dirname(thrift.__thrift_file__)] + + include_dirs_ + for include_dir in replace_include_dirs: path = os.path.join(include_dir, p[2]) if os.path.exists(path): child = parse(path) @@ -153,7 +153,6 @@ def p_const_map_item(p): def p_const_ref(p): '''const_ref : IDENTIFIER''' child = thrift_stack[-1] - for name in p[1].split('.'): father = child child = getattr(child, name, None) @@ -609,7 +608,7 @@ def _cast_bool(v): def _cast_byte(v): - assert isinstance(v, str) + assert isinstance(v, int) return v From c77615346e877ae702a6a80b608577a02a2ee77d Mon Sep 17 00:00:00 2001 From: "lisen.jin" Date: Fri, 8 Jul 2016 16:50:49 +0800 Subject: [PATCH 2/5] fix a wrong line feed --- thriftpy/parser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index d62e5ae..db6131c 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -49,7 +49,7 @@ def p_include(p): if thrift.__thrift_file__ is None: raise ThriftParserError('Unexcepted include statement while loading' 'from file like object.') - replace_include_dirs = [os.path.dirname(thrift.__thrift_file__)] + replace_include_dirs = [os.path.dirname(thrift.__thrift_file__)] \ + include_dirs_ for include_dir in replace_include_dirs: path = os.path.join(include_dir, p[2]) From c991d9d3620c17c82cd65879f8421e6e0187b27c Mon Sep 17 00:00:00 2001 From: "lisen.jin" Date: Thu, 22 Sep 2016 15:58:39 +0800 Subject: [PATCH 3/5] fix the bug of include same name files --- thriftpy/parser/parser.py | 51 ++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index db6131c..477effd 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -55,7 +55,11 @@ def p_include(p): path = os.path.join(include_dir, p[2]) if os.path.exists(path): child = parse(path) - setattr(thrift, child.__name__, child) + child_list = getattr(thrift, child.__name__, None) + if child_list is None: + child_list = [] + child_list.append(child) + setattr(thrift, child.__name__, child_list) # modified _add_thrift_meta('includes', child) return raise ThriftParserError(('Couldn\'t include thrift %s in any ' @@ -155,17 +159,19 @@ def p_const_ref(p): child = thrift_stack[-1] for name in p[1].split('.'): father = child - child = getattr(child, name, None) - if child is None: + child_list = getattr(child, name, None) + if child_list is None: raise ThriftParserError('Cann\'t find name %r at line %d' % (p[1], p.lineno(1))) - if _get_ttype(child) is None or _get_ttype(father) == TType.I32: - # child is a constant or enum value - p[0] = child - else: - raise ThriftParserError('No enum value or constant found ' - 'named %r' % p[1]) + for child in child_list: + if _get_ttype(child) is None or _get_ttype(father) == TType.I32: + # child is a constant or enum value + p[0] = child + return + else: + raise ThriftParserError('No enum value or constant found ' + 'named %r' % p[1]) def p_ttype(p): @@ -248,7 +254,15 @@ def p_service(p): if len(p) == 8: extends = thrift for name in p[4].split('.'): - extends = getattr(extends, name, None) + if isinstance(extends, list): + for e in extends: + temp = getattr(e, name, None) + if temp: + extends = temp + break + else: + extends = getattr(extends, name, None) + if extends is None: raise ThriftParserError('Can\'t find service %r for ' 'service %r to extend' % @@ -358,10 +372,19 @@ def p_ref_type(p): ref_type = thrift_stack[-1] for name in p[1].split('.'): - ref_type = getattr(ref_type, name, None) - if ref_type is None: - raise ThriftParserError('No type found: %r, at line %d' % - (p[1], p.lineno(1))) + if isinstance(ref_type, list): + for r in ref_type: + temp = getattr(r, name, None) + if temp: + ref_type = temp + break + else: + ref_type = getattr(ref_type, name, None) + + if ref_type is None: + raise ThriftParserError('No type found: %r, at line %d' % + (p[1], p.lineno(1))) + if hasattr(ref_type, '_ttype'): p[0] = getattr(ref_type, '_ttype'), ref_type From 4e9898416cbd7f5cb3c30a0580d2021092c98e1c Mon Sep 17 00:00:00 2001 From: "lisen.jin" Date: Thu, 22 Sep 2016 19:34:08 +0800 Subject: [PATCH 4/5] fix a bug of for loop --- thriftpy/parser/parser.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index 477effd..d6fcbed 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -158,6 +158,7 @@ def p_const_ref(p): '''const_ref : IDENTIFIER''' child = thrift_stack[-1] for name in p[1].split('.'): + father = child child_list = getattr(child, name, None) if child_list is None: @@ -250,7 +251,6 @@ def p_service(p): | SERVICE IDENTIFIER EXTENDS IDENTIFIER '{' function_seq '}' ''' thrift = thrift_stack[-1] - if len(p) == 8: extends = thrift for name in p[4].split('.'): @@ -262,11 +262,11 @@ def p_service(p): break else: extends = getattr(extends, name, None) - - if extends is None: - raise ThriftParserError('Can\'t find service %r for ' - 'service %r to extend' % - (p[4], p[2])) + + if extends is None: + raise ThriftParserError('Can\'t find service %r for ' + 'service %r to extend' % + (p[4], p[2])) if not hasattr(extends, 'thrift_services'): raise ThriftParserError('Can\'t extends %r, not a service' @@ -370,7 +370,6 @@ def p_field_type(p): def p_ref_type(p): '''ref_type : IDENTIFIER''' ref_type = thrift_stack[-1] - for name in p[1].split('.'): if isinstance(ref_type, list): for r in ref_type: @@ -381,9 +380,9 @@ def p_ref_type(p): else: ref_type = getattr(ref_type, name, None) - if ref_type is None: - raise ThriftParserError('No type found: %r, at line %d' % - (p[1], p.lineno(1))) + if ref_type is None: + raise ThriftParserError('No type found: %r, at line %d' % + (p[1], p.lineno(1))) if hasattr(ref_type, '_ttype'): @@ -392,7 +391,7 @@ def p_ref_type(p): p[0] = ref_type -def p_base_type(p): # noqa +def p_base_type(p): # noq '''base_type : BOOL | BYTE | I16 From 344bd86e9c572ba3992fbb8bcdc91fd3122d74a1 Mon Sep 17 00:00:00 2001 From: "lisen.jin" Date: Mon, 9 Jan 2017 16:50:41 +0800 Subject: [PATCH 5/5] change return to break in method p_const_ref --- thriftpy/parser/parser.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index d6fcbed..0522115 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -158,7 +158,6 @@ def p_const_ref(p): '''const_ref : IDENTIFIER''' child = thrift_stack[-1] for name in p[1].split('.'): - father = child child_list = getattr(child, name, None) if child_list is None: @@ -169,7 +168,7 @@ def p_const_ref(p): if _get_ttype(child) is None or _get_ttype(father) == TType.I32: # child is a constant or enum value p[0] = child - return + break else: raise ThriftParserError('No enum value or constant found ' 'named %r' % p[1]) @@ -251,6 +250,7 @@ def p_service(p): | SERVICE IDENTIFIER EXTENDS IDENTIFIER '{' function_seq '}' ''' thrift = thrift_stack[-1] + if len(p) == 8: extends = thrift for name in p[4].split('.'): @@ -262,11 +262,11 @@ def p_service(p): break else: extends = getattr(extends, name, None) - - if extends is None: - raise ThriftParserError('Can\'t find service %r for ' - 'service %r to extend' % - (p[4], p[2])) + + if extends is None: + raise ThriftParserError('Can\'t find service %r for ' + 'service %r to extend' % + (p[4], p[2])) if not hasattr(extends, 'thrift_services'): raise ThriftParserError('Can\'t extends %r, not a service' @@ -370,6 +370,7 @@ def p_field_type(p): def p_ref_type(p): '''ref_type : IDENTIFIER''' ref_type = thrift_stack[-1] + for name in p[1].split('.'): if isinstance(ref_type, list): for r in ref_type: @@ -380,9 +381,9 @@ def p_ref_type(p): else: ref_type = getattr(ref_type, name, None) - if ref_type is None: - raise ThriftParserError('No type found: %r, at line %d' % - (p[1], p.lineno(1))) + if ref_type is None: + raise ThriftParserError('No type found: %r, at line %d' % + (p[1], p.lineno(1))) if hasattr(ref_type, '_ttype'): @@ -391,7 +392,7 @@ def p_ref_type(p): p[0] = ref_type -def p_base_type(p): # noq +def p_base_type(p): # noqa '''base_type : BOOL | BYTE | I16 @@ -454,11 +455,9 @@ def p_definition_type(p): def parse(path, module_name=None, include_dirs=None, include_dir=None, lexer=None, parser=None, enable_cache=True): """Parse a single thrift file to module object, e.g.:: - >>> from thriftpy.parser.parser import parse >>> note_thrift = parse("path/to/note.thrift") - :param path: file path to parse, should be a string ending with '.thrift'. :param module_name: the name for parsed module, the default is the basename without extension of `path`. @@ -530,12 +529,10 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None, def parse_fp(source, module_name, lexer=None, parser=None, enable_cache=True): """Parse a file-like object to thrift module object, e.g.:: - >>> from thriftpy.parser.parser import parse_fp >>> with open("path/to/note.thrift") as fp: parse_fp(fp, "note_thrift") - :param source: file-like object, expected to have a method named `read`. :param module_name: the name for parsed module, shoule be endswith '_thrift'. @@ -838,4 +835,4 @@ def _ttype_spec(ttype, name, required=False): def _get_ttype(inst, default_ttype=None): if hasattr(inst, '__dict__') and '_ttype' in inst.__dict__: return inst.__dict__['_ttype'] - return default_ttype + return default_ttype \ No newline at end of file