From f2a57f72c48604b791fc99cdf0837a4e3dd691a4 Mon Sep 17 00:00:00 2001 From: Steven Myint Date: Sun, 29 Apr 2018 12:50:06 -0700 Subject: [PATCH] Format --- cpp/ast.py | 25 +++++++++++-------------- cpp/find_warnings.py | 10 +++++----- cpp/symbols.py | 7 ------- cpp/tokenize.py | 8 +++----- test_ast.py | 11 +++++------ 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/cpp/ast.py b/cpp/ast.py index 60c156b..c4e7b20 100644 --- a/cpp/ast.py +++ b/cpp/ast.py @@ -350,7 +350,6 @@ def __init__(self, start, end, name, templated_types, modifiers, templated_types: [Class (Type?)] template type info between <> modifiers: [str] type modifiers (keywords) eg, const, mutable, etc. reference, pointer, array: bools - """ _GenericDeclaration.__init__(self, start, end, name, []) self.templated_types = templated_types @@ -414,7 +413,6 @@ def to_type(self, tokens): Returns: [Class(...), ...] - """ result = [] name_tokens = [] @@ -833,12 +831,12 @@ def _get_var_tokens_up_to_w_function(self, skip_bracket_content, names = [token.name for token in tokens] ctr = collections.Counter(names) if ('(' in expected_tokens and - ctr['<'] != ctr['>'] and - ctr['function'] == 1 and - last.name == '('): + ctr['<'] != ctr['>'] and + ctr['function'] == 1 and + last.name == '('): - idx = names.index("function") - if idx + 1 < len(tokens) and tokens[idx + 1].name == "<": + idx = names.index('function') + if idx + 1 < len(tokens) and tokens[idx + 1].name == '<': new_tokens, new_last = \ self._get_var_tokens_up_to(False, '(', ';') tokens.append(last) @@ -1096,10 +1094,10 @@ def _get_method(self, return_type_and_name, modifiers, templated_types, member = member[0] if token.name == '(' or token.name == '{': end = '}' if token.name == '{' else ')' - initializers[member] = [x - for x in list(self._get_matching_char( - token.name, end)) - if x.name != ',' and x.name != end] + initializers[member] = [ + x for x in list(self._get_matching_char(token.name, + end)) + if x.name != ',' and x.name != end] token = self._get_next_token() # Handle pointer to functions. @@ -1448,7 +1446,7 @@ def handle_template(self): elif token.name == 'template': return self.handle_template() self._add_back_token(token) - tokens, last = self._get_var_tokens_up_to_w_function(False, "(", ";") + tokens, last = self._get_var_tokens_up_to_w_function(False, '(', ';') tokens.append(last) self._add_back_tokens(tokens) if last.name == '(': @@ -1625,7 +1623,7 @@ def handle_using(self): tokens = self._get_tokens_up_to(';') assert tokens new_type = self.converter.to_type(tokens) - if "namespace" in new_type[0].modifiers: + if 'namespace' in new_type[0].modifiers: return Using(tokens[0].start, tokens[0].end, tokens) else: # aside from namespaces, "using" can be used just like a typedef @@ -1659,7 +1657,6 @@ def builder_from_source(source, filename, system_includes, Returns: ASTBuilder - """ return ASTBuilder(tokenize.get_tokens(source), filename, diff --git a/cpp/find_warnings.py b/cpp/find_warnings.py index 60acc5f..35e05b9 100644 --- a/cpp/find_warnings.py +++ b/cpp/find_warnings.py @@ -20,7 +20,6 @@ warnings will always be displayed. There is no way to suppress any. There also needs to be a way to use annotations in the source code to suppress warnings. - """ from __future__ import absolute_import @@ -303,13 +302,13 @@ def _add_use(node, namespace, name=''): # Try to search for the value of the variable declaration for any # symbols, such as `#define` values or other variable names which # may be included in other files. - obj = getattr(node, "initial_value", None) + obj = getattr(node, 'initial_value', None) if obj: _do_lookup(obj, namespace) # If node is a VariableDeclaration, check if the variable type is # a symbol used in other includes. - obj = getattr(node, "type", None) + obj = getattr(node, 'type', None) if obj and isinstance(obj.name, basestring): _do_lookup(obj.name, namespace) @@ -319,7 +318,8 @@ def _add_use(node, namespace, name=''): return def _add_variable(node, namespace, reference=False): - obj = node.type if isinstance(node, ast.VariableDeclaration) else node + obj = node.type if isinstance( + node, ast.VariableDeclaration) else node if obj.reference or obj.pointer or reference: _add_reference(obj.name, namespace) @@ -397,7 +397,7 @@ def _add_template_use(name, types, namespace, reference=False): # These are things like auto_ptr which do # not require the class definition, only decl. _add_reference(cls.name, namespace) - elif name.startswith('Q') and name.endswith("Pointer"): + elif name.startswith('Q') and name.endswith('Pointer'): # Special case templated classes from the Qt framework. _add_reference(cls.name, namespace) else: diff --git a/cpp/symbols.py b/cpp/symbols.py index f493443..56bf64d 100644 --- a/cpp/symbols.py +++ b/cpp/symbols.py @@ -52,7 +52,6 @@ def _lookup_namespace(self, symbol, namespace): Args: symbol: Symbol namespace: pointer into self.namespaces - """ for namespace_part in symbol.parts: namespace = namespace.get(namespace_part) @@ -67,7 +66,6 @@ def _lookup_global(self, symbol): Args: symbol: Symbol - """ assert symbol.parts namespace = self.namespaces @@ -91,7 +89,6 @@ def _lookup_in_all_namespaces(self, symbol): Args: symbol: Symbol - """ namespace = self.namespaces # Create a stack of namespaces. @@ -123,7 +120,6 @@ def lookup_symbol(self, name, namespace_stack): Raises: Error if the symbol cannot be found. - """ # TODO(nnorwitz): a convenient API for this depends on the # representation of the name. e.g., does symbol_name contain @@ -148,7 +144,6 @@ def _add(self, symbol_name, namespace, node, module): """Helper function for adding symbols. See add_symbol(). - """ result = symbol_name in namespace namespace[symbol_name] = node, module @@ -165,7 +160,6 @@ def add_symbol(self, symbol_name, namespace_stack, node, module): Returns: bool(if symbol was *not* already present) - """ # TODO(nnorwitz): verify symbol_name doesn't contain :: ? if namespace_stack: @@ -185,7 +179,6 @@ def get_namespace(self, name_seq): Returns: ['names', 'that', 'are', 'namespaces', 'possibly', 'empty', 'list'] - """ namespaces = self.namespaces result = [] diff --git a/cpp/tokenize.py b/cpp/tokenize.py index 61b8d9a..45c89bc 100644 --- a/cpp/tokenize.py +++ b/cpp/tokenize.py @@ -27,7 +27,7 @@ _letters = 'abcdefghijklmnopqrstuvwxyz' _valid_identifier_first_char = _letters + _letters.upper() + '_$' _valid_identifier_char = _valid_identifier_first_char + '0123456789' -VALID_IDENTIFIER_FIRST_CHARS = frozenset( _valid_identifier_first_char ) +VALID_IDENTIFIER_FIRST_CHARS = frozenset(_valid_identifier_first_char) VALID_IDENTIFIER_CHARS = frozenset(_valid_identifier_char) HEX_DIGITS = frozenset('0123456789abcdefABCDEF') INT_OR_FLOAT_DIGITS = frozenset('01234567890eE-+') @@ -59,7 +59,6 @@ class Token(object): start contains the index of the first char of the token in the source end contains the index of the last char of the token in the source - """ def __init__(self, token_type, name, start, end): @@ -110,7 +109,6 @@ def get_tokens(source): Yields: Token that represents the next token in the source. - """ if not source.endswith('\n'): source += '\n' @@ -137,7 +135,8 @@ def get_tokens(source): token_type = UNKNOWN start = i c = source[i] - if c in valid_identifier_first_chars or c == '_': # Find a string token. + # Find a string token. + if c in valid_identifier_first_chars or c == '_': token_type = NAME while source[i] in valid_identifier_chars: i += 1 @@ -291,7 +290,6 @@ def _find(string, sub_string, start_index): """Return index of sub_string in string. Raise TokenError if sub_string is not found. - """ result = string.find(sub_string, start_index) if result == -1: diff --git a/test_ast.py b/test_ast.py index 2f796ba..541c07f 100755 --- a/test_ast.py +++ b/test_ast.py @@ -35,7 +35,6 @@ def _install_generic_equal(cls, attrs): Args: cls: Python class to add __eq__ method to attrs: string - space separated of attribute names to compare - """ attrs = attrs.split() @@ -610,7 +609,6 @@ class ASTBuilderIntegrationTest(unittest.TestCase): an integration test. It doesn't test any individual method. It tests whole code blocks. - """ def test_variable_array(self): @@ -993,7 +991,8 @@ class Foo { arg2 = nodes[0].body[2] arg3 = nodes[0].body[3] - exp_ctor = Function('Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[]) + exp_ctor = Function( + 'Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[]) exp_var = [VariableDeclaration('arg1', Type('int'), initial_value='1'), VariableDeclaration('arg2', Type('int'), initial_value='2'), VariableDeclaration('arg3', Type('int'), initial_value='3')] @@ -1024,7 +1023,8 @@ class Foo { arg2 = nodes[0].body[2] arg3 = nodes[0].body[3] - exp_ctor = Function('Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[]) + exp_ctor = Function( + 'Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[]) exp_var = [VariableDeclaration('arg1', Type('int'), initial_value='1'), VariableDeclaration('arg2', Type('int'), initial_value='2'), VariableDeclaration('arg3', Type('int'), initial_value='3')] @@ -1125,7 +1125,7 @@ def test_system_include(self): def test_include_path_overrides(self): paths = [os.path.dirname(os.path.realpath(__file__))] - fname = "test/include.h" + fname = 'test/include.h' def _tokens(): tokens_quotes = get_tokens('#include "test/include.h"') @@ -1235,6 +1235,5 @@ def test_inline_function(self): nodes[0]) - if __name__ == '__main__': unittest.main()