# HG changeset patch # User Paul Boddie # Date 1367443434 -7200 # Node ID b743b8b066dcb52855452125ec900684f9afbf54 # Parent a8fbd7fc783f73ac23acfefa77a205543810fff0 Added a special base class providing specific annotations for attribute access. Made the Node.visit method return results of default method invocations. Tidied the docstring output. Fixed the exec output. diff -r a8fbd7fc783f -r b743b8b066dc compiler/ast.py --- a/compiler/ast.py Tue Mar 12 00:12:25 2013 +0100 +++ b/compiler/ast.py Wed May 01 23:23:54 2013 +0200 @@ -80,6 +80,27 @@ self._attrtypes = None self._attrspecifictypes = None +class AttributeAccessor: + + "Access-related node." + + def __init__(self): + self._attr = None + self._attrusers = None + self._username = None + + # Deductions. + + self._access_type = None + self._value_deduced = None + self._attr_deduced = None + self._set_context = None + + self._attrs_deduced = None + self._attrs_deduced_from_usage = None + self._attrs_deduced_from_specific_usage = None + self._position_deduced = None + class OperatorUser: "Operator-related node." @@ -105,7 +126,7 @@ pass # implemented by subclasses def visit(self, visitor, *args): - visitor.default(self, *args) + return visitor.default(self, *args) class EmptyNode(Node): pass @@ -178,9 +199,10 @@ def visit(self, visitor, *args): return visitor.visitAnd(self, *args) -class AssAttr(Node): +class AssAttr(Node, AttributeAccessor): def __init__(self, expr, attrname, flags, lineno=None): Node.__init__(self) + AttributeAccessor.__init__(self) self.expr = expr self.attrname = attrname self.flags = flags @@ -189,9 +211,6 @@ # Additional annotations. self._expr = None - self._attr = None - self._attrusers = None - self._username = None def getChildren(self): return self.expr, self.attrname, self.flags @@ -553,7 +572,7 @@ self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", self.name, self.bases and "(%s)" % ", ".join(map(str, self.bases)) or "", - indent(self.doc and "\n%s" % docstring(self.doc) or ""), + self.doc and "\n\t" + docstring(self.doc) or "", indent("\n%s" % self.code) ) @@ -768,7 +787,7 @@ return "Exec(%r, %r, %r)" % (self.expr, self.locals, self.globals) def __str__(self): - return "exec %s%s%s" % (self.expr, self.locals and ", %s" % self.locals or "", + return "exec %s%s%s" % (self.expr, self.locals and "in %s" % self.locals or "", self.globals and ", %s" % self.globals or "") def visit(self, visitor, *args): @@ -910,7 +929,7 @@ self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", self.name, ", ".join(parameters), - indent(self.doc and "\n\n%s\n" % docstring(self.doc) or ""), + self.doc and "\n\n\t%s\n" % docstring(self.doc) or "", indent("\n%s" % self.code) ) @@ -1024,9 +1043,10 @@ def visit(self, visitor, *args): return visitor.visitGenExprInner(self, *args) -class Getattr(Node): +class Getattr(Node, AttributeAccessor): def __init__(self, expr, attrname, lineno=None): Node.__init__(self) + AttributeAccessor.__init__(self) self.expr = expr self.attrname = attrname self.lineno = lineno @@ -1034,9 +1054,6 @@ # Additional annotations. self._expr = None - self._attr = None - self._attrusers = None - self._username = None def getChildren(self): return self.expr, self.attrname @@ -1492,18 +1509,16 @@ def visit(self, visitor, *args): return visitor.visitMul(self, *args) -class Name(Node): +class Name(Node, AttributeAccessor): def __init__(self, name, lineno=None): Node.__init__(self) + AttributeAccessor.__init__(self) self.name = name self.lineno = lineno # Additional annotations. self._scope = None - self._attr = None - self._attrusers = None - self._username = None def getChildren(self): return self.name, @@ -1878,9 +1893,10 @@ def visit(self, visitor, *args): return visitor.visitSub(self, *args) -class Subscript(Node, OperatorUser): +class Subscript(Node, AttributeAccessor, OperatorUser): def __init__(self, expr, flags, subs, lineno=None): Node.__init__(self) + AttributeAccessor.__init__(self) OperatorUser.__init__(self) self.expr = expr self.flags = flags @@ -1890,9 +1906,6 @@ # Additional annotations. self._expr = None - self._attr = None - self._attrusers = None - self._username = None def getChildren(self): children = []