Skip to content

Commit ba78392

Browse files
committed
Use iterator-based range instead of a list-based range for buffer filling
1 parent 13944e5 commit ba78392

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

quirc/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def __init__(self, width, height):
6666
self._width = width
6767
self._height = height
6868

69+
self._width_iter = compat.range(self._width)
70+
self._height_iter = compat.range(self._height)
71+
6972
self._obj = api.new()
7073
api.resize(self._obj, self._width, self._height)
7174

@@ -80,8 +83,8 @@ def decode(self, image):
8083
pixels = image.load()
8184

8285
idx = 0
83-
for i in range(self._width):
84-
for j in range(self._height):
86+
for i in self._width_iter:
87+
for j in self._height_iter:
8588
buffer[idx] = ctypes.c_uint8(pixels[j, i])
8689
idx += 1
8790

quirc/compat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
"""Miscellaneous compatibility checks"""
44

5+
import sys
6+
7+
8+
range = range if sys.version_info[0] == 3 else xrange
9+
510
# Image libraries
611
have_pil = True
712
try:

quirc/converters.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import ctypes
99

10+
from compat import range
11+
1012

1113
def pil(image):
1214
"""Convert the PIL.Image object to the pixels buffer
@@ -21,6 +23,9 @@ def pil(image):
2123

2224
pixels = image.load()
2325

24-
for i in range(width):
25-
for j in range(height):
26+
width_iter = range(width)
27+
height_iter = range(height)
28+
29+
for i in width_iter:
30+
for j in height_iter:
2631
yield ctypes.c_uint8(pixels[j, i])

0 commit comments

Comments
 (0)