Python _ast.Tuple() Examples
The following are 30
code examples of _ast.Tuple().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
_ast
, or try the search function
.
Example #1
Source File: checker.py From Transcrypt with Apache License 2.0 | 6 votes |
def TRY(self, node): handler_names = [] # List the exception handlers for handler in node.handlers: if isinstance(handler.type, ast.Tuple): for exc_type in handler.type.elts: handler_names.append(getNodeName(exc_type)) elif handler.type: handler_names.append(getNodeName(handler.type)) # Memorize the except handlers and process the body self.exceptHandlers.append(handler_names) for child in node.body: self.handleNode(child, node) self.exceptHandlers.pop() # Process the other nodes: "except:", "else:", "finally:" self.handleChildren(node, omit='body')
Example #2
Source File: stmt.py From equip with Apache License 2.0 | 6 votes |
def make_assign_chained(i, bytecode): store_exprs = [] value_exprs = [] store_state, value_state = True, False while i >= 0: op, arg = bytecode[i][2], bytecode[i][3] if store_state: if op == DUP_TOP: prev_op = bytecode[i - 1][2] if i > 0 else -1 if prev_op not in STORE_OPCODES: value_state = True store_state = False elif op in STORE_OPCODES: i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store()) store_exprs.insert(0, store_stmt) elif value_state: i, value_exprs = Statement.make_expr(i, bytecode) break i -= 1 store_exprs = _ast.Tuple(store_exprs, _ast.Store()) return i, _ast.Assign([store_exprs], value_exprs)
Example #3
Source File: checker.py From blackmamba with MIT License | 6 votes |
def TRY(self, node): handler_names = [] # List the exception handlers for i, handler in enumerate(node.handlers): if isinstance(handler.type, ast.Tuple): for exc_type in handler.type.elts: handler_names.append(getNodeName(exc_type)) elif handler.type: handler_names.append(getNodeName(handler.type)) if handler.type is None and i < len(node.handlers) - 1: self.report(messages.DefaultExceptNotLast, handler) # Memorize the except handlers and process the body self.exceptHandlers.append(handler_names) for child in node.body: self.handleNode(child, node) self.exceptHandlers.pop() # Process the other nodes: "except:", "else:", "finally:" self.handleChildren(node, omit='body')
Example #4
Source File: checker.py From blackmamba with MIT License | 6 votes |
def convert_to_value(item): if isinstance(item, ast.Str): return item.s elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes): return item.s elif isinstance(item, ast.Tuple): return tuple(convert_to_value(i) for i in item.elts) elif isinstance(item, ast.Num): return item.n elif isinstance(item, ast.Name): result = VariableKey(item=item) constants_lookup = { 'True': True, 'False': False, 'None': None, } return constants_lookup.get( result.name, result, ) elif (not PY33) and isinstance(item, ast.NameConstant): # None, True, False are nameconstants in python3, but names in 2 return item.value else: return UnhandledKeyType()
Example #5
Source File: pyparser.py From teleport with Apache License 2.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #6
Source File: pyparser.py From android_universal with MIT License | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #7
Source File: pyparser.py From ansible-cmdb with GNU General Public License v3.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #8
Source File: pyparser.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #9
Source File: pyparser.py From docassemble with MIT License | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #10
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def getParent(self, node): # Lookup the first parent which is not Tuple, List or Starred while True: node = node.parent if not hasattr(node, 'elts') and not hasattr(node, 'ctx'): return node
Example #11
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def __init__(self, name, source, scope): if '__all__' in scope and isinstance(source, ast.AugAssign): self.names = list(scope['__all__'].names) else: self.names = [] if isinstance(source.value, (ast.List, ast.Tuple)): for node in source.value.elts: if isinstance(node, ast.Str): self.names.append(node.s) super(ExportBinding, self).__init__(name, source)
Example #12
Source File: pyparser.py From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #13
Source File: checker.py From blackmamba with MIT License | 5 votes |
def ASSERT(self, node): if isinstance(node.test, ast.Tuple) and node.test.elts != []: self.report(messages.AssertTuple, node) self.handleChildren(node)
Example #14
Source File: checker.py From blackmamba with MIT License | 5 votes |
def getParent(self, node): # Lookup the first parent which is not Tuple, List or Starred while True: node = node.parent if not hasattr(node, 'elts') and not hasattr(node, 'ctx'): return node
Example #15
Source File: checker.py From blackmamba with MIT License | 5 votes |
def __init__(self, name, source, scope): if '__all__' in scope and isinstance(source, ast.AugAssign): self.names = list(scope['__all__'].names) else: self.names = [] if isinstance(source.value, (ast.List, ast.Tuple)): for node in source.value.elts: if isinstance(node, ast.Str): self.names.append(node.s) super(ExportBinding, self).__init__(name, source)
Example #16
Source File: pyparser.py From mako with MIT License | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #17
Source File: pyparser.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #18
Source File: pyparser.py From teleport with Apache License 2.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #19
Source File: pyparser.py From teleport with Apache License 2.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #20
Source File: calc.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def visit_tuple(self, node: _ast.Tuple): # elts, ctx return tuple(self._run(x) for x in node.elts)
Example #21
Source File: calc.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def delete(self, node): cls = node.__class__ if cls == _ast.Name: del self.symbol_table[node.id] elif cls == _ast.Tuple: for elt in node.elts: self.delete(elt)
Example #22
Source File: calc.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def assign(self, node, val): cls = node.__class__ if cls == _ast.Name: self.symbol_table[node.id] = val elif cls in (_ast.Tuple, _ast.List): if not isinstance(val, abc.Iterable): raise TypeError( 'cannot unpack non-iterable {} object'.format( type(val).__name__, ) ) for telem, tval in itertools.zip_longest( node.elts, val, fillvalue=PLACEHOLDER, ): if telem == PLACEHOLDER: raise ValueError('not enough values to unpack') if tval == PLACEHOLDER: raise ValueError('too many values to unpack') self.assign(telem, tval) elif cls == _ast.Subscript: sym = self._run(node.value) xslice = self._run(node.slice) if isinstance(node.slice, _ast.Index): sym[xslice] = val elif isinstance(node.slice, _ast.Slice): sym[slice(xslice.start, xslice.stop)] = val elif isinstance(node.slice, _ast.ExtSlice): sym[xslice] = val else: raise BadSyntax('This assign method is not allowed')
Example #23
Source File: types.py From equip with Apache License 2.0 | 5 votes |
def is_sequence(ast_node): return isinstance(ast_node, _ast.Str) \ or isinstance(ast_node, _ast.Tuple) \ or isinstance(ast_node, _ast.List)
Example #24
Source File: types.py From equip with Apache License 2.0 | 5 votes |
def sequence_typeof(ast_node): if isinstance(ast_node, _ast.Str): return StringType() elif isinstance(ast_node, _ast.Tuple): return TupleType() elif isinstance(ast_node, _ast.List): return ListType() return SequenceType()
Example #25
Source File: stmt.py From equip with Apache License 2.0 | 5 votes |
def make_tuple_list(i, bytecode): op, arg = bytecode[i][2], bytecode[i][3] pop, push = get_stack_effect(op, arg) values = [] for k in range(pop): i, expr = Statement.make_expr(i - 1, bytecode) values.insert(0, expr) cls = _ast.Tuple if op == BUILD_TUPLE else _ast.List return i, cls(values, _ast.Load())
Example #26
Source File: stmt.py From equip with Apache License 2.0 | 5 votes |
def make_assign_opt_unpack(i, bytecode): store_exprs = [] value_exprs = [] store_state, value_state = True, False while i >= 0: op, arg = bytecode[i][2], bytecode[i][3] if store_state: if op == ROT_TWO: prev_op = bytecode[i - 1][2] if i > 0 else -1 if prev_op == ROT_THREE: i -= 1 value_state = True store_state = False elif op in STORE_OPCODES: i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store()) store_exprs.insert(0, store_stmt) elif value_state: i, value_stmt = Statement.make_expr(i, bytecode) value_exprs.insert(0, value_stmt) i -= 1 store_exprs = _ast.Tuple(store_exprs, _ast.Store()) if not isinstance(value_exprs, _ast.AST): value_exprs = _ast.Tuple(value_exprs, _ast.Load()) return i, _ast.Assign([store_exprs], value_exprs) # Only one case here for: # a = b = z.d.f = foo() # => AST: _ast.Assign(targets=[Tuple(a, b, z.d.f)], value=foo())
Example #27
Source File: stmt.py From equip with Apache License 2.0 | 5 votes |
def make_assign_unpack(i, bytecode, unpack_num=-1): if unpack_num < 1: logger.error("Could not find the number of unpacked items. ") return i, None store_exprs = [] value_exprs = [] store_state, value_state = True, False while i >= 0: op, arg = bytecode[i][2], bytecode[i][3] if store_state: if op == UNPACK_SEQUENCE: store_state = False prev_op = bytecode[i - 1][2] if i > 0 else -1 if prev_op == BUILD_TUPLE: value_state = True else: i, value_exprs = Statement.make_expr(i - 1, bytecode) break elif op in STORE_OPCODES: i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store()) store_exprs.insert(0, store_stmt) elif value_state: i, value_stmt = Statement.make_expr(i, bytecode) value_exprs.insert(0, value_stmt) i -= 1 store_exprs = _ast.Tuple(store_exprs, _ast.Store()) if not isinstance(value_exprs, _ast.AST): value_exprs = _ast.Tuple(value_exprs, _ast.Load()) return i, _ast.Assign([store_exprs], value_exprs)
Example #28
Source File: pyparser.py From jbox with MIT License | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #29
Source File: pyparser.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def _expand_tuples(self, args): for arg in args: if isinstance(arg, _ast.Tuple): for n in arg.elts: yield n else: yield arg
Example #30
Source File: stmt.py From equip with Apache License 2.0 | 4 votes |
def make_assign(i, bytecode): op = bytecode[i][2] if op == STORE_SUBSCR: return Statement.make_subscript(i, bytecode) prev_op = bytecode[i - 1][2] if i > 0 else -1 if prev_op in INPLACE_OPCODES: in_cls = Statement.INPLACE_OPERATORS[prev_op] i -= 1 i, rhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugStore()) i, lhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugLoad()) return i, _ast.AugAssign(lhs, in_cls(), rhs) else: # We can either have multiple assignments: a = b = c = 1 # or unpacked sequences: a, b = 1, foo() # the compiler does some optimization so that: a, b = c, d # does not rely on UNPACK_SEQUENCE, but a ROT_TWO (or ROT_THREE & ROT_TWO for 3 elements). # This happens for 2 or 3 elements to unpack targets = [] value = None has_unpack, has_ROT_2_3, has_multiple = False, False, 0 num_unpack = -1 j = i while j >= 0: op = bytecode[j][2] if op == UNPACK_SEQUENCE: has_unpack = True num_unpack = bytecode[j][3] break elif op in (ROT_TWO, ROT_THREE): has_ROT_2_3 = True break if op == DUP_TOP: has_multiple += 1 j -= 1 if has_unpack: return Statement.make_assign_unpack(i, bytecode, unpack_num=num_unpack) elif has_ROT_2_3: return Statement.make_assign_opt_unpack(i, bytecode) elif has_multiple > 0: return Statement.make_assign_chained(i, bytecode) else: # A simple assignment i, store_expr = Statement.make_expr(i, bytecode) i, value_expr = Statement.make_expr(i - 1, bytecode) return i, _ast.Assign([store_expr], value_expr) return i, None # 2 cases here: # (1) a, b, c = foo() <=> v = foo(), a = v[0], b = v[1], c = v[2] # => AST: _ast.Assign(targets=[Tuple(a, b, c)], value=foo()) # # (2) a, b = foo(), bar() <=> a = foo(), b = bar() # => AST: _ast.Assign(targets=[Tuple(a, b)], value=Tuple(baz(), bar()))