Commit 78aa444d authored by Stefan Behnel's avatar Stefan Behnel

Avoid hasattr()+getattr pattern in favour of a single lookup.

parent 55309f78
...@@ -217,8 +217,9 @@ class EmbedSignature(CythonTransform): ...@@ -217,8 +217,9 @@ class EmbedSignature(CythonTransform):
old_doc = None old_doc = None
new_doc = self._embed_signature(signature, old_doc) new_doc = self._embed_signature(signature, old_doc)
node.entry.doc = EncodedString(new_doc) node.entry.doc = EncodedString(new_doc)
if hasattr(node, 'py_func') and node.py_func is not None: py_func = getattr(node, 'py_func', None)
node.py_func.entry.doc = EncodedString(new_doc) if py_func is not None:
py_func.entry.doc = EncodedString(new_doc)
return node return node
def visit_PropertyNode(self, node): def visit_PropertyNode(self, node):
......
...@@ -631,7 +631,7 @@ class ExprNode(Node): ...@@ -631,7 +631,7 @@ class ExprNode(Node):
def type_dependencies(self, env): def type_dependencies(self, env):
# Returns the list of entries whose types must be determined # Returns the list of entries whose types must be determined
# before the type of self can be inferred. # before the type of self can be inferred.
if hasattr(self, 'type') and self.type is not None: if getattr(self, 'type', None) is not None:
return () return ()
return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ()) return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ())
...@@ -640,12 +640,13 @@ class ExprNode(Node): ...@@ -640,12 +640,13 @@ class ExprNode(Node):
# Differs from analyse_types as it avoids unnecessary # Differs from analyse_types as it avoids unnecessary
# analysis of subexpressions, but can assume everything # analysis of subexpressions, but can assume everything
# in self.type_dependencies() has been resolved. # in self.type_dependencies() has been resolved.
if hasattr(self, 'type') and self.type is not None: type = getattr(self, 'type', None)
return self.type if type is not None:
elif hasattr(self, 'entry') and self.entry is not None: return type
return self.entry.type entry = getattr(self, 'entry', None)
else: if entry is not None:
self.not_implemented("infer_type") return entry.type
self.not_implemented("infer_type")
def nonlocally_immutable(self): def nonlocally_immutable(self):
# Returns whether this variable is a safe reference, i.e. # Returns whether this variable is a safe reference, i.e.
...@@ -2243,7 +2244,6 @@ class NameNode(AtomicExprNode): ...@@ -2243,7 +2244,6 @@ class NameNode(AtomicExprNode):
return entry.cname return entry.cname
def generate_result_code(self, code): def generate_result_code(self, code):
assert hasattr(self, 'entry')
entry = self.entry entry = self.entry
if entry is None: if entry is None:
return # There was an error earlier return # There was an error earlier
...@@ -2533,7 +2533,7 @@ class NameNode(AtomicExprNode): ...@@ -2533,7 +2533,7 @@ class NameNode(AtomicExprNode):
error(self.pos, "Deletion of C names not supported") error(self.pos, "Deletion of C names not supported")
def annotate(self, code): def annotate(self, code):
if hasattr(self, 'is_called') and self.is_called: if getattr(self, 'is_called', False):
pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1) pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
if self.type.is_pyobject: if self.type.is_pyobject:
style, text = 'py_call', 'python function (%s)' style, text = 'py_call', 'python function (%s)'
...@@ -6977,7 +6977,7 @@ class AttributeNode(ExprNode): ...@@ -6977,7 +6977,7 @@ class AttributeNode(ExprNode):
return module_scope.lookup_type(self.attribute) return module_scope.lookup_type(self.attribute)
if not self.obj.is_string_literal: if not self.obj.is_string_literal:
base_type = self.obj.analyse_as_type(env) base_type = self.obj.analyse_as_type(env)
if base_type and hasattr(base_type, 'scope') and base_type.scope is not None: if base_type and getattr(base_type, 'scope', None) is not None:
return base_type.scope.lookup_type(self.attribute) return base_type.scope.lookup_type(self.attribute)
return None return None
...@@ -13525,11 +13525,13 @@ class ProxyNode(CoercionNode): ...@@ -13525,11 +13525,13 @@ class ProxyNode(CoercionNode):
return self.arg.infer_type(env) return self.arg.infer_type(env)
def _proxy_type(self): def _proxy_type(self):
if hasattr(self.arg, 'type'): type = getattr(self.arg, 'type', None)
self.type = self.arg.type if type:
self.type = type
self.result_ctype = self.arg.result_ctype self.result_ctype = self.arg.result_ctype
if hasattr(self.arg, 'entry'): arg_entry = getattr(self.arg, 'entry', None)
self.entry = self.arg.entry if arg_entry:
self.entry = arg_entry
def generate_result_code(self, code): def generate_result_code(self, code):
self.arg.generate_result_code(code) self.arg.generate_result_code(code)
...@@ -13566,11 +13568,13 @@ class CloneNode(CoercionNode): ...@@ -13566,11 +13568,13 @@ class CloneNode(CoercionNode):
def __init__(self, arg): def __init__(self, arg):
CoercionNode.__init__(self, arg) CoercionNode.__init__(self, arg)
self.constant_result = arg.constant_result self.constant_result = arg.constant_result
if hasattr(arg, 'type'): type = getattr(arg, 'type', None)
self.type = arg.type if type:
self.type = type
self.result_ctype = arg.result_ctype self.result_ctype = arg.result_ctype
if hasattr(arg, 'entry'): arg_entry = getattr(arg, 'entry', None)
self.entry = arg.entry if arg_entry:
self.entry = arg_entry
def result(self): def result(self):
return self.arg.result() return self.arg.result()
...@@ -13588,8 +13592,9 @@ class CloneNode(CoercionNode): ...@@ -13588,8 +13592,9 @@ class CloneNode(CoercionNode):
self.type = self.arg.type self.type = self.arg.type
self.result_ctype = self.arg.result_ctype self.result_ctype = self.arg.result_ctype
self.is_temp = 1 self.is_temp = 1
if hasattr(self.arg, 'entry'): arg_entry = getattr(self.arg, 'entry', None)
self.entry = self.arg.entry if arg_entry:
self.entry = arg_entry
return self return self
def coerce_to(self, dest_type, env): def coerce_to(self, dest_type, env):
......
...@@ -895,8 +895,9 @@ class CArgDeclNode(Node): ...@@ -895,8 +895,9 @@ class CArgDeclNode(Node):
could_be_name = False could_be_name = False
self.base_type.is_arg = True self.base_type.is_arg = True
base_type = self.base_type.analyse(env, could_be_name=could_be_name) base_type = self.base_type.analyse(env, could_be_name=could_be_name)
if hasattr(self.base_type, 'arg_name') and self.base_type.arg_name: base_arg_name = getattr(self.base_type, 'arg_name', None)
self.declarator.name = self.base_type.arg_name if base_arg_name:
self.declarator.name = base_arg_name
# The parser is unable to resolve the ambiguity of [] as part of the # The parser is unable to resolve the ambiguity of [] as part of the
# type (e.g. in buffers) or empty declarator (as with arrays). # type (e.g. in buffers) or empty declarator (as with arrays).
...@@ -9319,9 +9320,6 @@ class ParallelRangeNode(ParallelStatNode): ...@@ -9319,9 +9320,6 @@ class ParallelRangeNode(ParallelStatNode):
else: else:
self.start, self.stop, self.step = self.args self.start, self.stop, self.step = self.args
if hasattr(self.schedule, 'decode'):
self.schedule = self.schedule.decode('ascii')
if self.schedule not in (None, 'static', 'dynamic', 'guided', 'runtime'): if self.schedule not in (None, 'static', 'dynamic', 'guided', 'runtime'):
error(self.pos, "Invalid schedule argument to prange: %s" % (self.schedule,)) error(self.pos, "Invalid schedule argument to prange: %s" % (self.schedule,))
...@@ -9380,7 +9378,8 @@ class ParallelRangeNode(ParallelStatNode): ...@@ -9380,7 +9378,8 @@ class ParallelRangeNode(ParallelStatNode):
# ensure lastprivate behaviour and propagation. If the target index is # ensure lastprivate behaviour and propagation. If the target index is
# not a NameNode, it won't have an entry, and an error was issued by # not a NameNode, it won't have an entry, and an error was issued by
# ParallelRangeTransform # ParallelRangeTransform
if hasattr(self.target, 'entry'): target_entry = getattr(self.target, 'entry', None)
if target_entry:
self.assignments[self.target.entry] = self.target.pos, None self.assignments[self.target.entry] = self.target.pos, None
node = super(ParallelRangeNode, self).analyse_expressions(env) node = super(ParallelRangeNode, self).analyse_expressions(env)
......
...@@ -122,8 +122,7 @@ class ResultRefNode(AtomicExprNode): ...@@ -122,8 +122,7 @@ class ResultRefNode(AtomicExprNode):
self.may_hold_none = may_hold_none self.may_hold_none = may_hold_none
if expression is not None: if expression is not None:
self.pos = expression.pos self.pos = expression.pos
if hasattr(expression, "type"): self.type = getattr(expression, "type", None)
self.type = expression.type
if pos is not None: if pos is not None:
self.pos = pos self.pos = pos
if type is not None: if type is not None:
...@@ -144,8 +143,9 @@ class ResultRefNode(AtomicExprNode): ...@@ -144,8 +143,9 @@ class ResultRefNode(AtomicExprNode):
def update_expression(self, expression): def update_expression(self, expression):
self.expression = expression self.expression = expression
if hasattr(expression, "type"): type = getattr(expression, "type", None)
self.type = expression.type if type:
self.type = type
def analyse_types(self, env): def analyse_types(self, env):
if self.expression is not None: if self.expression is not None:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment