Skip to content

Commit 87bc399

Browse files
committed
Add Python 3 support.
1 parent fd5cc21 commit 87bc399

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

setup.py

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
MINIMUM_CYTHON_VERSION = '0.13'
88

9+
10+
def cmp(a, b):
11+
return (a > b) - (a < b)
12+
13+
914
class TestCommand(Command):
1015
description = 'Run packaged tests'
1116
user_options = []

src/re2.pyx

+18-14
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ cdef class Match:
180180
return self._groups[1:]
181181

182182
def group(self, *args):
183+
try:
184+
string = basestring
185+
except NameError as e:
186+
string = (str, bytes)
183187
if len(args) > 1:
184188
return tuple([self.group(i) for i in args])
185189
elif len(args) > 0:
@@ -191,22 +195,22 @@ cdef class Match:
191195

192196
self.init_groups()
193197

194-
if isinstance(groupnum, basestring):
198+
if isinstance(groupnum, string):
195199
return self.groupdict()[groupnum]
196200

197201
idx = groupnum
198202

199203
if idx > self.nmatches - 1:
200204
raise IndexError("no such group")
201205
return self._groups[idx]
202-
206+
203207
cdef object _convert_positions(self, positions):
204208
cdef char * s = self.match_string
205209
cdef int cpos = 0
206210
cdef int upos = 0
207211
cdef int size = len(self.match_string)
208-
cdef int c
209-
212+
cdef int c
213+
210214
new_positions = []
211215
i = 0
212216
num_positions = len(positions)
@@ -253,7 +257,7 @@ cdef class Match:
253257
posdict = dict(zip(positions, self._convert_positions(positions)))
254258

255259
return [(posdict[x], posdict[y]) for x,y in spans]
256-
260+
257261

258262
cdef _make_spans(self):
259263
if self._spans is not None:
@@ -274,7 +278,7 @@ cdef class Match:
274278
start = piece.data() - s
275279
end = start + piece.length()
276280
spans.append((start, end))
277-
281+
278282
if self.encoded:
279283
spans = self._convert_spans(spans)
280284

@@ -361,13 +365,13 @@ cdef class Match:
361365

362366
if self._lastindex < 1:
363367
return None
364-
368+
365369
it = self.named_groups.begin()
366370
while it != self.named_groups.end():
367371
if deref(it).second == self._lastindex:
368372
return cpp_to_pystring(deref(it).first)
369373
inc(it)
370-
374+
371375
return None
372376

373377

@@ -655,7 +659,7 @@ cdef class Pattern:
655659
if fixed_repl == NULL:
656660
fixed_repl = new _re2.cpp_string(cstring, s - cstring - 1)
657661
if c == 'n':
658-
fixed_repl.push_back('\n')
662+
fixed_repl.push_back('\n')
659663
else:
660664
fixed_repl.push_back('\\')
661665
fixed_repl.push_back('\\')
@@ -853,14 +857,14 @@ def prepare_pattern(pattern, int flags):
853857
elif this[1] == 'D':
854858
new_pattern.append(r'\P{Nd}')
855859
elif this[1] == 'W':
856-
# Since \w and \s are made out of several character groups,
860+
# Since \w and \s are made out of several character groups,
857861
# I don't see a way to convert their complements into a group
858862
# without rewriting the whole expression, which seems too complicated.
859863

860864
raise CharClassProblemException()
861865
elif this[1] == 'S':
862866
raise CharClassProblemException()
863-
else:
867+
else:
864868
new_pattern.append(this)
865869
else:
866870
new_pattern.append(this)
@@ -899,7 +903,7 @@ def prepare_pattern(pattern, int flags):
899903

900904
return ''.join(new_pattern)
901905

902-
906+
903907

904908
def _compile(pattern, int flags=0, int max_mem=8388608):
905909
"""
@@ -936,7 +940,7 @@ def _compile(pattern, int flags=0, int max_mem=8388608):
936940
elif current_notification == <int>FALLBACK_WARNING:
937941
warnings.warn("WARNING: Using re module. Reason: %s" % error_msg)
938942
return re.compile(original_pattern, flags)
939-
943+
940944
# Set the options given the flags above.
941945
if flags & _I:
942946
opts.set_case_sensitive(0);
@@ -968,7 +972,7 @@ def _compile(pattern, int flags=0, int max_mem=8388608):
968972
raise RegexError(error_msg)
969973
elif error_code not in (_re2.ErrorBadPerlOp, _re2.ErrorRepeatSize,
970974
_re2.ErrorBadEscape):
971-
# Raise an error because these will not be fixed by using the
975+
# Raise an error because these will not be fixed by using the
972976
# ``re`` module.
973977
raise RegexError(error_msg)
974978
elif current_notification == <int>FALLBACK_WARNING:

0 commit comments

Comments
 (0)