Permalink
Browse files

Switched from `xrange` to `range` -- to be Python3.2 compatible

  • Loading branch information...
1 parent 802cc02 commit aa6e43e28bea2ecfa0bf9b71ca44a370b559b142 @superbobry superbobry committed Jun 24, 2011
Showing with 62 additions and 54 deletions.
  1. +39 −31 pyte/screens.py
  2. +12 −12 tests/test_diff.py
  3. +10 −10 tests/test_history.py
  4. +1 −1 tox.ini
View
70 pyte/screens.py
@@ -37,6 +37,14 @@
from . import modes as mo, graphics as g, charsets as cs
+try:
+ xrange
+except NameError:
+ pass
+else:
+ range = xrange
+
+
def take(n, iterable):
"""Returns first n items of the iterable as a list."""
return list(islice(iterable, n))
@@ -168,7 +176,7 @@ def reset(self):
:manpage:`xterm` -- we now know that.
"""
self[:] = (take(self.columns, self.default_line)
- for _ in xrange(self.lines))
+ for _ in range(self.lines))
self.mode = set([mo.DECAWM, mo.DECTCEM, mo.LNM, mo.DECTCEM])
self.margins = Margins(0, self.lines - 1)
@@ -181,7 +189,7 @@ def reset(self):
# From ``man terminfo`` -- "... hardware tabs are initially
# set every `n` spaces when the terminal is powered up. Since
# we aim to support VT102 / VT220 and linux -- we use n = 8.
- self.tabstops = set(xrange(7, self.columns, 8))
+ self.tabstops = set(range(7, self.columns, 8))
self.cursor = Cursor(0, 0)
self.cursor_position()
@@ -213,7 +221,7 @@ def resize(self, lines=None, columns=None):
# size, add lines to the bottom.
if diff < 0:
self.extend(take(self.columns, self.default_line)
- for _ in xrange(diff, 0))
+ for _ in range(diff, 0))
# b) if the current display size is greater than requested
# size, take lines off the top.
elif diff > 0:
@@ -225,7 +233,7 @@ def resize(self, lines=None, columns=None):
# a) if the current display size is less than the requested
# size, expand each line to the new size.
if diff < 0:
- for y in xrange(lines):
+ for y in range(lines):
self[y].extend(take(abs(diff), self.default_line))
# b) if the current display size is greater than requested
# size, trim each line from the right to the new size.
@@ -474,8 +482,8 @@ def insert_lines(self, count=None):
# If cursor is outside scrolling margins it -- do nothin'.
if top <= self.cursor.y <= bottom:
- # v +1, because xrange() is exclusive.
- for line in xrange(self.cursor.y, min(bottom + 1, self.cursor.y + count)):
+ # v +1, because range() is exclusive.
+ for line in range(self.cursor.y, min(bottom + 1, self.cursor.y + count)):
self.pop(bottom)
self.insert(line, take(self.columns, self.default_line))
@@ -495,7 +503,7 @@ def delete_lines(self, count=None):
# If cursor is outside scrolling margins it -- do nothin'.
if top <= self.cursor.y <= bottom:
# v -- +1 to include the bottom margin.
- for _ in xrange(min(bottom - self.cursor.y + 1, count)):
+ for _ in range(min(bottom - self.cursor.y + 1, count)):
self.pop(self.cursor.y)
self.insert(bottom, list(
repeat(self.cursor.attrs, self.columns)))
@@ -512,7 +520,7 @@ def insert_characters(self, count=None):
"""
count = count or 1
- for _ in xrange(min(self.columns - self.cursor.y, count)):
+ for _ in range(min(self.columns - self.cursor.y, count)):
self[self.cursor.y].insert(self.cursor.x, self.cursor.attrs)
self[self.cursor.y].pop()
@@ -526,7 +534,7 @@ def delete_characters(self, count=None):
"""
count = count or 1
- for _ in xrange(min(self.columns - self.cursor.x, count)):
+ for _ in range(min(self.columns - self.cursor.x, count)):
self[self.cursor.y].pop(self.cursor.x)
self[self.cursor.y].append(self.cursor.attrs)
@@ -546,7 +554,7 @@ def erase_characters(self, count=None):
"""
count = count or 1
- for column in xrange(self.cursor.x, min(self.cursor.x + count, self.columns)):
+ for column in range(self.cursor.x, min(self.cursor.x + count, self.columns)):
self[self.cursor.y][column] = self.cursor.attrs
def erase_in_line(self, type_of=0, private=False):
@@ -565,12 +573,12 @@ def erase_in_line(self, type_of=0, private=False):
interval = (
# a) erase from the cursor to the end of line, including
# the cursor,
- xrange(self.cursor.x, self.columns),
+ range(self.cursor.x, self.columns),
# b) erase from the beginning of the line to the cursor,
# including it,
- xrange(0, self.cursor.x + 1),
+ range(0, self.cursor.x + 1),
# c) erase the entire line.
- xrange(0, self.columns)
+ range(0, self.columns)
)[type_of]
for column in interval:
@@ -593,17 +601,17 @@ def erase_in_display(self, type_of=0, private=False):
interval = (
# a) erase from cursor to the end of the display, including
# the cursor,
- xrange(self.cursor.y + 1, self.lines),
+ range(self.cursor.y + 1, self.lines),
# b) erase from the beginning of the display to the cursor,
# including it,
- xrange(0, self.cursor.y),
+ range(0, self.cursor.y),
# c) erase the whole display.
- xrange(0, self.lines)
+ range(0, self.lines)
)[type_of]
for line in interval:
self[line][:] = \
- (self.cursor.attrs for _ in xrange(self.columns))
+ (self.cursor.attrs for _ in range(self.columns))
# In case of 0 or 1 we have to erase the line with the cursor.
if type_of in [0, 1]:
@@ -801,20 +809,20 @@ def __init__(self, *args):
def set_mode(self, *modes, **kwargs):
if mo.DECSCNM >> 5 in modes and kwargs.get("private"):
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).set_mode(*modes, **kwargs)
def reset_mode(self, *modes, **kwargs):
if mo.DECSCNM >> 5 in modes and kwargs.get("private"):
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).reset_mode(*modes, **kwargs)
def reset(self):
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).reset()
def resize(self, *args, **kwargs):
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).resize(*args, **kwargs)
def draw(self, *args):
@@ -823,22 +831,22 @@ def draw(self, *args):
def index(self):
if self.cursor.y == self.margins.bottom:
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).index()
def reverse_index(self):
if self.cursor.y == self.margins.top:
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).reverse_index()
def insert_lines(self, *args):
- self.dirty.update(xrange(self.cursor.y, self.lines))
+ self.dirty.update(range(self.cursor.y, self.lines))
super(DiffScreen, self).insert_lines(*args)
def delete_lines(self, *args):
- self.dirty.update(xrange(self.cursor.y, self.lines))
+ self.dirty.update(range(self.cursor.y, self.lines))
super(DiffScreen, self).delete_lines(*args)
def insert_characters(self, *args):
@@ -859,14 +867,14 @@ def erase_in_line(self, *args):
def erase_in_display(self, type_of=0):
self.dirty.update((
- xrange(self.cursor.y + 1, self.lines),
- xrange(0, self.cursor.y),
- xrange(0, self.lines)
+ range(self.cursor.y + 1, self.lines),
+ range(0, self.cursor.y),
+ range(0, self.lines)
)[type_of])
super(DiffScreen, self).erase_in_display(type_of)
def alignment_display(self):
- self.dirty.update(xrange(self.lines))
+ self.dirty.update(range(self.lines))
super(DiffScreen, self).alignment_display()
@@ -987,7 +995,7 @@ def prev_page(self):
._replace(position=self.history.position - self.lines)
self[:] = list(reversed([
- self.history.top.pop() for _ in xrange(mid)
+ self.history.top.pop() for _ in range(mid)
])) + self[:-mid]
self.ensure_width()
@@ -1008,7 +1016,7 @@ def next_page(self):
._replace(position=self.history.position + self.lines)
self[:] = self[mid:] + [
- self.history.bottom.popleft() for _ in xrange(mid)
+ self.history.bottom.popleft() for _ in range(mid)
]
self.ensure_width()
View
24 tests/test_diff.py
@@ -14,22 +14,22 @@ def test_mark_whole_screen():
# a) init.
assert hasattr(screen, "dirty")
assert isinstance(screen.dirty, set)
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
# b) reset().
screen.dirty.clear()
screen.reset()
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
# c) resize().
screen.dirty.clear()
screen.resize()
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
# d) alignment_display().
screen.dirty.clear()
screen.alignment_display()
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
def test_mark_single_line():
@@ -56,11 +56,11 @@ def test_modes():
screen.dirty.clear()
screen.set_mode(mo.DECSCNM >> 5, private=True)
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
screen.dirty.clear()
screen.reset_mode(mo.DECSCNM >> 5, private=True)
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
def test_index():
@@ -74,7 +74,7 @@ def test_index():
# b) whole screen is dirty.
screen.cursor_to_line(24)
screen.index()
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
def test_reverse_index():
@@ -83,7 +83,7 @@ def test_reverse_index():
# a) not at the top margin -- whole screen is dirty.
screen.reverse_index()
- assert screen.dirty == set(xrange(screen.lines))
+ assert screen.dirty == set(range(screen.lines))
# b) nothing is marked dirty.
screen.dirty.clear()
@@ -99,7 +99,7 @@ def test_insert_delete_lines():
for method in ["insert_lines", "delete_lines"]:
screen.dirty.clear()
getattr(screen, method)()
- assert screen.dirty == set(xrange(screen.cursor.y, screen.lines))
+ assert screen.dirty == set(range(screen.cursor.y, screen.lines))
def test_erase_in_display():
@@ -109,14 +109,14 @@ def test_erase_in_display():
# a) from cursor to the end of the screen.
screen.dirty.clear()
screen.erase_in_display()
- assert screen.dirty == set(xrange(screen.cursor.y, screen.lines))
+ assert screen.dirty == set(range(screen.cursor.y, screen.lines))
# b) from the begining of the screen to cursor.
screen.dirty.clear()
screen.erase_in_display(1)
- assert screen.dirty == set(xrange(0, screen.cursor.y + 1))
+ assert screen.dirty == set(range(0, screen.cursor.y + 1))
# c) whole screen.
screen.dirty.clear()
screen.erase_in_display(2)
- assert screen.dirty == set(xrange(0, screen.lines))
+ assert screen.dirty == set(range(0, screen.lines))
View
20 tests/test_history.py
@@ -17,7 +17,7 @@ def test_index():
# Filling the screen with line numbers, so it's easier to
# track history contents.
- for idx in xrange(len(screen)):
+ for idx in range(len(screen)):
screen.draw(unicode(idx))
if idx is not len(screen) - 1:
screen.linefeed()
@@ -38,7 +38,7 @@ def test_index():
assert screen.history.top[-1] == line
# c) rotation.
- for _ in xrange(len(screen) * screen.lines):
+ for _ in range(len(screen) * screen.lines):
screen.index()
assert len(screen.history.top) == 25 # pages // 2 * lines
@@ -49,7 +49,7 @@ def test_reverse_index():
# Filling the screen with line numbers, so it's easier to
# track history contents.
- for idx in xrange(len(screen)):
+ for idx in range(len(screen)):
screen.draw(unicode(idx))
if idx is not len(screen) - 1:
screen.linefeed()
@@ -72,7 +72,7 @@ def test_reverse_index():
assert screen.history.bottom[1] == line
# c) rotation.
- for _ in xrange(len(screen) * screen.lines):
+ for _ in range(len(screen) * screen.lines):
screen.reverse_index()
assert len(screen.history.bottom) == 25 # pages // 2 * lines
@@ -85,7 +85,7 @@ def test_prev_page():
# Once again filling the screen with line numbers, but this time,
# we need them to span on multiple lines.
- for idx in xrange(len(screen) * 10):
+ for idx in range(len(screen) * 10):
map(screen.draw, unicode(idx))
screen.linefeed()
@@ -152,7 +152,7 @@ def test_prev_page():
# Once again filling the screen with line numbers, but this time,
# we need them to span on multiple lines.
- for idx in xrange(len(screen) * 10):
+ for idx in range(len(screen) * 10):
map(screen.draw, unicode(idx))
screen.linefeed()
@@ -191,7 +191,7 @@ def test_next_page():
# Once again filling the screen with line numbers, but this time,
# we need them to span on multiple lines.
- for idx in xrange(len(screen) * 5):
+ for idx in range(len(screen) * 5):
map(screen.draw, unicode(idx))
screen.linefeed()
@@ -259,7 +259,7 @@ def test_next_page():
def test_ensure_width():
screen = HistoryScreen(5, 5, history=50)
- for idx in xrange(len(screen) * 5):
+ for idx in range(len(screen) * 5):
map(screen.draw, unicode(idx))
screen.linefeed()
@@ -305,7 +305,7 @@ def test_ensure_width():
def test_not_enough_lines():
screen = HistoryScreen(5, 5, history=6)
- for idx in xrange(len(screen)):
+ for idx in range(len(screen)):
map(screen.draw, unicode(idx))
screen.linefeed()
@@ -347,7 +347,7 @@ def test_not_enough_lines():
def test_draw():
screen = HistoryScreen(5, 5, history=50)
- for idx in xrange(len(screen) * 5):
+ for idx in range(len(screen) * 5):
map(screen.draw, unicode(idx))
screen.linefeed()
View
2 tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py26,py27,py32,pypy
+envlist = py26,py27,py32
[testenv]
commands = py.test

0 comments on commit aa6e43e

Please sign in to comment.