Make iterators python 3 and 2 compatible.

This commit is contained in:
Holger Rapp 2014-02-14 09:14:20 +01:00
parent b438af2653
commit ccf44a61ff
3 changed files with 34 additions and 30 deletions

View File

@ -15,16 +15,17 @@ class _LineIterator(object):
def __iter__(self): def __iter__(self):
return self return self
def __next__(self):
"""Returns the next line."""
self._line_index, line = next(self._lines)
return line
next = __next__ # for python2
@property @property
def line_index(self): def line_index(self):
"""The 1 based line index in the current file.""" """The 1 based line index in the current file."""
return self._line_index return self._line_index
def next(self):
"""Returns the next line."""
self._line_index, line = next(self._lines)
return line
def _handle_snippet_or_global(line, lines, globals): def _handle_snippet_or_global(line, lines, globals):
"""Parses the snippet that begins at the current line.""" """Parses the snippet that begins at the current line."""

View File

@ -27,8 +27,8 @@ class _TextIterator(object):
"""Iterator interface.""" """Iterator interface."""
return self return self
def next(self): def __next__(self):
"""Returns the next character. Part of the iterator interface.""" """Returns the next character."""
if self._idx >= len(self._text): if self._idx >= len(self._text):
raise StopIteration raise StopIteration
@ -39,8 +39,8 @@ class _TextIterator(object):
else: else:
self._col += 1 self._col += 1
self._idx += 1 self._idx += 1
return rv return rv
next = __next__ # for python2
def peek(self, count=1): def peek(self, count=1):
"""Returns the next 'count' characters without advancing the stream.""" """Returns the next 'count' characters without advancing the stream."""
@ -63,7 +63,7 @@ def _parse_number(stream):
""" """
rv = "" rv = ""
while stream.peek() and stream.peek() in string.digits: while stream.peek() and stream.peek() in string.digits:
rv += stream.next() rv += next(stream)
return int(rv) return int(rv)
@ -78,9 +78,9 @@ def _parse_till_closing_brace(stream):
in_braces = 1 in_braces = 1
while True: while True:
if EscapeCharToken.starts_here(stream, '{}'): if EscapeCharToken.starts_here(stream, '{}'):
rv += stream.next() + stream.next() rv += next(stream) + next(stream)
else: else:
char = stream.next() char = next(stream)
if char == '{': if char == '{':
in_braces += 1 in_braces += 1
elif char == '}': elif char == '}':
@ -102,10 +102,10 @@ def _parse_till_unescaped_char(stream, chars):
escaped = False escaped = False
for char in chars: for char in chars:
if EscapeCharToken.starts_here(stream, char): if EscapeCharToken.starts_here(stream, char):
rv += stream.next() + stream.next() rv += next(stream) + next(stream)
escaped = True escaped = True
if not escaped: if not escaped:
char = stream.next() char = next(stream)
if char in chars: if char in chars:
break break
rv += char rv += char
@ -135,13 +135,13 @@ class TabStopToken(Token):
return cls.CHECK.match(stream.peek(10)) is not None return cls.CHECK.match(stream.peek(10)) is not None
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # $ next(stream) # $
stream.next() # { next(stream) # {
self.number = _parse_number(stream) self.number = _parse_number(stream)
if stream.peek() == ":": if stream.peek() == ":":
stream.next() next(stream)
self.initial_text = _parse_till_closing_brace(stream) self.initial_text = _parse_till_closing_brace(stream)
def __repr__(self): def __repr__(self):
@ -161,10 +161,10 @@ class VisualToken(Token):
def _parse(self, stream, indent): def _parse(self, stream, indent):
for _ in range(8): # ${VISUAL for _ in range(8): # ${VISUAL
stream.next() next(stream)
if stream.peek() == ":": if stream.peek() == ":":
stream.next() next(stream)
self.alternative_text, char = _parse_till_unescaped_char(stream, '/}') self.alternative_text, char = _parse_till_unescaped_char(stream, '/}')
self.alternative_text = unescape(self.alternative_text) self.alternative_text = unescape(self.alternative_text)
@ -198,12 +198,12 @@ class TransformationToken(Token):
return cls.CHECK.match(stream.peek(10)) is not None return cls.CHECK.match(stream.peek(10)) is not None
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # $ next(stream) # $
stream.next() # { next(stream) # {
self.number = _parse_number(stream) self.number = _parse_number(stream)
stream.next() # / next(stream) # /
self.search = _parse_till_unescaped_char(stream, '/')[0] self.search = _parse_till_unescaped_char(stream, '/')[0]
self.replace = _parse_till_unescaped_char(stream, '/')[0] self.replace = _parse_till_unescaped_char(stream, '/')[0]
@ -225,7 +225,7 @@ class MirrorToken(Token):
return cls.CHECK.match(stream.peek(10)) is not None return cls.CHECK.match(stream.peek(10)) is not None
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # $ next(stream) # $
self.number = _parse_number(stream) self.number = _parse_number(stream)
def __repr__(self): def __repr__(self):
@ -244,8 +244,8 @@ class EscapeCharToken(Token):
return True return True
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # \ next(stream) # \
self.initial_text = stream.next() self.initial_text = next(stream)
def __repr__(self): def __repr__(self):
return "EscapeCharToken(%r,%r,%r)" % ( return "EscapeCharToken(%r,%r,%r)" % (
@ -261,7 +261,7 @@ class ShellCodeToken(Token):
return stream.peek(1) == '`' return stream.peek(1) == '`'
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # ` next(stream) # `
self.code = _parse_till_unescaped_char(stream, '`')[0] self.code = _parse_till_unescaped_char(stream, '`')[0]
def __repr__(self): def __repr__(self):
@ -281,9 +281,9 @@ class PythonCodeToken(Token):
def _parse(self, stream, indent): def _parse(self, stream, indent):
for _ in range(3): for _ in range(3):
stream.next() # `!p next(stream) # `!p
if stream.peek() in '\t ': if stream.peek() in '\t ':
stream.next() next(stream)
code = _parse_till_unescaped_char(stream, '`')[0] code = _parse_till_unescaped_char(stream, '`')[0]
@ -314,7 +314,7 @@ class VimLCodeToken(Token):
def _parse(self, stream, indent): def _parse(self, stream, indent):
for _ in range(4): for _ in range(4):
stream.next() # `!v next(stream) # `!v
self.code = _parse_till_unescaped_char(stream, '`')[0] self.code = _parse_till_unescaped_char(stream, '`')[0]
def __repr__(self): def __repr__(self):
@ -344,6 +344,6 @@ def tokenize(text, indent, offset):
done_something = True done_something = True
break break
if not done_something: if not done_something:
stream.next() next(stream)
except StopIteration: except StopIteration:
yield EndOfTextToken(stream, indent) yield EndOfTextToken(stream, indent)

View File

@ -96,6 +96,9 @@ class VimInterface:
tries = 50 tries = 50
while tries: while tries:
if os.path.exists(fn): if os.path.exists(fn):
if sys.version_info >= (3,0):
return open(fn,"r", encoding="utf-8").read()[:-1]
else:
return open(fn,"r").read()[:-1] return open(fn,"r").read()[:-1]
time.sleep(.05) time.sleep(.05)
tries -= 1 tries -= 1