summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/pytree.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/pytree.py')
-rw-r--r--Lib/lib2to3/pytree.py76
1 files changed, 55 insertions, 21 deletions
diff --git a/Lib/lib2to3/pytree.py b/Lib/lib2to3/pytree.py
index 2a6ef2e..179caca 100644
--- a/Lib/lib2to3/pytree.py
+++ b/Lib/lib2to3/pytree.py
@@ -13,7 +13,8 @@ There's also a pattern matching implementation here.
__author__ = "Guido van Rossum <guido@python.org>"
import sys
-from io import StringIO
+import warnings
+from StringIO import StringIO
HUGE = 0x7FFFFFFF # maximum repeat count, default max
@@ -63,6 +64,16 @@ class Base(object):
__hash__ = None # For Py3 compatibility.
+ def __ne__(self, other):
+ """
+ Compare two nodes for inequality.
+
+ This calls the method _eq().
+ """
+ if self.__class__ is not other.__class__:
+ return NotImplemented
+ return not self._eq(other)
+
def _eq(self, other):
"""
Compare two nodes for equality.
@@ -98,6 +109,26 @@ class Base(object):
"""
raise NotImplementedError
+ def set_prefix(self, prefix):
+ """
+ Set the prefix for the node (see Leaf class).
+
+ DEPRECATED; use the prefix property directly.
+ """
+ warnings.warn("set_prefix() is deprecated; use the prefix property",
+ DeprecationWarning, stacklevel=2)
+ self.prefix = prefix
+
+ def get_prefix(self):
+ """
+ Return the prefix for the node (see Leaf class).
+
+ DEPRECATED; use the prefix property directly.
+ """
+ warnings.warn("get_prefix() is deprecated; use the prefix property",
+ DeprecationWarning, stacklevel=2)
+ return self.prefix
+
def replace(self, new):
"""Replace this node with a new one in the parent."""
assert self.parent is not None, str(self)
@@ -183,7 +214,8 @@ class Base(object):
def leaves(self):
for child in self.children:
- yield from child.leaves()
+ for x in child.leaves():
+ yield x
def depth(self):
if self.parent is None:
@@ -197,12 +229,12 @@ class Base(object):
"""
next_sib = self.next_sibling
if next_sib is None:
- return ""
+ return u""
return next_sib.prefix
if sys.version_info < (3, 0):
def __str__(self):
- return str(self).encode("ascii")
+ return unicode(self).encode("ascii")
class Node(Base):
@@ -245,7 +277,7 @@ class Node(Base):
This reproduces the input source exactly.
"""
- return "".join(map(str, self.children))
+ return u"".join(map(unicode, self.children))
if sys.version_info > (3, 0):
__str__ = __unicode__
@@ -262,17 +294,18 @@ class Node(Base):
def post_order(self):
"""Return a post-order iterator for the tree."""
for child in self.children:
- yield from child.post_order()
+ for node in child.post_order():
+ yield node
yield self
def pre_order(self):
"""Return a pre-order iterator for the tree."""
yield self
for child in self.children:
- yield from child.pre_order()
+ for node in child.pre_order():
+ yield node
- @property
- def prefix(self):
+ def _prefix_getter(self):
"""
The whitespace and comments preceding this node in the input.
"""
@@ -280,11 +313,12 @@ class Node(Base):
return ""
return self.children[0].prefix
- @prefix.setter
- def prefix(self, prefix):
+ def _prefix_setter(self, prefix):
if self.children:
self.children[0].prefix = prefix
+ prefix = property(_prefix_getter, _prefix_setter)
+
def set_child(self, i, child):
"""
Equivalent to 'node.children[i] = child'. This method also sets the
@@ -354,7 +388,7 @@ class Leaf(Base):
This reproduces the input source exactly.
"""
- return self.prefix + str(self.value)
+ return self.prefix + unicode(self.value)
if sys.version_info > (3, 0):
__str__ = __unicode__
@@ -380,18 +414,18 @@ class Leaf(Base):
"""Return a pre-order iterator for the tree."""
yield self
- @property
- def prefix(self):
+ def _prefix_getter(self):
"""
The whitespace and comments preceding this token in the input.
"""
return self._prefix
- @prefix.setter
- def prefix(self, prefix):
+ def _prefix_setter(self, prefix):
self.changed()
self._prefix = prefix
+ prefix = property(_prefix_getter, _prefix_setter)
+
def convert(gr, raw_node):
"""
Convert raw node information to a Node or Leaf instance.
@@ -514,7 +548,7 @@ class LeafPattern(BasePattern):
if type is not None:
assert 0 <= type < 256, type
if content is not None:
- assert isinstance(content, str), repr(content)
+ assert isinstance(content, basestring), repr(content)
self.type = type
self.content = content
self.name = name
@@ -564,7 +598,7 @@ class NodePattern(BasePattern):
if type is not None:
assert type >= 256, type
if content is not None:
- assert not isinstance(content, str), repr(content)
+ assert not isinstance(content, basestring), repr(content)
content = list(content)
for i, item in enumerate(content):
assert isinstance(item, BasePattern), (i, item)
@@ -699,7 +733,7 @@ class WildcardPattern(BasePattern):
"""
if self.content is None:
# Shortcut for special case (see __init__.__doc__)
- for count in range(self.min, 1 + min(len(nodes), self.max)):
+ for count in xrange(self.min, 1 + min(len(nodes), self.max)):
r = {}
if self.name:
r[self.name] = nodes[:count]
@@ -709,8 +743,8 @@ class WildcardPattern(BasePattern):
else:
# The reason for this is that hitting the recursion limit usually
# results in some ugly messages about how RuntimeErrors are being
- # ignored. We only have to do this on CPython, though, because other
- # implementations don't have this nasty bug in the first place.
+ # ignored. We don't do this on non-CPython implementation because
+ # they don't have this problem.
if hasattr(sys, "getrefcount"):
save_stderr = sys.stderr
sys.stderr = StringIO()